ABAP Based Solution for FTP Operations (Send/Receive Files

)

Applies to:
SAP BI 7.0/BW 3.x. For more information, visit the Business Intelligence homepage.

Summary
In any IT infrastructure of a Company it is very common to have many servers supporting various applications. Therefore SAP BI being a consumer of data may have to maintain interface with many systems. In such cases sending or receiving files via FTP is a very common task. In this article an ABAP based solution is presented to achieve this in hassle free manner. Author: Hemant Kumar Vyas

Company: Infosys Technologies Limited Created on: 13 December 2009

Author Bio
Hemant Kumar Vyas has been working for Infosys technologies limited for past 2.5 years and has experience in SAP BI 7.0 implementations.

SAP COMMUNITY NETWORK © 2009 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 1

ABAP Based Solution for FTP Operations (Send/Receive Files)

Table of Contents
Salient Features .................................................................................................................................................. 3 Steps to Create ............................................................................................................................................... 3 Create Structure ZZFILI .................................................................................................................................. 4 Creating Table Type Z_ZZFILI ........................................................................................................................ 7 Creating Function Module Z_GET_DIR_LIST ................................................................................................ 9
Source Code Function Module................................................................................................................................... 13 Creating Program Z0I005........................................................................................................................................... 16 Creating Include Z0I005_TOP ................................................................................................................................... 17 Creating Include Z0I005_SELECTION ...................................................................................................................... 18 Creating Include Z0I005_FORMS .............................................................................................................................. 18 Some Points Regarding Codes .................................................................................................................................. 26 Creating Text Element and Selection Text Elements ................................................................................................. 28

Related Content ................................................................................................................................................ 31 Disclaimer and Liability Notice .......................................................................................................................... 32

SAP COMMUNITY NETWORK © 2009 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 2

ABAP Based Solution for FTP Operations (Send/Receive Files)

Salient Features
Following are some salient points regarding  Program will be reusable (i.e. independent of projects/generic).  Program will accept local path, Remote path, User Id, password etc.  Program will also have an option for file stem field which will let the user choose which files to send/receive I.e. ASD* will pick files starting with ASD only.  Can be integrated in process chains using a variant.  Variant need not be changed when transport happens as Program takes care of the path on whatever system it is. Following screenshot shows various parameters.

Steps to Create The Program works on the principle that once we open a FTP connection using standard SAP provided Function Modules we can create the sequence of commands to execute to our effect. Following are the steps to create the solution: 1. Create structure ZZFILI (is used for providing a structure to organize file data). 2. Create table type Z_ZZFILI. 3. Create Function Module Z_GET_DIR_LIST (is used to read the files available in the directory) 4. Create ABAP Program Z0I005. 5. Create Includes Z0I005_TOP, Z0I005_FORMS, Z0I005_SELECTION. 6. Create Text elements and selection text elements. 7. Create Messages.

SAP COMMUNITY NETWORK © 2009 SAP AG

SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com 3

sap.sap.boc. When asked for type choose ‘data element’ SAP COMMUNITY NETWORK © 2009 SAP AG SDN .com | BOC .ABAP Based Solution for FTP Operations (Send/Receive Files) Create Structure ZZFILI 1.com | BPX .sap.bpx.com 4 .sdn. Create data element ZNAME (Char 1024) description: File Name Go to transaction code SE11 and choose ‘data type’ type the name ZNAME and press ‘create’.

sap.sap.com | BOC .bpx.sap.com | BPX .com 5 . and ZMASK (Char 40) Description: File Mask. See SAP COMMUNITY NETWORK © 2009 SAP AG SDN . Similarly create ZSIZE (INT4) Description Size screenshots below.ABAP Based Solution for FTP Operations (Send/Receive Files) Save and Activate.sdn.boc.

