Create an abap program as shown below
Use the below code and schedule it background as per your requirement. *&-----------------------*&------------------------------------------------------------------------------------------------------------* ---* *& Report ZFTP_FILES *& *&------------------------*&------------------------------------------------------------------------------------------------------------* --* *& *& *&------------------------*&------------------------------------------------------------------------------------------------------------* --* REPORT zmdm_ftp_files. DATA: v_key TYPE i VALUE 26101957, v_compress(1) TYPE c VALUE 'N', v_hdl TYPE i, v_slen TYPE i. DATA: BEGIN OF itab_files OCCURS 0, line(100) TYPE c, END OF itab_files. DATA: BEGIN OF itab_files_det OCCURS 0, line(100) TYPE c, END OF itab_files_det.
DATA: itab_direc TYPE TABLE OF zmdm_ftp INITIAL SIZE 0, wa_direc TYPE zmdm_ftp. DATA: l_date TYPE char10, wa_docdata TYPE sodocchgi1, itab_objpack TYPE STANDARD TABLE OF sopcklsti1, itab_objtxt itab_objt xt TYPE STANDARD TABLE OF solisti1, solisti1 , itab_objbin1 itab_objb in1 TYPE STANDARD TABLE OF solisti1, wa_objbin1 TYPE solisti1, wa_objtxt TYPE solisti1, wa_objpack TYPE sopcklsti1, wa_reclist TYPE somlreci1, itab_reclist itab_recli st TYPE STANDARD TABLE OF somlreci1. DATA: v_pwd TYPE char30, v_len TYPE i, l_count TYPE i. CONSTANTS: CONSTANTS: c_ret
TYPE char1
VALUE cl_abap_char_utilities=>cr cl_abap_char_utilities=>cr_lf. _lf.
PARAMETERS: p_user TYPE char12 OBLIGATORY LOWER CASE, p_pwd TYPE char32 OBLIGATORY LOWER CASE, p_host(60) p_host(6 0) TYPE TYPE c OBLIGATORY LOWER CASE, * p_dir(125) p_dir(12 5) TYPE c OBLIGATORY LOWER CASE, p_dest TYPE rfcdes-rfcdest DEFAULT 'SAPFTPA' OBLIGATORY. SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001. PARAMETER: PARAMETER: p_email TYPE soobjinfi1-obj_name LOWER CASE. CASE. SELECTION-SCREEN END OF BLOCK b1. AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_email. *** F4 help for application server file PERFORM sub_mail_f4. START-OF-SELECTION. v_slen = STRLEN( p_pwd ). CALL FUNCTION 'HTTP_SCRAMBLE' EXPORTING SOURCE = p_pwd sourcelen = v_slen key = v_key IMPORTING destination = v_pwd. CALL FUNCTION 'FTP_CONNECT' EXPORTING user = p_user password = v_pwd host = p_host rfc_destination = p_dest IMPORTING handle = v_hdl EXCEPTIONS not_connected = 1
OTHERS = 2. IF sy-subrc <> 0. MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. ENDIF.
SELECT * FROM zmdm_ftp INTO TABLE itab_direc. LOOP AT itab_direc INTO wa_direc. DATA: v_directory(175) TYPE c, l_line TYPE string, itab_result TYPE TABLE OF text WITH HEADER LINE. *Command for directory path REFRESH itab_result. CLEAR l_count. DO 3 TIMES. l_count = l_count + 1. CLEAR v_directory. CASE l_count. WHEN 1. CONCATENATE CONCATENATE wa_direc-map '/Exception/ImportX'(002) '/Exception/ImportX'(002) INTO v_directory. WHEN 2. CONCATENATE CONCATENATE wa_direc-map '/Exception/StructuralX'(003) '/Exception/StructuralX'(003) INTO v_directory. WHEN 3. CONCATENATE CONCATENATE wa_direc-map '/Exception/ValueX'(004) '/Exception/ValueX'(004) INTO v_directory. ENDCASE. CONDENSE CONDENSE v_directory. CONCATENATE CONCATENATE 'cd'(005) v_directory INTO v_directory SEPARATED BY space. CALL FUNCTION 'FTP_COMMAND' EXPORTING handle = v_hdl command = v_directory compress = v_compress TABLES data = itab_result EXCEPTIONS tcpip_error = 1 command_error = 2 data_error = 3 OTHERS = 4. IF sy-subrc <> 0. CONTINUE. ENDIF. DATA: v_lines(3) TYPE c, v_command(80) TYPE c. FREE itab_files. *Get the file names on remote server CALL FUNCTION 'FTP_COMMAND' EXPORTING
handle = v_hdl command = 'nlist'(006) compress = v_compress TABLES data = itab_files EXCEPTIONS command_error = 1 tcpip_error = 2.
"N
IF sy-subrc = 0. DELETE itab_files FROM 1 TO 3. CLEAR v_lines. DESCRIBE TABLE itab_files LINES v_lines. DELETE itab_files INDEX v_lines. *
DESCRIBE DESCRIBE TABLE itab_files LINES v_lines. CLEAR v_lines. LOOP AT itab_files. *Get the Modification date and time for the TC05 files CONCATENATE CONCATENATE 'modtime'(007) itab_files-line INTO v_command SEPARATED BY space. REFRESH itab_files_det. CALL FUNCTION 'FTP_COMMAND' EXPORTING handle = v_hdl command = v_command compress = v_compress "N TABLES data = itab_files_det. * EXCEPTIONS * command_error = 1 * tcpip_error = 2. DELETE itab_files_det INDEX 1. LOOP AT itab_files_det. REPLACE itab_files-line itab_files-line IN itab_files_det-line itab_files_det-line WITH space. CONDENSE CONDENSE itab_files_det-line. itab_files_det-line. CLEAR l_date. CONCATENATE CONCATENATE itab_files_det-line+6(4) itab_files_det-line+6(4) itab_files_det-line+0(2) itab_files_det-line+0(2) itab_files_det-line+3(2) itab_files_det-line+3(2) INTO l_date. IF l_date = sy-datum. v_lines = v_lines + 1. ENDIF. ENDLOOP. ENDLOOP. CLEAR:v_len, wa_objbin1. v_len = STRLEN( v_directory ). IF v_lines IS NOT INITIAL. CONCATENATE CONCATENATE v_directory+3(v_len) wa_direc-bound v_lines INTO l_line SEPARATED BY cl_abap_char_utilities=>hor cl_abap_char_utilities=>horizontal_tab. izontal_tab. "' '. CONCATENATE CONCATENATE c_ret l_line INTO wa_objbin1-line. APPEND wa_objbin1 TO itab_objbin1. WRITE:/3 v_directory+3(v_len),
125 wa_direc-bound, 135 v_lines. ENDIF. ENDIF. * ENDIF. ENDDO. ENDLOOP. * Disconnect the connection CALL FUNCTION 'FTP_DISCONNECT' EXPORTING handle = v_hdl. *Close the connection CALL FUNCTION 'RFC_CONNECTION_CLOSE' EXPORTING destination = p_dest. * EXCEPTIONS * OTHERS = 1. IF itab_objbin1[] IS NOT INITIAL AND p_email IS NOT INITIAL. PERFORM sub_send_mail. ENDIF. *&------------------------*&------------------------------------------------------------------------------------------------------------* --* *& Form sub_mail_f4 *&------------------------*&------------------------------------------------------------------------------------------------------------* --* FORM sub_mail_f4 . DATA: itab_soxdl TYPE STANDARD TABLE OF soxdl. CALL FUNCTION 'SO_DLI_LIST_READ_XDL' EXPORTING private = 'X' public = 'X' subscript = 'X' TABLES dli_display_tab = itab_soxdl EXCEPTIONS communication_failure communication_failure = 1 dl_list_no_entries = 2 owner_not_exist =3 system_failure =4 x_error =5 parameter_error =6 OTHERS = 7. IF sy-subrc <> 0. MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. ENDIF. CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST' EXPORTING retfield = 'OBJNAM' dynpprog = sy-cprog
dynpnr = sy-dynnr dynprofield = 'P_EMAIL' value_org = 'S' TABLES value_tab = itab_soxdl[] EXCEPTIONS parameter_error = 1 no_values_found = 2 OTHERS = 3. IF sy-subrc <> 0. MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. ENDIF. ENDFORM.
" sub_mail_f4
*&------------------------*&------------------------------------------------------------------------------------------------------------* --* *& Form SUB_SEND_MAIL *&------------------------*&------------------------------------------------------------------------------------------------------------* --* FORM sub_send_mail . DATA: l_sdate TYPE char10. WRITE sy-datum TO l_sdate. CONCATENATE CONCATENATE 'Inbound and Outbound Exceptions for the date'(008) l_sdate INTO wa_objtxtline SEPARATED BY space. APPEND wa_objtxt TO itab_objtxt. CLEAR v_lines. * Describe the body of the message DESCRIBE TABLE itab_objtxt LINES v_lines. READ TABLE itab_objtxt INTO wa_objtxt INDEX v_lines. wa_docdata-doc_size = ( v_lines - 1 ) * 255 + STRLEN( wa_objtxt ). CLEAR wa_objpack-transf_bin. wa_objpack-head_start = 1. wa_objpack-head_num = 0. wa_objpack-body_start = 1. wa_objpack-body_num = v_lines. wa_objpack-doc_type = 'RAW'. APPEND wa_objpack TO itab_objpack. *Attachment 1 CLEAR v_lines. DESCRIBE DESCRIBE TABLE itab_objbin1 LINES v_lines. wa_objpack-doc_size = v_lines * 255. wa_objpack-transf_bin wa_objpack-transf_bi n = 'X'. wa_objpack-head_start = 1. wa_objpack-head_num = 1. wa_objpack-body_start = 1. wa_objpack-body_num = v_lines. wa_objpack-doc_type = 'XLS'. wa_objpack-obj_descr = 'Exception File'(009). APPEND wa_objpack TO itab_objpack. wa_docdata-obj_descr = 'FTP File Lists in Exception'(010).
wa_docdata-obj_name = 'FTP File Lists in Exception'(010). *Create the list of recipients wa_reclist-receiver wa_reclist-receiver = p_email. wa_reclist-rec_type wa_reclist-rec_type = 'C'. APPEND wa_reclist TO itab_reclist. *Send the e-mail CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1' EXPORTING document_data = wa_docdata put_in_outbox = 'X' commit_work = 'X' TABLES packing_list = itab_objpack contents_bin = itab_objbin1 contents_txt = itab_objtxt receivers = itab_reclist EXCEPTIONS too_many_receivers =1 document_not_sent =2 document_type_not_exist = 3 operation_no_authorization operation_no_authorizati on = 4 parameter_error =5 x_error =6 enqueue_error =7 OTHERS = 8. IF sy-subrc = 0. COMMIT WORK. SKIP 1. WRITE:/ 'Email Successfully sent to'(011), p_email, 'Distribution list'(013). ELSE. SKIP 1. WRITE:/ 'Email not sent to'(012), p_email, 'Distribution list'(013). ENDIF. ENDFORM.
" SUB_SEND_MAIL