How to write a program to move files in application server to an archive folder
Many times after loading the file from the application server to BW targets, we need to move it in a separate folder such as archive folder to avoid the same file being repeatedly processed. Also the archive folder will serve as a storage for history of all the files that were already loaded.This helps in referencing them at a later point in time if required.
This archive program can be added as the last step in the process chain created for the purpose of uploading the file. This needs a variant to be created which can be used while calling the program.
Creation of file archival program can be done in the tcode SE38.
Please note: Here in some cases it may happen that only selective files need to be archived from the main folder. In that case, create a variant for the program and use the variant value in the case statement in the below program.
For example: if variant is “sales”, delete files whose name does not contain “sales”. Similarly if variant value is “finance”, delete files whose name does not contain “finance” and so on.
Sample archive program:
Here DEV, QA and PROD are taken as dummy source system ids for development, test and production BW systems.
REPORT zsample_archive. " ------------------------------------------------------" *------Description--------------------------------------* *This program create archive file in archive directory * *for the selection, and delete the respective file from * *the orginal directory *-------------------------------------------------------* *AUTHOR : AK Verma *Date : 16 FEB 2017 *---------Changes---------------------------------------* * 1) Done by: * 2) Date: * 3) Change description: *-------------------------------------------------------* *Declaration TYPES: BEGIN OF itab1 , rec(1000) TYPE c, END OF itab1. DATA : itab TYPE TABLE OF itab1, wa TYPE itab1. DATA : f_fname TYPE localfile . " Final file name. DATA: p_file TYPE localfile. " To capture all file name DATA: p_tfile type localfile. DATA: ifile TYPE TABLE OF salfldir WITH HEADER LINE. DATA: p_path TYPE salfile-longname. PARAMETERS: P_VAR TYPE C LENGTH 6. IF sy-sysid EQ 'DEV'. p_path = '/usr/sap/DEV/Reports'. ELSEIF sy-sysid EQ 'QA'. p_path = '/usr/sap/QA/Reports'. ELSEIF sy-sysid EQ 'PROD'. p_path = '/usr/sap/PROD/Reports'. ENDIF. * To get all file in application file folder CALL FUNCTION 'RZL_READ_DIR_LOCAL' EXPORTING name = p_path TABLES file_tbl = ifile EXCEPTIONS argument_error = 1 not_found = 2 OTHERS = 3. * Ifile contains all file names in application folder LOOP AT ifile. IF ifile-name CS '.csv'. SKIP. ELSEIF ifile-name CS '.xml'. SKIP. ELSE. DELETE ifile. ENDIF. ENDLOOP. *"Delete other files from the list CASE P_VAR. WHEN 'SALES'. DELETE ifile WHERE name NS 'SALES'. DELETE ifile WHERE name CS 'FINANCE'. DELETE ifile WHERE name CS 'HR'. WHEN 'FINANCE'. DELETE ifile WHERE name NS 'FINANCE'. DELETE ifile WHERE name CS 'SALES'. DELETE ifile WHERE name CS 'HR'. WHEN 'HR'. DELETE ifile WHERE name NS 'HR'. DELETE ifile WHERE name CS 'FINANCE'. DELETE ifile WHERE name CS 'SALES'. : : WHEN OTHERS. NOTHING ENDCASE. " create archive file LOOP AT ifile. CONCATENATE p_path '/' ifile-name INTO p_file. CONCATENATE p_path '/' 'archive' '/' ifile-name INTO p_tfile. CLEAR: wa, itab[]. OPEN DATASET p_file FOR INPUT IN LEGACY TEXT MODE." ENCODING DEFAULT. IF sy-subrc = 0. DO. READ DATASET p_file INTO wa. IF sy-subrc <> 0. EXIT. ENDIF. IF sy-index = 1. CONTINUE. ENDIF. wa-rec = wa. APPEND wa TO itab. ENDDO. ELSE. MESSAGE 'Error in opening the file' TYPE 'E'. EXIT. ENDIF. CLOSE DATASET p_file. IF sy-subrc NE 0. MESSAGE 'Error closing the File' TYPE 'E'. EXIT. ELSEIF sy-subrc IS INITIAL. WRITE : 'File saved in the location' , p_file. ENDIF. ***create archive file here CHECK itab[] IS NOT INITIAL. OPEN DATASET p_tfile FOR OUTPUT IN LEGACY TEXT MODE." ENCODING DEFAULT. IF sy-subrc = 0. LOOP AT itab INTO wa . IF sy-subrc = 0. TRANSFER wa-rec TO p_tfile. ENDIF. ENDLOOP. ELSE. MESSAGE 'Error in opening the file' TYPE 'E'. EXIT. ENDIF. CLOSE DATASET f_fname. IF sy-subrc NE 0. MESSAGE 'Error closing the File' TYPE 'E'. EXIT. ELSEIF sy-subrc IS INITIAL. WRITE : 'File saved in the location' , p_tfile. ENDIF. *** delete source file now OPEN DATASET p_file FOR INPUT IN LEGACY TEXT MODE." ENCODING DEFAULT. IF sy-subrc = 0. DO. DELETE DATASET p_file. IF sy-subrc <> 0. EXIT. ENDIF. ENDDO. ELSE. MESSAGE 'Error in opening the file ' TYPE 'E'. EXIT. ENDIF. CLOSE DATASET p_file. IF sy-subrc NE 0. MESSAGE 'Error closing the File' TYPE 'E'. EXIT. ELSEIF sy-subrc IS INITIAL. WRITE : 'File Deleted from the location' , p_file. ENDIF. ENDLOOP. **---------------------------------------------<
Once the archive program is ready, check, save and activate the program.
This program can now be included in the process chain with correct variant.
Variants can be created in SE38 itself.
For example: Sales PC will have the archive program as the last step with variant as “SALES”