Today i'll illustrate how to read and write files from an applications server on an SAP R/3 systems.
First some pitfalls:
- If you run a system with multiple application servers be very careful that the file system you are accessing is mounted on the servers your programs or transactions are running on.
- This seems trivial, but it happens very often that programs suddently fail at night, when they run on batch "systems", but they run just fine when run during the day.
- Be very careful using the "ASC"/"BIN" options in functions and transactions.
- Sometimes someone downloaded a file ( also via FTP ) and put it back by uploading it again. The ascii conversions had destroyed the file structure and the programs reading the file simply went nuts.
- Transactions CG3Y and CG3Z
- Just check them out: they are simple and effective, and are the ones used most of the times.
- Combined with AL11 to help us find out the directory structure.
- ABAP PROGRAMMING:
- OPEN DATASET / TRANSFER / CLOSE DATASET
- There are plenty of examples with these simple commands in the internet: just look them up in the abap help or google it.
- FUNCTION MODULES for example
- ARCHIVFILE_SERVER_TO_CLIENT & ARCHIVFILE_CLIENT_TO_SERVER
REPORT z_get_srv_file .Example 2: upload file to application server
PARAMETERS: file TYPE SAPB-SAPPFAD
default '/usr/sap/trans/data/R916228.PD1'.
PARAMETERS: lfile TYPE SAPB-SAPPFAD
default 'c:\R916228.PD1'.
START-OF-SELECTION.
CALL FUNCTION 'ARCHIVFILE_SERVER_TO_CLIENT'
EXPORTING
path = file
TARGETPATH = lfile
EXCEPTIONS
ERROR_FILE = 1
OTHERS = 2
.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
REPORT z_put_srv_file .
PARAMETERS: file TYPE sapb-sappfad
DEFAULT 'D:\usr\sap\trans\data\R916228.PD1'.
PARAMETERS: lfile TYPE sapb-sappfad
DEFAULT 'E:\R916228.PD1'.
START-OF-SELECTION.
CALL FUNCTION 'ARCHIVFILE_CLIENT_TO_SERVER'
EXPORTING
path = lfile
targetpath = file
EXCEPTIONS
error_file = 1
OTHERS = 2.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
Another important concept is the LOGICAL file : i'll write just a couple of lines to give the reader an idea and provide a code snippet or two.
Because of SAP R/3 running on different OS with different OS path syntax, SAP provided a means of hiding the actual path of a server file from the end user and programmer.
Transaction FILE allows you to customize phisical file names, in a pretty complicated fashion if you like (also specifing different path structures for different operating systems and the like)
Your phisical file name or path is associated to a LOGICAL file name or path.
The function "FILE_GET_NAME" does the dirty work of of creating the physical path using the informastion stored in the tables behind view cluster "FILE"
Here the code snippet:
A more real life example is the following:
DATA: FLAG,
FORMAT(3),
FNAME(60).
WRITE SY-OPSYS.
CALL FUNCTION 'FILE_GET_NAME'
EXPORTING
LOGICAL_FILENAME = 'MYFILE'
OPERATING_SYSTEM = SY-OPSYS
PARAMETER_1 = '01'
IMPORTING
EMERGENCY_FLAG = FLAG
FILE_FORMAT = FORMAT
FILE_NAME = FNAME
EXCEPTIONS
FILE_NOT_FOUND = 1
OTHERS = 2.
IF SY-SUBRC = 0.
WRITE: / 'Flag :', FLAG,
/ 'Format :', FORMAT,
/ 'Phys. Name:', FNAME.
ENDIF.
parameters: server like filename-fileintern default 'ZBLA'
lower case.
CALL FUNCTION 'FILE_GET_NAME'
EXPORTING
logical_filename = server
IMPORTING
file_name = p_file "lth_logic-fileintern
EXCEPTIONS
file_not_found = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE 'Errore' TYPE 'E'.
ENDIF.
OPEN DATASET p_file FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
Last thing: reading and writing from a presentation server (the user PC or terminal)
- HR_99S_UPLOAD ; HR_99S_DOWNLOAD
- GUI_DOWNLOAD ; GUI_UPLOAD.
Obviously these functions do not work in background.