*--------------------------------------------------------------------*
* Eae galera, sempre em nossas vidas de ABAP, vamos precisar saber
* fazer download e upload de arquivo, sabemos tambem que estas mesmas funções
* 'GUI_DOWNLOAD' e 'GUI_UPLOAD' estão obsoletas.
* Conforme o 'F1' da função o mesmo indica você usar a classe
* 'CL_GUI_FRONTEND_SERVICES' que será a classe que vamos estudar hoje.
* Tentarei mostrar alguns Metodos interessantes dessa classe que trabalham
* com arquivos e pastas.
* A ideia desse programa será bem simples, ele te possibilita criar uma
* pasta e incluir nela copias dos arquivos de uma outra pasta, e no final
* , como tudo isso é só para teste, existe a possibilidade de deletar todos
* os arquivos copiados juntamente com a pasta caso deseje. Esse programa
* não fara nenhuma alteração de algum arquivo que você já tenha no Desktop,
* irá trabalhar somente com os criados pelo proprio programa.
* Recomendo criar uma pasta qualquer, incluir um arquivo qualquer também
* e começar a brincadeira.
* Como sempre faço, existem algums Breaks no código para forçá-los a
* entender o que cada metodo faz. Abaixo a lista dos metodos usados dessa
* Classe.
* cl_gui_frontend_services=>directory_browse
* cl_gui_frontend_services=>directory_create
* cl_gui_frontend_services=>directory_exist
* cl_gui_frontend_services=>directory_delete
* cl_gui_frontend_services=>directory_list_files
* cl_gui_frontend_services=>file_copy
* cl_gui_frontend_services=>file_exist
* cl_gui_frontend_services=>file_get_attributes
* cl_gui_frontend_services=>file_delete
* Lembando que o código está simples, mais para estudos mesmo, sugestões,
* critina ou qualquer tipo de opnião estamos disponivel no final do post,
* a nomenclatura dos metodos estão bem intuitivas para não precisar comentar
* muito, mas qualquer duvida estamos aí também.
* Bons estudos aeeee.
REPORT z_cl_gui_frontend_services.
TABLES :
sscrfields.
CLASS
zcl_main DEFINITION DEFERRED.
DATA :
o_main TYPE REF TO zcl_main.
DATA :
subrc TYPE sy-subrc,
result TYPE abap_bool,
gt_file_table TYPE rstt_t_files.
* T01 - Pastas
* C01 - Processar
* T02 - Arquivos
* T03 - Deletar arquivos e pastas novas após o processo ?
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-t01.
PARAMETERS :
folder TYPE string,
n_folder TYPE string DEFAULT 'teste'.
SELECTION-SCREEN END OF BLOCK b1.
SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE text-t02.
PARAMETERS :
folder2 TYPE string.
SELECTION-SCREEN END OF BLOCK b2.
SELECTION-SCREEN BEGIN OF BLOCK b3 WITH FRAME TITLE text-t03.
PARAMETERS :
del_fold AS CHECKBOX DEFAULT abap_true,
del_file AS CHECKBOX DEFAULT abap_true.
SELECTION-SCREEN END OF BLOCK b3.
CLASS zcl_main DEFINITION.
PUBLIC SECTION.
METHODS:
*--------------------------------------------------------------------*
directory,
*--------------------------------------------------------------------*
directory_browse
IMPORTING
iv_title TYPE string
CHANGING
cv_folder TYPE string,
*--------------------------------------------------------------------*
directory_create
IMPORTING
iv_directory TYPE string
CHANGING
cv_rc TYPE sy-subrc,
*--------------------------------------------------------------------*
directory_exist
IMPORTING
iv_directory TYPE string
CHANGING
cv_result TYPE abap_bool,
*--------------------------------------------------------------------*
directory_delete
IMPORTING
iv_directory TYPE string
CHANGING
cv_rc TYPE sy-subrc,
*--------------------------------------------------------------------*
directory_list_files
IMPORTING
iv_directory TYPE string
EXPORTING
ev_count TYPE i
CHANGING
ct_file_table TYPE rstt_t_files,
*--------------------------------------------------------------------*
file
CHANGING
ct_file_table TYPE rstt_t_files,
*--------------------------------------------------------------------*
file_copy
IMPORTING
iv_source TYPE string
iv_destination TYPE string
iv_overwrite TYPE abap_bool OPTIONAL
EXPORTING
ev_rc TYPE sy-subrc,
*--------------------------------------------------------------------*
file_exist
IMPORTING
iv_file TYPE string
EXPORTING
ev_result TYPE abap_bool,
*--------------------------------------------------------------------*
file_get_attributes
IMPORTING
iv_file TYPE string
EXPORTING
ev_attributes TYPE char10,
*--------------------------------------------------------------------*
popup_to_confirm_files
CHANGING
ct_file_table TYPE rstt_t_files,
*--------------------------------------------------------------------*
popup_to_confirm
IMPORTING
iv_title TYPE string
iv_question TYPE string
EXPORTING
ev_answer TYPE char1,
*--------------------------------------------------------------------*
delete_folder,
*--------------------------------------------------------------------*
delete_file
CHANGING
ct_file_table TYPE rstt_t_files,
*--------------------------------------------------------------------*
file_delete
IMPORTING
iv_file TYPE string
EXPORTING
ev_rc TYPE sy-subrc
.
ENDCLASS.
CLASS zcl_main IMPLEMENTATION.
METHOD directory.
BREAK-POINT.
IF folder IS NOT INITIAL
AND n_folder IS NOT INITIAL.
me->directory_exist(
EXPORTING
iv_directory = folder && '\' && n_folder
CHANGING
cv_result = result
).
IF result IS INITIAL.
me->directory_create(
EXPORTING
iv_directory = folder && '\' && n_folder
CHANGING
cv_rc = subrc
).
IF subrc IS INITIAL.
WRITE : / `Pasta : ` && folder && '\'
&& n_folder && ` criada`.
ELSE.
WRITE : / `Pasta : ` && folder && '\'
&& n_folder && ` - Problema na criação.`.
ENDIF.
ELSE.
WRITE : / `Pasta : ` && folder && '\'
&& n_folder && ` - Já existe.`.
ENDIF.
ELSE.
WRITE : / `Não foi feito nada, pois você esquece de `
&& `colocar a pasta e/ou diretorio.`.
ENDIF.
ENDMETHOD.
METHOD directory_browse.
cl_gui_frontend_services=>directory_browse(
EXPORTING
window_title = iv_title " Title of Browsing Window
CHANGING
selected_folder = cv_folder " Folder Selected By User
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
not_supported_by_gui = 3
OTHERS = 4
).
ENDMETHOD.
METHOD directory_create.
cl_gui_frontend_services=>directory_create(
EXPORTING
directory = iv_directory " Directory name
CHANGING
rc = cv_rc " Return Code
EXCEPTIONS
directory_create_failed = 1
cntl_error = 2
error_no_gui = 3
directory_access_denied = 4
directory_already_exists = 5
path_not_found = 6
unknown_error = 7
not_supported_by_gui = 8
wrong_parameter = 9
OTHERS = 10
).
ENDMETHOD.
METHOD directory_exist.
cl_gui_frontend_services=>directory_exist(
EXPORTING
directory = iv_directory " Directory name
RECEIVING
result = cv_result " Result
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
wrong_parameter = 3
not_supported_by_gui = 4
OTHERS = 5
).
ENDMETHOD.
METHOD directory_delete.
cl_gui_frontend_services=>directory_delete(
EXPORTING
directory = iv_directory " Directory to Delete
CHANGING
rc = cv_rc " Return Code
EXCEPTIONS
directory_delete_failed = 1
cntl_error = 2
error_no_gui = 3
path_not_found = 4
directory_access_denied = 5
unknown_error = 6
not_supported_by_gui = 7
wrong_parameter = 8
OTHERS = 9
).
ENDMETHOD.
METHOD directory_list_files.
cl_gui_frontend_services=>directory_list_files(
EXPORTING
directory = iv_directory
* filter = '*.*'
* files_only =
* directories_only =
CHANGING
file_table = ct_file_table
count = ev_count
EXCEPTIONS
cntl_error = 1
directory_list_files_failed = 2
wrong_parameter = 3
error_no_gui = 4
not_supported_by_gui = 5
OTHERS = 6
).
ENDMETHOD.
METHOD file.
BREAK-POINT.
IF folder2 IS NOT INITIAL.
DATA :
ls_file_table LIKE LINE OF gt_file_table.
me->directory_list_files(
EXPORTING
iv_directory = folder2
CHANGING
ct_file_table = ct_file_table
).
IF ct_file_table[] IS NOT INITIAL.
me->popup_to_confirm_files(
CHANGING
ct_file_table = ct_file_table
).
IF ct_file_table[] IS NOT INITIAL.
LOOP AT ct_file_table INTO ls_file_table.
me->file_exist(
EXPORTING
iv_file = folder && '\' && n_folder
&& '\' && ls_file_table-filename
IMPORTING
ev_result = result
).
IF result IS NOT INITIAL.
me->popup_to_confirm(
EXPORTING
iv_title = 'Arquivo Existente'
iv_question = `Arquivo : ` && folder && '\' &&
n_folder && '\' && ls_file_table-filename
&& ` - já existe, deseja sobrescrever ?`
IMPORTING
ev_answer = result
).
IF result = '1'. " Sobrescrever !!!!
result = abap_true.
ELSE.
CLEAR result.
ENDIF.
ENDIF.
me->file_copy(
EXPORTING
iv_source = folder2 && '\'
&& ls_file_table-filename
iv_destination = folder && '\' && n_folder
&& '\' && ls_file_table-filename
iv_overwrite = result
IMPORTING
ev_rc = subrc
).
IF subrc IS INITIAL.
DATA :
lv_attributes(10).
me->file_get_attributes(
EXPORTING
iv_file = folder && '\' && n_folder
&& '\' && ls_file_table-filename
IMPORTING
ev_attributes = lv_attributes
).
WRITE : / `Arquivo : ` && folder2 && '\'
&& ls_file_table-filename
&& ` - Copiado com exito. Atributo : ` && lv_attributes.
ELSE.
WRITE : / `Arquivo : ` && folder2
&& '\' && ls_file_table-filename && ` - Erro ao copiar.`.
ENDIF.
ENDLOOP.
ELSE.
WRITE : /
`Não foram selecionados arquivos para cópia da pasta de origem.`.
ENDIF.
ELSE.
WRITE : /
`Não foram encontrados arquivos para cópia da pasta de origem.`.
ENDIF.
ELSE.
WRITE : /
`Não foi definido a pasta de origem dos arquivos a serem copiados.`.
ENDIF.
ENDMETHOD.
METHOD popup_to_confirm_files.
* Caso não lembre.
* http://abapjuniores.blogspot.com.br/2017/06/funcao-reusealvpopuptoselect.html
TYPES :
BEGIN OF ty_popup,
select(1),
text(50),
END OF ty_popup.
DATA :
lt_popup TYPE TABLE OF ty_popup,
ls_popup LIKE LINE OF lt_popup,
lt_fcat_popup TYPE TABLE OF slis_fieldcat_alv,
ls_fcat_popup LIKE LINE OF lt_fcat_popup,
lt_excl TYPE slis_t_extab,
ls_excl TYPE slis_extab,
ls_file_table LIKE LINE OF ct_file_table,
l_tabix TYPE i.
ls_fcat_popup-tabname = 'LT_POPUP'.
ls_fcat_popup-col_pos = '1'.
ls_fcat_popup-fieldname = 'SELECT'.
ls_fcat_popup-seltext_m = 'SELECT'.
APPEND ls_fcat_popup TO lt_fcat_popup.
ls_fcat_popup-col_pos = '2'.
ls_fcat_popup-fieldname = 'TEXT'.
ls_fcat_popup-seltext_m = 'Status'.
ls_fcat_popup-outputlen = 50.
APPEND ls_fcat_popup TO lt_fcat_popup.
ls_excl-fcode = '&ETA'. APPEND ls_excl TO lt_excl.
ls_excl-fcode = '%SC'. APPEND ls_excl TO lt_excl.
ls_excl-fcode = '%SC+'. APPEND ls_excl TO lt_excl.
ls_excl-fcode = '&OUP'. APPEND ls_excl TO lt_excl.
ls_excl-fcode = '&ODN'. APPEND ls_excl TO lt_excl.
ls_excl-fcode = '&ILT'. APPEND ls_excl TO lt_excl.
ls_excl-fcode = '&OL0'. APPEND ls_excl TO lt_excl.
ls_excl-fcode = '&CRB'. APPEND ls_excl TO lt_excl.
ls_excl-fcode = '&CRL'. APPEND ls_excl TO lt_excl.
ls_excl-fcode = '&CRR'. APPEND ls_excl TO lt_excl.
ls_excl-fcode = '&CRE'. APPEND ls_excl TO lt_excl.
ls_excl-fcode = '&ALL'. APPEND ls_excl TO lt_excl.
ls_excl-fcode = '&SAL'. APPEND ls_excl TO lt_excl.
* Atualiza as Opções do Pop-up
LOOP AT ct_file_table INTO ls_file_table.
ls_popup-text = ls_file_table-filename.
APPEND ls_popup TO lt_popup.
ENDLOOP.
CALL FUNCTION 'REUSE_ALV_POPUP_TO_SELECT'
EXPORTING
i_title = 'Copiar os arquivos :'
i_zebra = abap_true
i_screen_start_column = 1
i_screen_start_line = 1
i_checkbox_fieldname = 'SELECT'
i_tabname = 'LT_POPUP'
it_fieldcat = lt_fcat_popup
it_excluding = lt_excl
TABLES
t_outtab = lt_popup
EXCEPTIONS
program_error = 1
OTHERS = 2.
IF sy-subrc IS INITIAL.
DELETE lt_popup WHERE select = space.
ENDIF.
* Valida as opções selecionadas
LOOP AT ct_file_table INTO ls_file_table.
l_tabix = sy-tabix.
READ TABLE lt_popup TRANSPORTING NO FIELDS
WITH KEY text = ls_file_table-filename.
IF sy-subrc IS NOT INITIAL.
DELETE ct_file_table INDEX l_tabix.
ENDIF.
ENDLOOP.
ENDMETHOD.
METHOD file_copy.
cl_gui_frontend_services=>file_copy(
EXPORTING
source = iv_source " Source
destination = iv_destination " Destination
overwrite = iv_overwrite " Overrides if Destination Exists
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
wrong_parameter = 3
disk_full = 4
access_denied = 5
file_not_found = 6
destination_exists = 7
unknown_error = 8
path_not_found = 9
disk_write_protect = 10
drive_not_ready = 11
not_supported_by_gui = 12
OTHERS = 13
).
ev_rc = sy-subrc.
ENDMETHOD.
METHOD file_exist.
cl_gui_frontend_services=>file_exist(
EXPORTING
file = iv_file " File to Check
RECEIVING
result = ev_result " Result
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
wrong_parameter = 3
not_supported_by_gui = 4
OTHERS = 5
).
ENDMETHOD.
METHOD popup_to_confirm.
CALL FUNCTION 'POPUP_TO_CONFIRM'
EXPORTING
titlebar = iv_title
text_question = iv_question
IMPORTING
answer = ev_answer
EXCEPTIONS
text_not_found = 1
OTHERS = 2.
ENDMETHOD.
METHOD file_get_attributes.
DATA :
l_readonly(1),
l_normal(1),
l_hidden(1),
l_archive(1).
cl_gui_frontend_services=>file_get_attributes(
EXPORTING
filename = iv_file " File Name
IMPORTING
readonly = l_readonly " Attribute: readonly
normal = l_normal " Attribute: Normal
hidden = l_hidden " Attribute: Hidden
archive = l_archive " Attribute: Archive
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
not_supported_by_gui = 3
wrong_parameter = 4
file_get_attributes_failed = 5
OTHERS = 6
).
CASE abap_true.
WHEN l_readonly.
ev_attributes = 'READONLY'.
WHEN l_normal.
ev_attributes = 'NORMAL'.
WHEN l_hidden.
ev_attributes = 'HIDDEN'.
WHEN l_archive.
ev_attributes = 'ARCHIVE'.
ENDCASE.
ENDMETHOD.
METHOD delete_folder.
BREAK-POINT.
me->directory_exist(
EXPORTING
iv_directory = folder && '\' && n_folder
CHANGING
cv_result = result
).
IF result IS NOT INITIAL.
WRITE : / `Pasta : ` && folder && '\' && n_folder && ` - Existe`.
me->directory_delete(
EXPORTING
iv_directory = folder && '\' && n_folder
CHANGING
cv_rc = subrc
).
IF sy-subrc IS INITIAL.
WRITE : / `Pasta : ` && folder
&& '\' && n_folder && ` - Deletada`.
ELSE.
WRITE : / `Pasta : ` && folder
&& '\' && n_folder && ` - Problema na deleção.`.
ENDIF.
ELSE.
WRITE : / `Pasta : ` && folder
&& '\' && n_folder && ` - Não existe`.
ENDIF.
ENDMETHOD.
METHOD delete_file.
DATA :
ls_file_table LIKE LINE OF ct_file_table.
BREAK-POINT.
LOOP AT ct_file_table INTO ls_file_table.
me->file_exist(
EXPORTING
iv_file = folder && '\' && n_folder
&& '\' && ls_file_table-filename
IMPORTING
ev_result = result
).
IF result IS NOT INITIAL.
me->file_delete(
EXPORTING
iv_file = folder && '\' && n_folder
&& '\' && ls_file_table-filename
IMPORTING
ev_rc = subrc
).
IF subrc IS INITIAL.
WRITE : / `Arquivo : ` && folder
&& '\' && n_folder && '\'
&& ls_file_table-filename && ` - Deletado`.
ELSE.
WRITE : / `Arquivo : ` && folder
&& '\' && n_folder && '\'
&& ls_file_table-filename && ` - Erro ao deletar`.
ENDIF.
ELSE.
WRITE : / `Arquivo : ` && folder
&& '\' && n_folder && '\'
&& ls_file_table-filename && ` - Não existe`.
ENDIF.
ENDLOOP.
ENDMETHOD.
METHOD file_delete.
cl_gui_frontend_services=>file_delete(
EXPORTING
filename = iv_file " Name of the file to be deleted
CHANGING
rc = ev_rc " Return Code
EXCEPTIONS
file_delete_failed = 1
cntl_error = 2
error_no_gui = 3
file_not_found = 4
access_denied = 5
unknown_error = 6
not_supported_by_gui = 7
wrong_parameter = 8
OTHERS = 9
).
ENDMETHOD.
ENDCLASS.
INITIALIZATION.
CREATE OBJECT o_main.
AT SELECTION-SCREEN OUTPUT.
* Caso não lembre.
* http://abapjuniores.blogspot.com.br/2013/10/programas-sem-objetos-de-textos.html
TRY .
%_folder_%_app_%-text = 'Destino da nova pasta :'.
%_n_folder_%_app_%-text = 'Nome da nova Pasta : '.
%_folder2_%_app_%-text = 'Pasta com os arquivos a copiar : '.
%_del_fold_%_app_%-text = '---> Pasta'.
%_del_file_%_app_%-text = '--->Arquivo'.
CATCH cx_root.
ENDTRY.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR folder.
o_main->directory_browse(
EXPORTING
iv_title = 'Diretório aonde será criado a pasta'
CHANGING
cv_folder = folder
).
AT SELECTION-SCREEN ON VALUE-REQUEST FOR folder2.
o_main->directory_browse(
EXPORTING
iv_title = 'Diretório de origem dos arquivos a serem copiados'
CHANGING
cv_folder = folder2
).
START-OF-SELECTION.
o_main->directory( ). " Cria a pasta
o_main->file( " Cria a copia dos arquivos
CHANGING
ct_file_table = gt_file_table
).
IF del_file IS NOT INITIAL.
o_main->delete_file( " Deletas os arquivos 'COPIA'
CHANGING
ct_file_table = gt_file_table
).
ENDIF.
IF del_fold IS NOT INITIAL.
o_main->delete_folder( ). " Deleta a pasta
ENDIF.