sap.com | BOC .sdn.sap. Go to Transaction code SE11 choose ‘data type’ and type ZZFILI and press create. Choose the enhancement category from (ExtrasEnhancement category) ‘Can not be enhanced’.com | BPX . Save and activate.boc.com 6 . SAP COMMUNITY NETWORK © 2009 SAP AG SDN . Make following entries. when asked for type choose structure.sap.bpx.ABAP Based Solution for FTP Operations (Send/Receive Files) After creating data elements Structure ZZFILI (List of Files) needs to be created.

sdn.ABAP Based Solution for FTP Operations (Send/Receive Files) Creating Table Type Z_ZZFILI For ECC6. 2. Go to Transaction code SE11.com | BOC .bpx. Give description and line type as ZZFILI.0 and above it is not possible to give ‘TABLES’ parameters so the solution requires a table type in the function module Z_GET_DIR_LIST and Z_MOVE_FILES. 3. 5. Follow the screen shots.boc.sap.com | BPX .sap. Press ‘create’.com 7 . SAP COMMUNITY NETWORK © 2009 SAP AG SDN . Select data type and type Z_ZZFILI. 4. Select type as table type.sap. 6. Steps to create table type Z_ZZFILI. 1.

sdn.com 8 .boc.com | BPX .com | BOC .sap.bpx.sap.ABAP Based Solution for FTP Operations (Send/Receive Files) Save and Activate SAP COMMUNITY NETWORK © 2009 SAP AG SDN .sap.

boc. We will create a function group ZSCG.com | BOC . Choose Function group from drop down box.sap. SAP COMMUNITY NETWORK © 2009 SAP AG SDN .com | BPX . but it is better to create separate function groups for function module whose functionalities are different.com 9 .bpx. 2.sap.sap. 3. Follow the screenshots. 4.) 1. Go to transaction code SE80.ABAP Based Solution for FTP Operations (Send/Receive Files) Creating Function Module Z_GET_DIR_LIST For creating function module we will need a function group (can be created new or can be existing. Type the name ZSCG and press enter.sdn.

ABAP Based Solution for FTP Operations (Send/Receive Files) After creation of Function Group do Right ClickCreateFunction module.sdn.com | BOC .com 10 .sap.sap. SAP COMMUNITY NETWORK © 2009 SAP AG SDN .sap.bpx.com | BPX .boc.

com | BPX .sap.com | BOC . SAP COMMUNITY NETWORK © 2009 SAP AG SDN .sap. 2.sdn.com 11 . Start with attributes.sap.bpx.boc. Import parameters.ABAP Based Solution for FTP Operations (Send/Receive Files) 1.

Changing parameters 5. Export parameters.com | BOC .sap. Exceptions SAP COMMUNITY NETWORK © 2009 SAP AG SDN .sap.sdn.com 12 .com | BPX .bpx. 4.sap.boc.ABAP Based Solution for FTP Operations (Send/Receive Files) 3.

type c.sap. *"---------------------------------------------------------------------*"*"Local Interface: *" IMPORTING *" REFERENCE(FILE_NAME) TYPE ZZFILI-ZNAME *" REFERENCE(FILE_MASK) TYPE ZZFILI-ZMASK *" EXPORTING *" REFERENCE(RETURN_CODE) TYPE SY-SUBRC *" CHANGING *" REFERENCE(LIST) TYPE Z_ZZFILI *" EXCEPTIONS *" LIST_ERROR *" FILE_NAME_TOO_LONG *"---------------------------------------------------------------------- types: name_of_dir(1024) name_of_file(260) name_of_path(1285) type c. type p. " length in bytes. " owner of the entry. " type of entry.sap.sap.boc.bpx. data: begin of file. name type(10) len(8) owner(8) mtime(6) mode(9) " name of directory. dirname type name_of_dir. seconds since 1970 type c. " last modification date.sdn. (possibly " truncated. type p.com 13 .) type c. type c.com | BPX . (possibly " truncated.ABAP Based Solution for FTP Operations (Send/Receive Files) 6.) type name_of_file. " like "rwx-r-x--x": protection mode. type c. " name of entry. SAP COMMUNITY NETWORK © 2009 SAP AG SDN .com | BOC . Add a text element Go toText ElementsText Symbols Source Code Function Module FUNCTION Z_GET_DIR_LIST.

) name type name_of_file. c. mod_time(8) type c." name of entry. " name of directory.ABAP Based Solution for FTP Operations (Send/Receive Files) useable(1) subrc(4) errno(3) errmsg(40) mod_date mod_time(8) seen(1) changed(1) end of file. call 'C_DIR_READ_FINISH' " just to be sure id 'ERRNO' field file_list-errno id 'ERRMSG' field file_list-errmsg. a_generic_name = file_mask. call 'C_DIR_READ_START' id 'DIR' field a_dir_name id 'FILE' field a_generic_name id 'ERRNO' field file-errno id 'ERRMSG' field file-errmsg. type type type type type type type type c. data: a_dir_name type name_of_dir. " hh:mm:ss seen(1) type c. " last modification date. (possibly " truncated. data: errcnt(2) type p value 0. seconds since 1970 mode(9) type c. " name of entry. " 'Place cursor on valid line !'. c.sdn. a_generic_name type name_of_file. (possibly " truncated. dirname type name_of_dir. " owner of the entry.) type(10) type c. parameters: p_dir type name_of_dir lower case.bpx. mtime(6) type p. len(8) type p. a_dir_name = file_name. changed(1) type c. " like "rwx-r-x--x": protection mode. " type of entry. errno(3) type c.com 14 . (may end with *) data wa_list like line of list. " name of directory. subrc(4) type c. c. c. c.sap. MESSAGE e220. " hh:mm:ss data: begin of file_list occurs 100.com | BPX .sap.com | BOC . " length in bytes.sap. errmsg(40) type c.boc. SAP COMMUNITY NETWORK © 2009 SAP AG SDN . useable(1) type c. * * * * IF a_dir_name IS INITIAL. mod_date type d. ENDIF. c. owner(8) type c. d. end of file_list.

