This article demonstrates how to use the SAP FTP function to transfer files from an application server to another FTP server.
Data:begin of ig_ftp_result occurs 0,
Line (100),
END of Ig_ftp_result.
Data:l_path (+) TYPE C. "File path, must end with/
Data:l_filename (+) TYPE C. The file name
Data:l_ftpcommand (+) TYPE C. The FTP command
Data:l_handle TYPE i. "HAND
"Source path, must end with/
Data:cons_source (+) TYPE C. "VALUE '/usr/sap/sy-sysid/sys/src/'."
Data:cons_dens like Zftpt-zpath. The destination path
CONSTANTS cons_key TYPE i VALUE 26101957.
*//initialization
Initialization.
* The path of the server to download files
Concatenate '/usr/sap/' sy-sysid '/sys/src/' into Cons_source.
*//START of SELECTION
Start-of-selection.
PERFORM Frm_ftp_file.
FORM Frm_ftp_file.
DATA:
L_dstlen TYPE I, "DESTINATION LEN
L_PW (+) TYPE C. The password
* Connect to FTP server
L_PW = ' password '.
*--ftp_connect requires an encrypted password to work
* CREATE the NEW PW BASE on LOGIN FTP PASS WORD.
Call ' ab_rfc_x_scramble_string ' "PASS WORD BUILD FUNCTION
ID ' SOURCE ' FIELD l_pw "PASS WORD
ID ' key ' FIELD Cons_key "the KEY to CREATE NEW PW
ID ' SCR ' FIELD ' X '
ID ' DESTINATION ' FIELD l_pw "PASS WORD
ID ' Dstlen ' FIELD l_dstlen. "NEW PASS WORD LEN
Do 3 times.
* OPEN the FTP SERVER.
Call FUNCTION ' Ftp_connect '
Exporting
user = ' username ' "User
Password = L_PW "PASS WORD
Host = ' 192.168.1.10 '
rfc_destination = ' sapftpa ' "DEFAULT
Importing
Handle = L_handle
EXCEPTIONS
not_connected = 1
OTHERS = 2.
IF SY-SUBRC = 0.
EXIT.
ENDIF.
Enddo.
IF SY-SUBRC <> 0.
WRITE:/Sy-datum, Sy-uzeit, Sy-uname, ' CONNECT FTP failed! '. "Messageg
STOP.
ENDIF.
* Change Local Directory
CLEAR L_ftpcommand.
Concatenate ' LCD ' cons_source into L_ftpcommand separated by space.
PERFORM frm_ftp_command USING L_ftpcommand Pr_return.
IF Pr_return = ' 1 '.
write:/sy-datum, Sy-uzeit, Sy-uname, ' FTP change local path Error! '.
STOP.
ENDIF.
* Change FTP directory
IF cons_dens <> '.
CLEAR L_ftpcommand.
Concatenate ' CD ' Cons_dens into L_ftpcommand separated by space.
PERFORM frm_ftp_command USING L_ftpcommand Pr_return.
IF Pr_return = ' 1 '.
write:/sy-datum, Sy-uzeit, Sy-uname, ' Change FTP path Error! '.
STOP.
ENDIF.
ENDIF.
* Change TRANSFER MODE
CLEAR L_ftpcommand.
* L_ftpcommand = ' binary '.
L_ftpcommand = ' ASCII '.
PERFORM frm_ftp_command USING L_ftpcommand Pr_return.
IF Pr_return = ' 1 '.
write:/sy-datum, Sy-uzeit, Sy-uname, ' Change FTP transfer mode Error! '.
STOP.
ENDIF.
* Put File into FTP SERVER
CLEAR L_ftpcommand.
Concatenate ' put ' l_filename into L_ftpcommand separated by space.
PERFORM frm_ftp_command USING L_ftpcommand Pr_return.
IF Pr_return = ' 1 '.
write:/sy-datum, Sy-uzeit, Sy-uname, ' There was an error in the file transfer! '.
STOP.
ENDIF.
* Disconnect the FTP server
Call FUNCTION ' Ftp_disconnect '
Exporting
Handle = L_handle.
write:/sy-datum, Sy-uzeit, sy-uname, ' File transfer succeeded! '.
EndForm. "Frm_ftp_file
************************************************************************
*& FORM Frm_ftp_command *
************************************************************************
*& FTP Command *
************************************************************************
FORM frm_ftp_command USING Pr_command Pr_ret.
Call FUNCTION ' Ftp_command '
Exporting
Handle = L_handle
Command = Pr_command
* COMPRESS =
* Rfc_destination =
* VERIFY =
* Importing
* FILESIZE =
* Filedate =
* FILETIME =
TABLES
data = Ig_ftp_result
EXCEPTIONS
Tcpip_error = 1
Command_error = 2
Data_error = 3
OTHERS = 4
.
* Disconnect
IF SY-SUBRC <> 0.
Pr_ret = ' 1 '.
Call FUNCTION ' Ftp_disconnect '
Exporting
Handle = L_handle.
EXIT.
ENDIF.
EndForm. "Frm_ftp_command
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
This article demonstrates how to use the SAP FTP function to transfer files from an application server to another FTP server.