ABAP - Working With Files
ABAP - Working With Files
ABAP - Working With Files
There are also a number of other function modules which could be used for uploading/downloding files between SAP and the presentation server. * Retrieve data file from presentation server(Upload from PC) DATA: i_file like rlgrap-filename value '/usr/sap/tmp/file.txt'. DATA: begin of it_datatab occurs 0, row(500) type c, end of it_datatab. CALL FUNCTION 'GUI_UPLOAD' EXPORTING filename = i_file filetype = 'ASC' TABLES data_tab = it_datatab "ITBL_IN_RECORD[] EXCEPTIONS file_open_error = 1 OTHERS = 2.
* Retrieve Data file from Application server(Upload from Unix) DATA: i_file like rlgrap-filename value '/usr/sap/tmp/file.txt'. OPEN DATASET i_file FOR INPUT IN TEXT MODE. IF sy-subrc NE 0. MESSAGE e999(za) WITH 'Error opening file' i_file. ENDIF. DO. * Reads each line of file individually READ DATASET i_file INTO wa_datatab. * Perform processing here * ..... ENDDO.
* Download internal table to Application server file(Unix) DATA: e_file like rlgrap-filename value '/usr/sap/tmp/file.txt'. open dataset e_file for output in text mode. lOOP AT it_datatab...... transfer it_datatab to e_file. ENDLOOP. close dataset e_file.
* File download, uses older techniques but achieves a perfectly * acceptable solution which also allows the user to append data to * an existing file. PARAMETERS: p_file like rlgrap-filename. * Internal table to store export data DATA: begin of it_excelfile occurs 0, row(500) type c, end of it_excelfile. DATA: rc TYPE sy-ucomm, ld_answer TYPE c. CALL FUNCTION 'WS_QUERY' EXPORTING query = 'FE' "File Exist? filename = p_file IMPORTING
return = rc. IF rc NE 0. "If File alread exists CALL FUNCTION 'POPUP_TO_CONFIRM' EXPORTING * TITLEBAR ='' * DIAGNOSE_OBJECT ='' text_question = 'File Already exists!!' text_button_1 = 'Replace' * ICON_BUTTON_1 ='' text_button_2 = 'New name' * ICON_BUTTON_2 ='' * DEFAULT_BUTTON = '1' * DISPLAY_CANCEL_BUTTON = 'X' * USERDEFINED_F1_HELP = ' ' * START_COLUMN = 25 * START_ROW =6 * POPUP_TYPE = IMPORTING answer = ld_answer * TABLES * PARAMETER = EXCEPTIONS text_not_found =1 OTHERS = 2. * Option 1: Overwrite ********************* IF ld_answer EQ '1'. CALL FUNCTION 'GUI_DOWNLOAD' EXPORTING * BIN_FILESIZE = filename = p_file "File Name filetype = 'ASC' * IMPORTING * FILELENGTH = TABLES data_tab = it_excelfile "Data table EXCEPTIONS file_write_error =1 no_batch =2 gui_refuse_filetransfer = 3 invalid_type =4 OTHERS = 5. IF sy-subrc <> 0. MESSAGE i003(zp) WITH 'There was an error during Excel file creation'(200). exit. "Causes short dump if removed and excel document was open ENDIF. * Option 2: New name. ********************* ELSEIF ld_answer EQ '2'. CALL FUNCTION 'DOWNLOAD' EXPORTING filename = p_file "File name filetype = 'ASC' "File type * col_select = 'X' "COL_SELECT * col_selectmask = 'XXXXXXXXXXXXXXXXXXXXXXXXXX' * "COL_SELECTMASK filetype_no_show = 'X' "Show file type selection? * IMPORTING * act_filename = filename_dat TABLES data_tab = it_excelfile "Data table * fieldnames = EXCEPTIONS
file_open_error = 01 file_write_error = 02 invalid_filesize = 03 invalid_table_width = 04 invalid_type = 05 no_batch = 06 unknown_error = 07. ENDIF. ELSE. "File does not alread exist. CALL FUNCTION 'GUI_DOWNLOAD' EXPORTING * BIN_FILESIZE = filename = p_file "File name filetype = 'ASC' "File type * IMPORTING * FILELENGTH = TABLES data_tab = it_excelfile "Data table EXCEPTIONS file_write_error =1 no_batch =2 gui_refuse_filetransfer = 3 invalid_type =4 OTHERS = 5. IF sy-subrc <> 0. MESSAGE i003(zp) WITH 'There was an error during Excel file creation'(200). exit. "Causes short dump if removed and excel document was open ENDIF. ENDIF.
ENDIF. *********************************************************************** * END-OF-SELECTION. END-OF-SELECTION. LOOP AT it_datatab INTO wa_datatab. WRITE:/ wa_datatab-col1, wa_datatab-col2, wa_datatab-col3. ENDLOOP.
Upload Tab delimited file from application server into internal table
ABAP code for uploading a TAB delimited file into an internal table. See code below for structures. The code is base on uploading a simple txt file. *&---------------------------------------------------------------------* *& Report ZUPLOADTAB *& * *&---------------------------------------------------------------------* *& Example of Uploading tab delimited file *& * *&---------------------------------------------------------------------* REPORT zuploadtab . PARAMETERS: p_infile LIKE rlgrap-filename OBLIGATORY DEFAULT '/usr/sap/'.. DATA: ld_file LIKE rlgrap-filename. *Internal tabe to store upload data TYPES: BEGIN OF t_record, name1 like pa0002-VORNA, name2 like pa0002-name2, age type i, END OF t_record. DATA: it_record TYPE STANDARD TABLE OF t_record INITIAL SIZE 0, wa_record TYPE t_record. *Text version of data table TYPES: begin of t_uploadtxt, name1(10) type c, name2(15) type c, age(5) type c, end of t_uploadtxt. DATA: wa_uploadtxt TYPE t_uploadtxt. *String value to data in initially. DATA: wa_string(255) type c. constants: con_tab TYPE x VALUE '09'. *If you have Unicode check active in program attributes then you will *need to declare constants as follows: *class cl_abap_char_utilities definition load. *constants: * con_tab type c value cl_abap_char_utilities=>HORIZONTAL_TAB.
* *
************************************************************************ *START-OF-SELECTION START-OF-SELECTION. ld_file = p_infile. OPEN DATASET ld_file FOR INPUT IN TEXT MODE ENCODING DEFAULT. IF sy-subrc NE 0. ELSE. DO. CLEAR: wa_string, wa_uploadtxt. READ DATASET ld_file INTO wa_string. IF sy-subrc NE 0. EXIT. ELSE.
SPLIT wa_string AT con_tab INTO wa_uploadtxt-name1 wa_uploadtxt-name2 wa_uploadtxt-age. MOVE-CORRESPONDING wa_uploadtxt TO wa_upload. APPEND wa_upload TO it_record. ENDIF. ENDDO. CLOSE DATASET ld_file. ENDIF. ************************************************************************ *END-OF-SELECTION END-OF-SELECTION. *!! Text data is now contained within the internal table IT_RECORD * Display report data for illustration purposes loop at it_record into wa_record. write:/ sy-vline, (10) wa_record-name1, sy-vline, (10) wa_record-name2, sy-vline, (10) wa_record-age, sy-vline. endloop.
* *
************************************************************************ *AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_INFILE. AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_infile. CALL FUNCTION 'WS_FILENAME_GET' EXPORTING def_filename = p_infile
mask = ',*.txt.' mode = 'O' title = 'Upload File'(078) IMPORTING filename = p_infile EXCEPTIONS inv_winsys =1 no_batch =2 selection_cancel = 3 selection_error = 4 OTHERS = 5. ************************************************************************ *START-OF-SELECTION START-OF-SELECTION. gd_file = p_infile. CALL FUNCTION 'GUI_UPLOAD' EXPORTING filename = gd_file has_field_separator = 'X' "file is TAB delimited TABLES data_tab = it_record EXCEPTIONS file_open_error =1 file_read_error =2 no_batch =3 gui_refuse_filetransfer = 4 invalid_type =5 no_authority =6 unknown_error =7 bad_data_format =8 header_not_allowed =9 separator_not_allowed = 10 header_too_long = 11 unknown_dp_error = 12 access_denied = 13 dp_out_of_memory = 14 disk_full = 15 dp_timeout = 16 OTHERS = 17. IF sy-subrc NE 0. write: 'Error ', sy-subrc, 'returned from GUI_UPLOAD FM'. skip. endif. * * * * * * * * * * * * * * * * * * * Alternative method, where by you split fields at each TAB after you have returned the data. No point unless you dont have access to GUI_UPLOAD but just included for information CALL FUNCTION 'GUI_UPLOAD' EXPORTING filename = gd_file filetype = 'ASC' TABLES data_tab = it_datatab "ITBL_IN_RECORD[] EXCEPTIONS file_open_error = 1 OTHERS = 2. IF sy-subrc NE 0. ELSE. LOOP AT it_datatab. CLEAR: wa_string, wa_uploadtxt. wa_string = it_datatab. SPLIT wa_string AT con_tab INTO wa_uploadtxt-name1
* wa_uploadtxt-name2 * wa_uploadtxt-age. * MOVE-CORRESPONDING wa_uploadtxt TO wa_record. * APPEND wa_record TO it_record. * ENDLOOP. * ENDIF. ************************************************************************ *END-OF-SELECTION END-OF-SELECTION. *!! Text data is now contained within the internal table IT_RECORD * Display report data for illustration purposes LOOP AT it_record INTO wa_record. WRITE:/ sy-vline, (10) wa_record-name1, sy-vline, (10) wa_record-name2, sy-vline, (10) wa_record-age, sy-vline. ENDLOOP.
*Selecting a Directory data: gd_path(500) type c. parameters: p_path like rlgrap-filename. at selection-screen on value-request for p_path. CALL METHOD cl_gui_frontend_services=>directory_browse EXPORTING window_title = 'File Directory' initial_folder = 'C:' CHANGING selected_folder = gd_path. CALL METHOD cl_gui_cfw=>flush. concatenate gd_path '' into p_path.
*Selecting a File, plus inserting default file extension tables rlgrap. data: it_tab type filetable, gd_subrc type i. selection-screen begin of block m with frame. select-options so_fpath for rlgrap-filename. selection-screen end of block m. at selection-screen on value-request for so_fpath-low. REFRESH: it_tab. CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_OPEN_DIALOG EXPORTING WINDOW_TITLE = 'Select File' DEFAULT_FILENAME = '*.txt' MULTISELECTION = 'X' CHANGING FILE_TABLE = it_tab RC = gd_subrc. loop at it_tab into so_fpath-low. so_fpath-sign = 'I'. so_fpath-option = 'EQ'. append so_fpath. endloop.
*Selecting a file to save too, plus inserting default file extension .xls tables rlgrap. DATA: ld_filename TYPE string, ld_path TYPE string, ld_fullpath TYPE string, ld_result TYPE i, gd_file TYPE c. selection-screen begin of block m with frame. PARAMETERS: p_file TYPE rlgrap-filename. selection-screen end of block m. * At selection screen AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file. * Display save dialog window CALL METHOD cl_gui_frontend_services=>file_save_dialog EXPORTING * window_title ='' default_extension = 'XLS' default_file_name = 'accountsdata' initial_directory = 'c:\temp\' CHANGING filename = ld_filename path = ld_path fullpath = ld_fullpath user_action = ld_result. p_file = ld_fullpath.
*TOP-OF-PAGE. TOP-OF-PAGE. PERFORM WRITE_PAGE_HEADING. ********************************************************************* *INITIALIZATION. INITIALIZATION. PARCOM = '/usr/sap/*'. "Only default value ********************************************************************* *START-OF-SELECTION. START-OF-SELECTION. PERFORM MAKE_LOC. PERFORM COLLECT_DATA. PERFORM FILE_LIST. ********************************************************************* *AT LINE-SELECTION. AT LINE-SELECTION. CLEAR FILE. FILE = SY-LISEL+55. REPLACE '|' WITH SPACE INTO FILE. IF SY-LISEL+1(1) = 'd'. PATH = FILE. PATH+200 = '/*'. CONDENSE PATH NO-GAPS. SUBMIT ZUNIXFILES WITH PARCOM = PATH AND RETURN. ENDIF. SUBMIT ZBROWSEFILE WITH PARCOM = FILE AND RETURN. "Call the browse program
*---------------------------------------------------------------------* * FORM MAKE_LOC. * *---------------------------------------------------------------------* FORM MAKE_LOC. CLEAR: PARCOM_LOC. PARCOM_LOC(7) = 'ls -ld '. PARCOM_LOC+7(45) = PARCOM. ENDFORM. *---------------------------------------------------------------------* * FORM COLLECT_DATA. *---------------------------------------------------------------------* FORM COLLECT_DATA. REFRESH TABL. CALL 'SYSTEM' ID 'COMMAND' FIELD PARCOM_LOC ID 'TAB' FIELD TABL-*SYS*. ENDFORM. *-------------------------------------------------------------------* * FORM FILE_LIST. * *-------------------------------------------------------------------* FORM FILE_LIST. FORMAT RESET. CLEAR SUM. LOOP AT TABL. CLEAR LINE. *
LINE = TABL-LINE. WRITE /2 LINE. PERFORM VERTICAL_LINES. SUM = SUM + SIZE. ENDLOOP. WRITE:/1(132) SY-ULINE. ENDFORM. *-------------------------------------------------------------------* * FORM WRITE_PAGE_HEADING. *-------------------------------------------------------------------* FORM WRITE_PAGE_HEADING. WRITE:/1(132) SY-ULINE. PERFORM VERTICAL_LINES. FORMAT INTENSIFIED. WRITE: /2(131) SPACE COLOR 1. WRITE: 2 SY-DATUM COLOR 1. WRITE: 35 'File Manager' COLOR 1. PERFORM VERTICAL_LINES. WRITE:/1(132) SY-ULINE. PERFORM VERTICAL_LINES. FORMAT INTENSIFIED. WRITE: /2(131) SPACE COLOR 3. PERFORM VERTICAL_LINES. FORMAT RESET. WRITE:/1(132) SY-ULINE. ENDFORM. *-------------------------------------------------------------------* * FORM VERTICAL_LINES. *-------------------------------------------------------------------* FORM VERTICAL_LINES. PERFORM WRITE_VLINE USING ' ' 1. PERFORM WRITE_VLINE USING ' ' 132. ENDFORM. *-------------------------------------------------------------------* * WRITE_VLINE. * *-------------------------------------------------------------------* FORM WRITE_VLINE USING MODE POS. IF MODE NE 'T'. FORMAT INTENSIFIED. ENDIF. POSITION POS. WRITE: SY-VLINE. IF MODE NE 'T'. FORMAT RESET. ENDIF. ENDFORM. * *