when 0. * WRITE : / FILE_LIST-NAME. raise list_error. " filename too long raise list_error. append wa_list to list. " end of directory exit. sort list by zname ascending.sap. do. 'SUBRC'. sy-subrc = 0. when 4. loop at file_list. file-dirname = a_dir_name.boc. call 'C_DIR_READ_FINISH' id 'ERRNO' field file_list-errno id 'ERRMSG' field file_list-errmsg. sort file_list by name. sy-subrc. endif. raise file_name_too_long.com | BPX . enddo.com 15 .sap.sdn. when 1. SAP COMMUNITY NETWORK © 2009 SAP AG SDN . sy-subrc = 4. if sy-subrc <> 0. clear file. endif.sap.com | BOC . when others. move sy-subrc to file-subrc. move-corresponding file to file_list. move file_list-name to wa_list-zname. ENDFUNCTION.ABAP Based Solution for FTP Operations (Send/Receive Files) if sy-subrc <> 0.bpx. if file-type = text-001. append file_list. endloop. case sy-subrc. write: / 'C_DIR_READ_FINISH'. call 'C_DIR_READ_NEXT' id 'TYPE' field file-type id 'NAME' field file-name id 'LEN' field file-len id 'OWNER' field file-owner id 'MTIME' field file-mtime id 'MODE' field file-mode id 'ERRNO' field file-errno id 'ERRMSG' field file-errmsg. endcase. endif.

sap. SAP COMMUNITY NETWORK © 2009 SAP AG SDN . Type the name as Z0I005.sap.bpx. include z0i005_top. "Use program to send files perform check_local_path.sdn. Give following properties. a) Now Add the following code to Z0I005 report z0i005 no standard page heading message-id z0i005 line-count 60 line-size 132. Hit Enter.com 16 . These can be specific to the project.boc. .com | BPX . "data definition "selection screen "forms if psend eq 'X'. 2.com | BOC . 4.ABAP Based Solution for FTP Operations (Send/Receive Files) Creating Program Z0I005 To create program follow the steps. 1. perform check_for_files. include z0i005_selection. 3. Go to transaction Code SE80. include z0i005_forms. start-of-selection. Select Program from drop down box.sap.

