How to Process Multiple Files from Application Server via ABAP Program
In this Blog Post we will understand how to process multiple files of certain File Masks from Application Server (AL11) via ABAP Program.
Lets try to understand it via following steps :
Input to our Executable Program will be following 3 parameters:
- File Mask for processing Files from Server (e.g. ABC* , files starting with ABC*)
- Directory path from Application Server where File are stored.
- Process Chain to execute that consists of steps for Processing this Files.
- In the next step, we will get all the Files from Application Server using Function “EPS2_GET_DIRECTORY_LISTING” and we will pass directory path and File mask we passed as an Input to this Function Module and the Function will return a table with the details related to the files fetched from Application Server.
CALL FUNCTION 'EPS2_GET_DIRECTORY_LISTING' EXPORTING iv_dir_name = lv_iv_dir_name file_mask = lv_file_mask TABLES dir_list = lt_file_tab EXCEPTIONS invalid_eps_subdir = 1 sapgparam_failed = 2 build_directory_failed = 3 no_authorization = 4 read_directory_failed = 5 too_many_read_errors = 6 empty_directory_list = 7 OTHERS = 8. IF sy-subrc = 0. "sorting by name to get the oldest file SORT lt_file_tab BY name. ENDIF.
2. As a next step, we wanted the count of Files fetched from server with same File Mask we passed, to achieve this we will make use of Internal table where we have passed data related to Files fetched from Server.
*Code to get count of Files DESCRIBE TABLE lt_file_tab LINES files_count.
3. Now since we have the count of files fetched for processing, we will execute a DO Loop based on the the count of files fetched and Call Function “RSPC_API_CHAIN_START” for executing process chain we passed as a parameter to the program and also which contains the steps that Processes the Files fetched.
*Code to loop through Files Count and Executing Process chain correspondingly IF files_count >= 1. DO files_count TIMES. CALL FUNCTION 'RSPC API CHAIN START' EXPORTING i_chain = p_pchain IMPORTING e_loqid = lv_logid.
4. Once process chain is executed in above step within same DO loop execution , we will check the status of Process chain before beginning the next iteration of DO Loop by calling Function “RSPC_API_CHAIN_GET_STATUS” and passing process chain name and log id of the Process chain run which we will get as a output from previous function call of “RSPC_API_CHAIN_START“.
IF sy-subrc <> 0. ELSE. DO. WAIT UP TO 2 SECONDS. CALL FUNCTION ‘RSPC_API_CHAIN_GET_STATUS‘ EXPORTING i_chain = p_pchain i_logid = lv_logid IMPORTING e_status = lv_status. IF lv_status = 'G' OR lv_status = 'R' EXIT. ENDIF. ENDDO. IF lv_status = ‘R'. MESSAGE ‘File has errors' TYPE "E” EXIT. ENDIF. ENDIF. ENDDO.
5. Once, we get status of Process chain from previous function call of “RSPC_API_CHAIN_GET_STATUS” if its Green or Red we will exit that particular Iteration of DO Loop to process next file ( In this particular scenario , Process chain has step to move the file to different folders based on the execution status of DTP)
By following the Steps above, we can process multiple files from Application server with same file masks by harnessing the use of Standard Functions from SAP.
— Source of the Code I have taken from my SAP system and no code is taken from the internet.
Hope the above Information added a bit of value to your time and knowledge .
If you enjoyed reading it do give it a Like and would be happy to read your feedback in the comments section.