0% found this document useful (0 votes)
319 views

Function Module FTP

This ABAP function module connects to an FTP server, retrieves text files from an inbound folder, processes the files, moves them to an outbound folder, and disconnects from the FTP server. It uses various FTP functions like FTP_CONNECT, FTP_COMMAND, FTP_SERVER_TO_R3, and FTP_R3_TO_SERVER to connect, navigate folders, read files, write files, and disconnect. The function accepts parameters for username, password, host, and inbound/outbound folders and returns a table with status messages.

Uploaded by

Adriano Permana
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
319 views

Function Module FTP

This ABAP function module connects to an FTP server, retrieves text files from an inbound folder, processes the files, moves them to an outbound folder, and disconnects from the FTP server. It uses various FTP functions like FTP_CONNECT, FTP_COMMAND, FTP_SERVER_TO_R3, and FTP_R3_TO_SERVER to connect, navigate folders, read files, write files, and disconnect. The function accepts parameters for username, password, host, and inbound/outbound folders and returns a table with status messages.

Uploaded by

Adriano Permana
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

FUNCTION MODULE FTP

https://belajarabap.wordpress.com/

FUNCTION yfm_ftp.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" REFERENCE(FI_USERNAME) TYPE E_DEXCOMMUSERNAME
*" REFERENCE(FI_PASSWORD) TYPE WT_AUTHVAL
*" REFERENCE(FI_HOST) TYPE /ISDFPS/HOSTNAME
*" REFERENCE(FI_RFCDEST) TYPE RFCDES-RFCDEST DEFAULT 'SAPFTP'
*" REFERENCE(FI_FOLDER_INBOUND) TYPE SRMRGDESC
*" REFERENCE(FI_FOLDER_OUTBOUND) TYPE SRMRGDESC
*" TABLES
*" FT_RETURN STRUCTURE TLINE
*"----------------------------------------------------------------------

DATA: BEGIN OF lt_files OCCURS 0,


line(128) TYPE c,
END OF lt_files.

DATA: BEGIN OF lt_result OCCURS 0,


line(128) TYPE c,
END OF lt_result.

DATA: BEGIN OF lt_blob OCCURS 0,


line(2250) TYPE x,
END OF lt_blob.

DATA: BEGIN OF lt_text OCCURS 0,


line(2250) TYPE c, https://belajarabap.wordpress.com
END OF lt_text.

DATA:
ld_password TYPE wt_authval,
ld_handle TYPE i,
ld_command TYPE s_w3txt128,
ld_tabix TYPE sy-tabix,
ld_bloblen TYPE i,
ld_file_out TYPE c LENGTH 128,
ld_extention(128).

1
**Scramble the password provided in a format recognized by SAP.
CALL FUNCTION 'SCRAMBLE_STRING'
EXPORTING
SOURCE = fi_password
key = 26101957
IMPORTING
target = ld_password.

**Connect to the Server using FTP


CALL FUNCTION 'FTP_CONNECT'
EXPORTING
user = fi_username
password = ld_password
host = fi_host
rfc_destination = fi_rfcdest
IMPORTING
handle = ld_handle
EXCEPTIONS
not_connected = 1
OTHERS = 2.
IF sy-subrc = 0.
ft_return-tdformat = 'S'.
ft_return-tdline = 'FTP_CONNECT OK'.
APPEND ft_return.
ELSE.
ft_return-tdformat = 'E'.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
INTO ft_return-tdline.
APPEND ft_return.
RETURN.
ENDIF.

**Accessing FTP Folder https://belajarabap.wordpress.com


CONCATENATE '/' fi_folder_inbound INTO ld_command. CONDENSE ld_command.
CONCATENATE 'cd' ld_command INTO ld_command SEPARATED BY space.

2
CALL FUNCTION 'FTP_COMMAND'
EXPORTING
handle = ld_handle
command = ld_command
compress = 'N'
TABLES
data = lt_result
EXCEPTIONS
tcpip_error = 1
command_error = 2
data_error = 3
OTHERS = 4.
IF sy-subrc = 0.
ft_return-tdformat = 'S'.
ft_return-tdline = 'Change folder directory OK'.
APPEND ft_return.
ELSE.
ft_return-tdformat = 'E'.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
INTO ft_return-tdline.
APPEND ft_return.
RETURN.
ENDIF.

**Accessing file in FTP Folder


CALL FUNCTION 'FTP_COMMAND'
EXPORTING
handle = ld_handle
command = 'nlist'
TABLES
data = lt_files
EXCEPTIONS
tcpip_error = 1
command_error = 2 https://belajarabap.wordpress.com
data_error = 3
OTHERS = 4.

IF sy-subrc = 0.
ft_return-tdformat = 'S'.
ft_return-tdline = 'Get file from FTP server OK'.
APPEND ft_return.
ELSE.

3
ft_return-tdformat = 'E'.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
INTO ft_return-tdline.
APPEND ft_return.
RETURN.
ENDIF.

LOOP AT lt_files.
TRANSLATE lt_files-line TO UPPER CASE.
IF lt_files-line CP '*.TXT'.
CONTINUE.
ELSE.
DELETE lt_files.
ENDIF.
ENDLOOP.