else. message i018. endif. Creating Include Z0I005_TOP Add following code *&---------------------------------------------------------------------* *& Include Z0I005_TOP *&---------------------------------------------------------------------* types: begin of ty_text. type type type type type i. if v_subrc eq 0. "results of moving file like line of it_result. b) Now Do Right ClickCreateInclude g) Give the name of include as ‘Z0I005_top’. Similarly create 2 more includes as Z0I005_selection. line(255) type c.com | BOC . begin of ty_ren_list.sap.com | BPX . endif. "list of files in a directory it_move_results type table of ty_move_results. types: types: ty_move_results type btcxpm.ABAP Based Solution for FTP Operations (Send/Receive Files) if v_no_files_found ne c_x. "FTP results table it_commands type table of ty_text. perform send_files. "FTP commands it_list type table of zzfili. constants: c_navgl(6) type c value 'ABC*'. data: it_result type table of ty_text.sdn. c_asterisk(1) type c value '*'. "Encryption key c. wa_command like line of it_commands. endif. tmp_name(50) type c. PERFORM retrieve_files.sap.boc. wa_list like line of it_list. i value 26101957. i. "Use Program to receive files PERFORM check_local_path. end of ty_text. data: wa_result data: v_handle v_command_index v_key v_encrypted_pwd(80) v_slen SAP COMMUNITY NETWORK © 2009 SAP AG SDN . wa_move_results like line of it_move_results.bpx. c_x(1) type c value 'X'. i. end of ty_ren_list.sap. name(50) type c.com 17 . Z0I005_forms.

*----------------------------------------------------------------------at selection-screen output. parameter: pstem type zzfili-zmask lower case obligatory default c_navgl. selection-screen skip 1. i.sap.com | BPX . c. zzfili-zmask. ppwd(40) type c lower case obligatory.ABAP Based Solution for FTP Operations (Send/Receive Files) v_filename v_source_dir v_dest_dir v_subrc v_no_files_found v_files_found v_file v_lines v_stem v_loop type type type like type type type type type type string. c. parameter: psend radiobutton group r2 default 'X'. Creating Include Z0I005_FORMS Add following code SAP COMMUNITY NETWORK © 2009 SAP AG SDN . pbin radiobutton group r1. preceive radiobutton group r2. string.com 18 . Creating Include Z0I005_SELECTION Add following code selection-screen begin of block b with frame title text-002.sap. constants: c_rfc_destination type rfcdes-rfcdest value 'SAPFTPA'. screen-invisible = '1'. selection-screen end of block b. *----------------------------------------------------------------------* Scramble Password field on screen loop at screen. modify screen. pasc radiobutton group r1 default 'X'.boc. puser(40) type c lower case obligatory. sy-subrc.sdn.bpx. phost(70) type c lower case obligatory.com | BOC .sap. selection-screen end of block a. c. string. selection-screen begin of block a with frame title text-001. plpath type zzfili-zname lower case obligatory. endloop. string. prpath(70) type c lower case obligatory. if screen-name = 'PPWD'. endif.

