SAP ABAP - Get All Files From FTP Server
ABAP Code
*&---------------------------------------------------------------------*
*& Report YFTP_PUT_FILE
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT yftp_get_file.
TYPES :
BEGIN OF ty_podata,
ebeln TYPE ebeln,
spr1(1) TYPE c, "cl_abap_char_utilities=>horizontal_tab,
ebelp TYPE ebelp,
spr2(1) TYPE c,
matnr TYPE matnr,
spr3(1) TYPE c,
txz01 TYPE ekpo-txz01,
END OF ty_podata.
TYPES: BEGIN OF text,
line(120) TYPE c,
END OF text.
DATA :
lt_podata TYPE TABLE OF ty_podata,
ls_podata TYPE ty_podata,
result TYPE TABLE OF text WITH HEADER LINE.
DATA: key TYPE i VALUE 26101957,
hdl TYPE i,
slen TYPE i,
x TYPE i,
docid LIKE sysuuid-c,
fname TYPE char30,
cmd(120),
error,
tab TYPE c VALUE cl_abap_char_utilities=>horizontal_tab,
* bTEXT(120) type x,
bline(120) TYPE x.
DATA: BEGIN OF mtab_data OCCURS 0,
line(132) TYPE c,
END OF mtab_data.
DATA: wa_data LIKE mtab_data.
PARAMETERS: user(30) TYPE c LOWER CASE,
pwd(30) TYPE c LOWER CASE,
host(64) TYPE c LOWER CASE,
pasv.
SELECTION-SCREEN SKIP 1.
PARAMETERS:
p_ebeln TYPE ebeln,
dest TYPE rfcdes-rfcdest DEFAULT 'SAPFTPA'.
AT SELECTION-SCREEN OUTPUT.
LOOP AT SCREEN.
IF screen-name = 'PWD'.
screen-invisible = '1'.
MODIFY SCREEN.
ENDIF.
ENDLOOP.
START-OF-SELECTION.
PERFORM ftp_open CHANGING hdl.
PERFORM ftp_command USING hdl:
* 'cd' '' CHANGING mtab_data[],
'nlist' '' CHANGING mtab_data[].
PERFORM extract_text_files.
PERFORM ftp_close.
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
FORM ftp_open CHANGING pv_handl.
slen = strlen( pwd ).
CALL FUNCTION 'HTTP_SCRAMBLE'
EXPORTING
source = pwd
sourcelen = slen
key = key
IMPORTING
destination = pwd.
CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
EXPORTING
text = 'Connect to FTP Server'.
CLEAR: pv_handl.
CALL FUNCTION 'FTP_CONNECT'
EXPORTING
user = user
password = pwd
host = host
rfc_destination = dest
IMPORTING
handle = pv_handl.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
IF NOT pasv IS INITIAL.
REFRESH result.
CALL FUNCTION 'FTP_COMMAND'
EXPORTING
handle = pv_handl
command = 'set passive on'
TABLES
data = result
EXCEPTIONS
tcpip_error = 1
command_error = 2
data_error = 3.
IF sy-subrc EQ 0.
WRITE: / 'Set passive mode'.
SKIP 1.
ENDIF.
ENDIF.
ENDFORM.
FORM ftp_command USING
pv_handl TYPE i
pv_comnd TYPE char120
pv_optns TYPE char120
CHANGING pt_data TYPE table.
DATA:
lv_command TYPE char120.
FREE: pt_data.
CONCATENATE pv_comnd pv_optns INTO lv_command
SEPARATED BY space.
CALL FUNCTION 'FTP_COMMAND'
EXPORTING
handle = pv_handl
command = lv_command
TABLES
data = pt_data
EXCEPTIONS
tcpip_error = 1
command_error = 2
data_error = 3
OTHERS = 4.
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. " ftp_command
FORM ftp_close.
CALL FUNCTION 'FTP_DISCONNECT'
EXPORTING
handle = hdl.
CALL FUNCTION 'RFC_CONNECTION_CLOSE'
EXPORTING
destination = dest
EXCEPTIONS
OTHERS = 1.
ENDFORM.
FORM extract_text_files.
DATA: BEGIN OF itab_tmp OCCURS 1,
line(600) ,
END OF itab_tmp .
CLEAR fname.
REFRESH lt_podata.
WRITE / 'All Text File List'.
LOOP AT mtab_data WHERE line CS '.txt'.
WRITE: / mtab_data-line.
fname = mtab_data-line.
FREE itab_tmp.
CALL FUNCTION 'FTP_SERVER_TO_R3'
EXPORTING
handle = hdl
fname = mtab_data-line "result-LINE+56(44)
* fname = '4000000037.txt' "result-LINE+56(44)
character_mode = 'X'
TABLES
text = itab_tmp
EXCEPTIONS
tcpip_error = 1
command_error = 2
data_error = 3
OTHERS = 4.
IF sy-subrc = 0.
LOOP AT itab_tmp.
SPLIT itab_tmp-line AT tab
INTO ls_podata-ebeln
ls_podata-ebelp
ls_podata-matnr
ls_podata-txz01.
APPEND ls_podata TO lt_podata.
CLEAR itab_tmp.
ENDLOOP.
ENDIF.
CLEAR mtab_data.
ENDLOOP.
SKIP 1.
WRITE / 'All Text File Content'.
ULINE.
WRITE: 'PO Number'.
WRITE: 12 'Item'.
WRITE: 18 'Material'.
WRITE: 59 'Description'.
SORT lt_podata BY ebeln ebelp.
LOOP AT lt_podata INTO ls_podata.
WRITE: / ls_podata-ebeln, ls_podata-ebelp, ls_podata-matnr, ls_podata-txz01.
CLEAR ls_podata.
ENDLOOP.
* cl_demo_output=>display( lt_podata ).
ENDFORM.
Comments
Post a Comment