IF lt_files[] IS INITIAL.
ft_return-tdformat = 'E'.
ft_return-tdline = 'No text file to be proceed'.
APPEND ft_return.
RETURN.
ENDIF.

LOOP AT lt_files.
ld_tabix = sy-tabix.

IF ld_tabix GT 1.
****For 1st file, this command already execute above
****But for 2nd file and so on, needs to execute this command to
****change access its folder, because the access folder already change
****when execute outbound file

CONCATENATE '/' fi_folder_inbound INTO ld_command. CONDENSE ld_command.


CONCATENATE 'cd' ld_command INTO ld_command SEPARATED BY space. https://belajarabap.wordpress.com

4
CALL FUNCTION 'FTP_COMMAND'
EXPORTING
handle = ld_handle
command = ld_command
compress = 'N'
TABLES
data = lt_result
EXCEPTIONS
tcpip_error = 1
command_error = 2
data_error = 3
OTHERS = 4.
IF sy-subrc = 0.
ft_return-tdformat = 'S'.
ft_return-tdline = 'Change folder directory OK'.
APPEND ft_return.
ELSE.
ft_return-tdformat = 'E'.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
INTO ft_return-tdline.
APPEND ft_return.
CONTINUE.
ENDIF.
ENDIF.

****Reading file
CALL FUNCTION 'FTP_SERVER_TO_R3'
EXPORTING
handle = ld_handle
fname = lt_files-line
character_mode = 'X'
IMPORTING
blob_length = ld_bloblen
TABLES https://belajarabap.wordpress.com
blob = lt_blob
text = lt_text
EXCEPTIONS
tcpip_error = 1
command_error = 2
data_error = 3
OTHERS = 4.
IF sy-subrc = 0.
ft_return-tdformat = 'S'.
ft_return-tdline = 'Reading file OK'.

5
APPEND ft_return.
ELSE.
ft_return-tdformat = 'E'.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
INTO ft_return-tdline.
APPEND ft_return.
CONTINUE.
ENDIF.

*******************************************************
****** PUT YOUR LOGIC FOR DATA PROCESSING IN HERE *****
*******************************************************

****Delete file after processing


CONCATENATE 'del' lt_files-line INTO ld_command SEPARATED BY space.

CALL FUNCTION 'FTP_COMMAND'


EXPORTING
handle = ld_handle
command = ld_command
TABLES
data = lt_result
EXCEPTIONS
tcpip_error = 1
command_error = 2
data_error = 3
OTHERS = 4.
IF sy-subrc = 0.
ft_return-tdformat = 'S'.
ft_return-tdline = 'Delete file OK'.
APPEND ft_return.
ELSE.
ft_return-tdformat = 'E'.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno https://belajarabap.wordpress.com
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
INTO ft_return-tdline.
APPEND ft_return.
CONTINUE.
ENDIF.

****Change folder to outbound


CONCATENATE '/' fi_folder_outbound INTO ld_command. CONDENSE ld_command.
CONCATENATE 'cd' ld_command INTO ld_command SEPARATED BY space.

6
CALL FUNCTION 'FTP_COMMAND'
EXPORTING
handle = ld_handle
command = ld_command
compress = 'N'
TABLES
data = lt_result
EXCEPTIONS
tcpip_error = 1
command_error = 2
data_error = 3
OTHERS = 4.
IF sy-subrc = 0.
ft_return-tdformat = 'S'.
ft_return-tdline = 'Change to outbound folder directory OK'.
APPEND ft_return.
ELSE.
ft_return-tdformat = 'E'.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
INTO ft_return-tdline.
APPEND ft_return.
CONTINUE.
ENDIF.

****Write to FTP Server


SPLIT lt_files-line AT '.' INTO ld_file_out ld_extention.
CONCATENATE ld_file_out '_out_' sy-datum '.' ld_extention
INTO ld_file_out.

CALL FUNCTION 'FTP_R3_TO_SERVER'


EXPORTING
handle = ld_handle
fname = ld_file_out
blob_length = ld_bloblen https://belajarabap.wordpress.com
character_mode = 'X'
TABLES
blob = lt_blob
text = lt_text
EXCEPTIONS
tcpip_error = 1
command_error = 2
data_error = 3
OTHERS = 4.
IF sy-subrc = 0.

7
ft_return-tdformat = 'S'.
ft_return-tdline = 'Write file to outbound folder OK'.
APPEND ft_return.
ELSE.
ft_return-tdformat = 'E'.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
INTO ft_return-tdline.
APPEND ft_return.
CONTINUE.
ENDIF.

ENDLOOP.

**Disconnect the FTP


CALL FUNCTION 'FTP_DISCONNECT'
EXPORTING
handle = ld_handle.

CALL FUNCTION 'RFC_CONNECTION_CLOSE'


EXPORTING
destination = fi_rfcdest
EXCEPTIONS
OTHERS = 1.
IF sy-subrc = 0.
ft_return-tdformat = 'S'.
ft_return-tdline = 'Cloes FTP connection OK'.
APPEND ft_return.
ELSE.
ft_return-tdformat = 'E'.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
INTO ft_return-tdline.
APPEND ft_return.
ENDIF. https://belajarabap.wordpress.com

ENDFUNCTION.

You might also like