sap.sdn. v_dest_dir = prpath. elseif sy-subrc eq 2. perform check_directory using v_source_dir.com | BOC . message e013.sap. l_source_dir = p_v_source_dir. condense v_filename no-gaps.ABAP Based Solution for FTP Operations (Send/Receive Files) *&---------------------------------------------------------------------* *& Include Z0I005_FORMS *&---------------------------------------------------------------------* form check_local_path . v_source_dir = plpath. * check whether local directory to send data from exists * and raise an error if it does not or if it is not accessible * due to permissions. condense v_dest_dir no-gaps. SAP COMMUNITY NETWORK © 2009 SAP AG SDN . others if sy-subrc ne 0. " CHECK_LOCAL_PATH *&---------------------------------------------------------------------* *& Form CHECK_DIRECTORY *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * -->P_V_SOURCE_DIR text *----------------------------------------------------------------------* form check_directory using p_v_source_dir. call function 'PFL_CHECK_DIRECTORY' exporting directory write_check = l_source_dir = 'X' * FILNAME exceptions pfl_dir_not_exist pfl_permission_denied pfl_cant_build_dataset_name pfl_file_not_exist = ' ' = 1 = 2 = 3 = 4 = 5. if sy-subrc eq 1. condense v_source_dir no-gaps.sap.com | BPX .boc. v_filename = pstem.com 19 . * check whether a directory exists and error if it does not or is not * accessible data: l_source_dir type btch0000-text80. endform. / l_source_dir. write: / 'Local directory on SAP Server does not exist:'(014).bpx.

condense v_stem no-gaps. message e014. endform. move pstem to v_stem.sap.com | BOC .p2 text *----------------------------------------------------------------------* form check_for_files .sdn. v_subrc = sy-subrc. replace all occurrences of c_asterisk in v_stem with space.bpx. if pstem(1) eq c_asterisk and pstem(2) is not initial. loop at it_list into wa_list where zname ns v_stem.sap.com | BPX . " CHECK_DIRECTORY *&---------------------------------------------------------------------* *& Form CHECK_FOR_FILES *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * --> p1 text * <-. / l_source_dir. write: / 'Unspecified error checking local directory on SAP Server:'(016). message e015. SAP COMMUNITY NETWORK © 2009 SAP AG SDN . * check if files exist * read files in directory refresh it_list.com 20 . endif. delete it_list. / l_source_dir. endif. call function 'Z_GET_DIR_LIST' exporting file_name file_mask = plpath = pstem = it_list changing list exceptions list_error = 1 file_name_too_long = 2. endif.ABAP Based Solution for FTP Operations (Send/Receive Files) write: / 'No access to local directory on SAP server:'(015). endloop.boc.sap. else.

com 21 . message e019 with v_subrc.sap.sap. v_no_files_found = c_x. if it_list[] is initial. write: /'Error reading SAP server local directory:'(031). endif.bpx. plpath. * Calculate password length v_slen = strlen( ppwd ). /'File stem:'(006). prpath.com | BOC . write: /'Files to be sent to the remote server:'(012). pstem. /'SAP Server file path:'(007). /'SAP Server file path:'(007). write: /10 wa_list-zname. else. skip 1.boc. endform. else.sap. if v_subrc eq 0. skip 2. * connect to the FTP server and send the files. endif. phost. /'Directory:'(011). loop at it_list into wa_list. plpath. write: /'No files have been found in SAP server directory:'(005). * Encrypt password call function 'HTTP_SCRAMBLE' exporting source sourcelen = ppwd = v_slen = v_key " CHECK_FOR_FILES key SAP COMMUNITY NETWORK © 2009 SAP AG SDN . endloop.ABAP Based Solution for FTP Operations (Send/Receive Files) skip 2. form send_files . message i005.sdn.com | BPX .

