This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com
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.
Salient Features .................................................................................................................................................. 3
Steps to Create ............................................................................................................................................... 3
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
ABAP Based Solution for FTP Operations (Send/Receive Files)
SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com
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.
ABAP Based Solution for FTP Operations (Send/Receive Files)
SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com
1. 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’. When asked for type choose ‘data element’
ABAP Based Solution for FTP Operations (Send/Receive Files)
SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com
After creating data elements Structure ZZFILI (List of Files) needs to be created. Go to Transaction code SE11 choose ‘data type’ and type ZZFILI and press create, when asked for type choose structure. Make following entries.
Choose the enhancement category from (ExtrasEnhancement category) ‘Can not be enhanced’. Save and activate.
ABAP Based Solution for FTP Operations (Send/Receive Files)
SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com
For ECC6.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.
Steps to create table type Z_ZZFILI.
1. Go to Transaction code SE11.
2. Select data type and type Z_ZZFILI.
3. Press ‘create’.
4. Select type as table type.
5. Give description and line type as ZZFILI.
6. Follow the screen shots.
ABAP Based Solution for FTP Operations (Send/Receive Files)
SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com
For creating function module we will need a function group (can be created new or can be existing, but it is better to create separate function groups for function module whose functionalities are different.)
1. We will create a function group ZSCG.
2. Go to transaction code SE80.
3. Choose Function group from drop down box.
4. Type the name ZSCG and press enter.
Follow the screenshots.
ABAP Based Solution for FTP Operations (Send/Receive Files)
SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com
IF WA_RESULT-LINE EQ TEXT-004 OR WA_RESULT-LINE EQ TEXT-005.
V_FILES_FOUND = C_X.
V_LOOP = 'Y'.
ENDIF.
ENDLOOP.
IF V_LOOP = 'N'.
V_NO_FILES_FOUND = C_X.
ENDIF.
SKIP 2.
IF V_SUBRC NE 0
OR ( V_SUBRC EQ 0
AND V_FILES_FOUND IS INITIAL ).
* 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.
IF V_NO_FILES_FOUND EQ C_X
OR ( V_SUBRC EQ 0
AND V_FILES_FOUND IS INITIAL ).
WRITE:
/'No files have been found on remote server. File Stem:'(004),
PSTEM.
MESSAGE E003 WITH PSTEM. "D01K910515
V_NO_FILES_FOUND = C_X.
ELSE.
MESSAGE E017.
ENDIF.
ENDIF.
ENDFORM. " 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.) 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) …
ABAP Based Solution for FTP Operations (Send/Receive Files)
SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com
2) The sequence of commands used for sending files is following a) CD <Remote path> (changes directory on server to desired one.) b) LCD <Local Path> (changes local directory) c) MGET <FILENAME>* (gets all the files specified by wild card e.g. 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. Though same FTP commands are used in NT servers as well but need to be sure).
If commands need to be modified these are the places to do it.
Sending file sequence
concatenate 'cd' prpath into wa_command-line separated by space. append wa_command to it_commands. * Change the local directory on the sap server concatenate 'lcd' plpath into wa_command-line separated by space. append wa_command to it_commands. * Set Ascii mode clear wa_command-line. if pbin eq 'X'. move 'bin' to wa_command-line. else. move 'asc' to wa_command-line. endif. append wa_command to it_commands. * files to be transferred loop at it_list into wa_list. * 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. append wa_command to it_commands. endloop.
Receiving file sequence
* Change the directory on the destination machine CONCATENATE 'cd' PRPATH INTO WA_COMMAND-LINE SEPARATED BY SPACE. APPEND WA_COMMAND TO IT_COMMANDS. * Change the local directory on the sap server CONCATENATE 'lcd' PLPATH INTO WA_COMMAND-LINE SEPARATED BY SPACE. APPEND WA_COMMAND TO IT_COMMANDS. CLEAR WA_COMMAND-LINE. IF PBIN EQ 'X'. MOVE 'bin' TO WA_COMMAND-LINE. ELSE. MOVE 'asc' TO WA_COMMAND-LINE. ENDIF. APPEND WA_COMMAND TO IT_COMMANDS. * get files from remote server CONCATENATE 'mget' PSTEM INTO WA_COMMAND-LINE SEPARATED BY SPACE. APPEND WA_COMMAND TO IT_COMMANDS.
ABAP Based Solution for FTP Operations (Send/Receive Files)
SAP COMMUNITY NETWORK SDN - sdn.sap.com | BPX - bpx.sap.com | BOC - boc.sap.com
This document may discuss sample coding or other information that does not include SAP official interfaces and therefore is not supported by SAP. Changes made based on this information are not supported and can be overwritten during an upgrade.
SAP will not be held liable for any damages caused by using or misusing the information, code or methods suggested in this document, and anyone using these methods does so at his/her own risk.
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, including any liability resulting from incompatibility between the content within this document and the materials and services offered by SAP. You agree that you will not hold, or seek to hold, SAP responsible or liable with respect to the content of this document.