Sunday, December 26, 2010
*&---------------------------------------------------------------------
*& Report  ZFTP_CONNECT
*&
*&---------------------------------------------------------------------
REPORT  zftp_connect.
DATA: BEGIN OF mtab_data OCCURS 0,
line(132) TYPE c,
END OF mtab_data.
DATA: BEGIN OF mtab_data1 OCCURS 0,
line(132) TYPE c,
END OF mtab_data1.
DATA : error TYPE string.
DATA: mc_password(20) TYPE c,
err(20) TYPE c,
mi_key TYPE i VALUE 26101957,
mi_pwd_len TYPE i,
mi_handle TYPE i.
DATA: mat TYPE bapimatall.
DATA: lv_user(30) TYPE c,
lv_pwd(30) TYPE c ,
lv_host(64) TYPE c,
iv_file_path(20) TYPE c,
cmd(120).
DATA: lv_dest TYPE rfcdes-rfcdest.

START-OF-SELECTION.
CALL FUNCTION 'FTP_CONNECT'
EXPORTING
user            = lv_user "Your SAP-UNIX FTP user name (case sensitive)
password        = lv_pwd  "Your SAP-UNIX server host name (case sensitive)
host            = '172.20.15.73'
rfc_destination = 'SAPFTP'
IMPORTING
handle          = mi_handle
EXCEPTIONS
not_connected   = 1
OTHERS          = 2.
CHECK sy-subrc = 0.
cmd = 'cd \Material" "Master'.
*--This is sender directory path
*--Note down that if there is space in directory name it can be handled by " "
*--command cd \Material master will generate error
PERFORM ftp_command USING cmd.
cmd = 'ascii'.
*--FTP transfers are generally performed in either ASCII or binary mode. Binary mode is the preferred mode to transfer non-text files. ASCII mode is the preferred mode to transfer text files.
PERFORM ftp_command USING cmd.
cmd = 'lcd d:\ftp'.          "This is target directory path on local machine
PERFORM ftp_command USING cmd.
cmd = 'ls'.                  "list all the files available under source folder
PERFORM ftp_command USING cmd.
*  mtab_data contains file names of all files under source folder
LOOP AT mtab_data FROM 2.
CONCATENATE 'get' mtab_data+39(80) INTO cmd SEPARATED BY space.
CALL FUNCTION 'FTP_COMMAND'
EXPORTING
handle        = mi_handle
command       = cmd
TABLES
data          = mtab_data1
EXCEPTIONS
tcpip_error   = 1
command_error = 2
data_error    = 3
OTHERS        = 4.
IF sy-subrc = 0.
LOOP AT mtab_data1.
WRITE: / mtab_data1.
ENDLOOP.
ELSE.
*      do some error checking.
CONCATENATE 'Error in FTP Command while executing' cmd INTO error SEPARATED BY space.
WRITE: / error.
ENDIF.
CLEAR mtab_data1[].
CONCATENATE 'delete' mtab_data+39(80) INTO cmd SEPARATED BY space.
PERFORM ftp_command USING cmd.
*   IF you WANT delete FILE after copy command you can run delete command
ENDLOOP.
CLEAR mtab_data[].
*--Disconnect from FTP server
CALL FUNCTION 'FTP_DISCONNECT'
EXPORTING
handle = mi_handle
EXCEPTIONS
OTHERS = 1.
*--Close RFC connection
CALL FUNCTION 'RFC_CONNECTION_CLOSE'
EXPORTING
destination = 'SAPFTP'
EXCEPTIONS
OTHERS      = 1.
*&---------------------------------------------------------------------*
*&      Form  ftp_command
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_CMD      text
*----------------------------------------------------------------------*
FORM ftp_command  USING    p_cmd.
CALL FUNCTION 'FTP_COMMAND'
EXPORTING
handle        = mi_handle
command       = p_cmd
TABLES
data          = mtab_data
EXCEPTIONS
tcpip_error   = 1
command_error = 2
data_error    = 3
OTHERS        = 4.
IF sy-subrc = 0.
LOOP AT mtab_data.
WRITE: / mtab_data.
ENDLOOP.
ELSE.
*    do some error checking.
WRITE: / 'Error in FTP Command'.
ENDIF.
IF p_cmd <> 'ls'.
CLEAR mtab_data[].
ENDIF.
ENDFORM.                    " ftp_command

0 comments:

Post a Comment

Your useful comments, suggestions are appreciated.Your comments are moderated.

Followers

Contact Form

Name

Email *

Message *

Web Dynpro ABAP Book

An SAP Consultant

Follow US


Want to Contribute ?

If you are interested in writing about the new stuff you learn everyday while working, please write to the.sap.consultants@gmail.com.

Click on Contribution for more details.