REPORT z_move_files_between_unix_dir NO STANDARD PAGE HEADING MESSAGE-ID ztest. *----------------------------------------------------------------------* * Program Description: * * This program moves all files from source UNIX directory to target * UNIX directory. *----------------------------------------------------------------------* * SELECTION SCREEN-----------------------------------------------------* SELECTION-SCREEN: BEGIN OF BLOCK sel_blk WITH FRAME TITLE text-001. PARAMETERS : p_srcdir LIKE temfile-dirname LOWER CASE OBLIGATORY DEFAULT '/usr/sap/', " INSTEAD OF '/usr/sap/rdi_output/COLLAUDO/ARCHIVE/' "Source Directory p_tgtdir LIKE temfile-dirname LOWER CASE OBLIGATORY DEFAULT '/usr/sap/'. " INSTEAD OF '/usr/sap/rdi_output/COLLAUDO/RDI/' "Destination Directory SELECTION-SCREEN: END OF BLOCK sel_blk. ************************************************************************ * Data Declarations ************************************************************************ *Internal table for File names DATA BEGIN OF t_xy_filetab OCCURS 0. INCLUDE STRUCTURE emg_fileinfo. DATA END OF t_xy_filetab. DATA t_move LIKE btcxpm OCCURS 0 WITH HEADER LINE. DATA: ws_file_path(256) TYPE c, "file path ws_lines TYPE i, ws_zstat LIKE extcmdexex-status, "status returned by "sxpg_command_execute ws_zexit LIKE extcmdexex-exitcode, "exit code returned by "sxpg_command_execue ws_comm_unix LIKE sxpgcolist-parameters, "unix command to "sxpg_command_execute ws_directory LIKE btch0000-text80, ws_length TYPE i. AT SELECTION-SCREEN ON p_srcdir. * Validate source directory PERFORM validate_directory USING p_srcdir. AT SELECTION-SCREEN ON p_tgtdir. * Validate target directory PERFORM validate_directory USING p_tgtdir. * START OF SELECTION---------------------------------------------------* START-OF-SELECTION. REFRESH t_xy_filetab[]. * Get all the file names from source directory CALL FUNCTION 'ISU_M_GET_FILES_OF_DIR' EXPORTING x_path = p_srcdir TABLES xy_filetab = t_xy_filetab EXCEPTIONS no_start = 1 no_next = 2 no_finish = 3 general_error = 4 OTHERS = 5. IF sy-subrc <> 0. MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. EXIT. ENDIF. CLEAR ws_lines. DESCRIBE TABLE t_xy_filetab LINES ws_lines. IF ws_lines <= 0. MESSAGE i888 WITH 'No files found in directory' p_srcdir. EXIT. ENDIF. * Place '/' at the end of source directory if not present CLEAR ws_length. ws_length = strlen( p_srcdir ). ws_length = ws_length - 1. IF p_srcdir+ws_length NE '/'. CONCATENATE p_srcdir '/' INTO p_srcdir. ENDIF. * Move all the files to target directory LOOP AT t_xy_filetab. CLEAR: ws_file_path, ws_comm_unix, ws_zstat, ws_zexit. REFRESH t_move[]. * Construct source file path CONCATENATE p_srcdir t_xy_filetab-name INTO ws_file_path. * Forming the command as per unix syntax to perform move in unix CONCATENATE ws_file_path p_tgtdir INTO ws_comm_unix SEPARATED BY space. * Call function to execute 'ZMV' command defined in SM69 CALL FUNCTION 'SXPG_COMMAND_EXECUTE' EXPORTING commandname = 'ZMV' additional_parameters = ws_comm_unix operatingsystem = sy-opsys stdout = 'X' stderr = 'X' terminationwait = 'X' IMPORTING status = ws_zstat exitcode = ws_zexit TABLES exec_protocol = t_move EXCEPTIONS no_permission = 1 command_not_found = 2 parameters_too_long = 3 security_risk = 4 wrong_check_call_interface = 5 program_start_error = 6 program_termination_error = 7 x_error = 8 parameter_expected = 9 too_many_parameters = 10 illegal_command = 11 wrong_asynchronous_parameters = 12 cant_enq_tbtco_entry = 13 jobcount_generation_error = 14 OTHERS = 15. IF sy-subrc <> 0 OR ws_zexit <> 0. MESSAGE ID sy-msgid TYPE 'I' NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. ENDIF. ENDLOOP. END-OF-SELECTION. FREE: t_move[], t_xy_filetab[]. *---------------------------------------------------------------------* * FORM validate_directory * *---------------------------------------------------------------------* * ........ * *---------------------------------------------------------------------* * --> P_DIR * *---------------------------------------------------------------------* FORM validate_directory USING p_dir. ws_directory = p_dir. CALL FUNCTION 'PFL_CHECK_DIRECTORY' EXPORTING directory = ws_directory EXCEPTIONS pfl_dir_not_exist = 1 pfl_permission_denied = 2 pfl_cant_build_dataset_name = 3 pfl_file_not_exist = 4 OTHERS = 5. IF sy-subrc <> 0. MESSAGE e888 WITH 'Invalid directory' ws_directory. ENDIF. ENDFORM.
1) RZL_READ_DIR_LOCAL if you need simple list of filenames.
2) EPS_GET_DIRECTORY_LISTING is more powerfull - it can also list subdirectories.
Get list and move
Check whether one file exists on App server :
Others :
http://wiki.sdn.sap.com/wiki/display/Snippets/Working+With+Files
http://blog.chinaunix.net/space.php?uid=20692741&do=blog&cuid=1300664