endloop. append wa_command to it_commands.sap. append wa_command to it_commands.boc. * Set Ascii mode clear wa_command-line. move 'bin' to wa_command-line.ABAP Based Solution for FTP Operations (Send/Receive Files) importing destination = v_encrypted_pwd. * Change the local directory on the sap server concatenate 'lcd' plpath into wa_command-line separated by space. refresh it_commands.bpx.com | BOC . * Send FTP commands to server call function 'FTP_COMMAND_LIST' exporting handle = v_handle importing command_index = v_command_index tables data = it_result SAP COMMUNITY NETWORK © 2009 SAP AG SDN . else. * change the directory on the destination machine concatenate 'cd' prpath into wa_command-line separated by space. if sy-subrc <> 0. endif. append wa_command to it_commands. * Connect to destination server call function 'FTP_CONNECT' exporting user password host rfc_destination = = = = puser v_encrypted_pwd phost c_rfc_destination importing handle = v_handle exceptions not_connected. move 'asc' to wa_command-line. message e002 with phost sy-subrc. refresh it_result.sap.sdn.com 22 .sap. * files to be transferred loop at it_list into wa_list.com | BPX . if pbin eq 'X'. append wa_command to it_commands. endif. * Put the file from the sap server to the destination machine concatenate 'put' wa_list-zname wa_list-zname into wa_command-line separated by space.

* Close RFC connection SAP COMMUNITY NETWORK © 2009 SAP AG SDN .sdn. " SEND_FILES *&--------------------------------------------------------------------* *& Form ftp_disconnect *&--------------------------------------------------------------------* * text *---------------------------------------------------------------------* form ftp_disconnect using in_handle in_rfc_destination. * if there is an error disconnect from the server * and display an appropriate message perform ftp_disconnect using v_handle c_rfc_destination.com | BPX .bpx. v_subrc = sy-subrc. endloop. endform.com 23 . message e004. prpath. write: /'Messages generated by FTP Send to remote server:'(009). write: /10 wa_result. * write results of FTP and error if an error occurred. if v_subrc ne 0.sap. * Disconnect from destination server call function 'FTP_DISCONNECT' exporting handle = in_handle.com | BOC . skip 2. ** For each FTP command result line loop at it_result into wa_result. phost.sap.ABAP Based Solution for FTP Operations (Send/Receive Files) commands = it_commands exceptions command_error = 1 tcpip_error = 2. endif. skip 1.boc. /'Directory:'(011).sap.

* Change the directory on the destination machine CONCATENATE 'cd' PRPATH INTO WA_COMMAND-LINE SEPARATED BY SPACE. REFRESH IT_COMMANDS.com | BPX . * connect to the FTP server and retrieve the files. MOVE 'bin' TO WA_COMMAND-LINE. REFRESH IT_RESULT. IF SY-SUBRC <> 0.sap. * Encrypt password CALL FUNCTION 'HTTP_SCRAMBLE' EXPORTING SOURCE SOURCELEN = PPWD = V_SLEN = V_KEY KEY IMPORTING DESTINATION = V_ENCRYPTED_PWD. = 1.sap. * Connect to destination server CALL FUNCTION 'FTP_CONNECT' EXPORTING USER PASSWORD HOST RFC_DESTINATION = = = = PUSER V_ENCRYPTED_PWD PHOST C_RFC_DESTINATION IMPORTING HANDLE = V_HANDLE EXCEPTIONS NOT_CONNECTED. APPEND WA_COMMAND TO IT_COMMANDS. APPEND WA_COMMAND TO IT_COMMANDS. * Change the local directory on the sap server CONCATENATE 'lcd' PLPATH INTO WA_COMMAND-LINE SEPARATED BY SPACE. MESSAGE E002 WITH PHOST SY-SUBRC.ABAP Based Solution for FTP Operations (Send/Receive Files) call function 'RFC_CONNECTION_CLOSE' exporting destination = in_rfc_destination exceptions others endform. "ftp_disconnect FORM RETRIEVE_FILES .sap.com | BOC .com 24 .boc.sdn. ENDIF. SAP COMMUNITY NETWORK © 2009 SAP AG SDN .bpx. CLEAR WA_COMMAND-LINE. * Calculate password length V_SLEN = STRLEN( PPWD ). IF PBIN EQ 'X'.

