Many times, we need to move an input file from one folder (say Input folder) to another folder (say Processed folder) after processing it. In such scenario in ABAP, we generally loop and read through the input file and transfer the input file data to the destination folder.
If the input file has large volume, then this process of moving the file from one folder to another by looping is time consuming and hits the performance of the system.
Another alternative is to create an External Operating System Command in SAP Transaction SM69 and use it in the program by calling the SAP standard FM ‘SXPG_COMMAND_EXECUTE’.
ABAPers usually do not have authorization to this Transaction SM69 and they need to depend on Basis/Security to get a new command created. Basis/Security in turn needs to have all the approvals in place to create these commands. Sometime due to audit issue, the external command cannot be created.
The smartest solution is to generate the UNIX Command in SAP ABAP programmatically and copy the input file instantly into the destination path.
Sample code to write UNIX command in SAP ABAP
* This is a code showing how to create and use COPY command of UNIX in ABAP
* Input file path
p_input TYPE localfile,
* Processed file path
p_proc TYPE localfile.
* Declare the Types to file data
TYPES: BEGIN OF L_X_OUTPUT,
*sys*(200), ” Please note, there are asterisk before and after sys (i.e.*sys*)
END OF L_X_OUTPUT.
* * Internal table to store file data
DATA l_i_output TYPE STANDARD TABLE OF l_x_output WITH HEADER LINE.
* * Variable for the UNIX command
DATA: l_v_unix_comm(255) TYPE c.
* Copy command of UNIX
CONCATENATE ‘cp’ p_input p_proc
INTO l_v_unix_comm SEPARATED BY space.
* For example the Copy command is stored as below
* cp ‘/data/interfaces/input/input_file’ ‘/data/interfaces/processed/processed_file’
* Examples of UNIX Command *• mv filename1 filename2 — moves a file (i.e. gives it a different name, or moves it into a *different directory (see below) *• cp filename1 filename2 — copies a file
* Execute the UNIX Copy command.
* This command will copy the file from input file path to the processed file path
CALL ‘SYSTEM’ ID ‘COMMAND’ FIELD l_v_unix_comm
IF sy-subrc eq 0.
write: ‘File is copied successfully using UNIX command in ABAP’.
Step1: Execute the above program
Benefit of this Tip
1) No need to loop through the file again to copy it to another path.
2) No need to depend on BASIS/Security team to get a new External Operating System Commands created in T Code SM69 and no need to call the standard FM to use that Command.
3) We can write all the commands which are recognized by UNIX.
Image source : www.netcmdlets.com