ENDIF. V_NO_FILES_FOUND = C_X. * not finding files to retrieve is not an error. * write results of FTP and error if an error occurred. SKIP 1.com | BOC . but will be reported. V_LOOP = 'Y'.boc. WRITE: /'Messages generated by FTP Retrieval from remote server:'(009).sap. APPEND WA_COMMAND TO IT_COMMANDS. ** For each FTP command result line LOOP AT IT_RESULT INTO WA_RESULT. * get files from remote server CONCATENATE 'mget' PSTEM INTO WA_COMMAND-LINE SEPARATED BY SPACE.com 25 . so look for error message to distinguish from genuine error IF WA_RESULT-LINE EQ TEXT-003. * Send FTP commands to server CALL FUNCTION 'FTP_COMMAND_LIST' EXPORTING HANDLE = V_HANDLE IMPORTING COMMAND_INDEX = V_COMMAND_INDEX TABLES DATA COMMANDS = IT_RESULT = IT_COMMANDS EXCEPTIONS COMMAND_ERROR = 1 TCPIP_ERROR = 2. ENDIF. CLEAR V_NO_FILES_FOUND. CLEAR V_FILES_FOUND.sap. PRPATH. /'Directory:'(011). V_SUBRC = SY-SUBRC.bpx.sdn. * * check for files returned from NT server return code is 0. APPEND WA_COMMAND TO IT_COMMANDS. * * * check for no files returned from AIX server return code is 1.com | BPX . MOVE 'asc' TO WA_COMMAND-LINE.ABAP Based Solution for FTP Operations (Send/Receive Files) ELSE.sap. WRITE: /10 WA_RESULT. so check for existence of SAP COMMUNITY NETWORK © 2009 SAP AG SDN . PHOST.

V_NO_FILES_FOUND = C_X. "D01K910515 ELSE. IF V_NO_FILES_FOUND EQ C_X OR ( V_SUBRC EQ 0 AND V_FILES_FOUND IS INITIAL ).sap.sdn.boc. WRITE: /'No files have been found on remote server. ENDIF. V_NO_FILES_FOUND = C_X. PSTEM.sap. MESSAGE E017. IF V_SUBRC NE 0 OR ( V_SUBRC EQ 0 AND V_FILES_FOUND IS INITIAL ). " RETRIEVE_FILES Some Points Regarding Codes 1) The sequence of commands used for sending files is following a) CD <Remote path> (changes directory on server to desired one. ENDIF. MESSAGE E003 WITH PSTEM.ABAP Based Solution for FTP Operations (Send/Receive Files) * transfer complete message IF WA_RESULT-LINE EQ TEXT-004 OR WA_RESULT-LINE EQ TEXT-005. IF V_LOOP = 'N'.com | BPX . * if there is an error or there are no files disconnect from the server * and display an appropriate message PERFORM FTP_DISCONNECT USING V_HANDLE C_RFC_DESTINATION. ENDIF. V_LOOP = 'Y'.) b) LCD <Local Path> (changes local directory) c) ASC or BIN (To set the transfer mode) d) PUT <FILE NAME1> (sends file from local machine to remote machine) e) PUT <FILE NAME2> f) … SAP COMMUNITY NETWORK © 2009 SAP AG SDN . ENDFORM.bpx.com | BOC . ENDLOOP. SKIP 2.com 26 .sap. File Stem:'(004). V_FILES_FOUND = C_X. ENDIF.

boc. IF PBIN EQ 'X'.sap. else.com | BOC .ABAP Based Solution for FTP Operations (Send/Receive Files) 2) The sequence of commands used for sending files is following a) CD <Remote path> (changes directory on server to desired one. append wa_command to it_commands. * get files from remote server CONCATENATE 'mget' PSTEM INTO WA_COMMANDLINE SEPARATED BY SPACE. SAP COMMUNITY NETWORK © 2009 SAP AG SDN . CLEAR WA_COMMAND-LINE.sap. concatenate 'cd' prpath into wa_commandline separated by space.sdn.bpx. endif. Receiving file sequence * Change the directory on the destination machine CONCATENATE 'cd' PRPATH INTO WA_COMMANDLINE SEPARATED BY SPACE. loop at it_list into wa_list. Though same FTP commands are used in NT servers as well but need to be sure).sap.com 27 . ENDIF. ELSE. if pbin eq 'X'.com | BPX . MOVE 'bin' TO WA_COMMAND-LINE. * Set Ascii mode * files to be transferred clear wa_command-line. If commands need to be modified these are the places to do it. APPEND WA_COMMAND TO IT_COMMANDS.g. MOVE 'asc' TO WA_COMMAND-LINE. * Put the file from the sap server to the destination machine concatenate 'put' wa_list-zname wa_list-zname into wa_commandline separated by space. Sending file sequence * Change the local directory on the sap server concatenate 'lcd' plpath into wa_commandline separated by space. APPEND WA_COMMAND TO IT_COMMANDS. append wa_command to it_commands. APPEND WA_COMMAND TO IT_COMMANDS.) b) LCD <Local Path> (changes local directory) c) MGET <FILENAME>* (gets all the files specified by wild card e. APPEND WA_COMMAND TO IT_COMMANDS. append wa_command to it_commands. move 'bin' to wa_command-line. append wa_command to it_commands. ABC*) Based on the operating systems in concern both at local and remote server these commands need to be tweaked (In given coding UNIX commands are used as most of the times servers are run on UNIX. endloop. move 'asc' to wa_command-line. * Change the local directory on the sap server CONCATENATE 'lcd' PLPATH INTO WA_COMMANDLINE SEPARATED BY SPACE.

sap.sap.boc.ABAP Based Solution for FTP Operations (Send/Receive Files) Creating Text Element and Selection Text Elements To Create Text Elements for the program do GoToText ElementsText Symbols Create following text symbols.bpx. Create selection text.sdn.com 28 .com | BOC .com | BPX . SAP COMMUNITY NETWORK © 2009 SAP AG SDN .sap.

sdn.com 29 .com | BOC .sap. For creating messages do Go ToMessages.sap.com | BPX . Give attributes and messages as shown.bpx.boc.sap.ABAP Based Solution for FTP Operations (Send/Receive Files) Create messages. After creating all the objects Save and Activate. SAP COMMUNITY NETWORK © 2009 SAP AG SDN .

sap.boc.com | BPX . SAP COMMUNITY NETWORK © 2009 SAP AG SDN .bpx.sdn.ABAP Based Solution for FTP Operations (Send/Receive Files) Following will be the screen when we run it.com | BOC .sap.com 30 .sap.

sap. visit the Business Intelligence homepage.bpx.sdn.com/wiki/display/Snippets/ABAP+solution+to+implement+FTP+transactions http://www.sap.com 31 .columbia.com | BPX .sdn.com/irj/scn/weblogs?blog=/pub/wlg/4743 For more information.sap.sdn. SAP COMMUNITY NETWORK © 2009 SAP AG SDN .edu/acis/eds/data_tools/ftp-man/ftp_com.ABAP Based Solution for FTP Operations (Send/Receive Files) Related Content http://wiki.sap.boc.com | BOC .sap.html http://www.

ABAP Based Solution for FTP Operations (Send/Receive Files) Disclaimer and Liability Notice This document may discuss sample coding or other information that does not include SAP official interfaces and therefore is not supported by SAP.sap. SAP responsible or liable with respect to the content of this document.com | BOC . code or methods suggested in this document.sap.com | BPX .bpx.boc. SAP offers no guarantees and assumes no responsibility or liability of any type with respect to the content of this technical article or code sample.sdn. Changes made based on this information are not supported and can be overwritten during an upgrade. SAP COMMUNITY NETWORK © 2009 SAP AG SDN . You agree that you will not hold. including any liability resulting from incompatibility between the content within this document and the materials and services offered by SAP. or seek to hold. and anyone using these methods does so at his/her own risk.com 32 . SAP will not be held liable for any damages caused by using or misusing the information.sap.

Sign up to vote on this title
UsefulNot useful