Table of contents
Introduction
In this article, only general directions are given. You may find snippets here
Files are handled differently when they are on the application server, or on the presentation server
Files may be processed in 2 modes, either text (in a given encoding), or binary (generated by a software like Microsoft Office, Adobe Acrobat, etc.) From ABAP, you may directly write and read text files, while binary files can only be written and read by the software. The mode is chosen when opening the file.
Working with files on the application server
- Transaction code AL11 takes you to the application server in which you find the sap directories and the files inside the directories.
The configure button on the main screen can be used to create and manage links to existing directories. - Files may be processed in 2 ways, decided in OPEN DATASET statement: either as text files with a given encoding (IN TEXT MODE, ENCODING and CODE PAGE additions), or as binary files (IN BINARY MODE addition).
How to create or write to a file
Use ABAP statements OPEN DATASET with FOR OUTPUT keyword, TRANSFER, CLOSE DATASET.
How to read a file
Use ABAP statements OPEN DATASET with FOR INPUT keyword, READ DATASET and CLOSE DATASET.
Checking file existence
- either OCS_GET_FILE_INFO function module
- or "OPEN DATASET" ABAP statement
How to get list of files in a directory
To read the names of files present in a particular path on the application server we may use SUBST_GET_FILE_LIST or EPS_GET_DIRECTORY_LISTING function modules.
Is there a search help for selecting a file
- F4_DXFILENAME_TOPRECURSION function module may be used to display a search help to choose a file
- Snippet
Best practice
Instead of hardcoding the file names, it is possible to define generic names which are called "logical filenames". They may be defined using FILE transaction code, and used using FILE_GET_NAME function module.
Miscellaneous
- sdn contribution : ABAP Server Side File Access, by Thomas Jung (class ZCL_ES_SERVER_FILE_ACCESS)
- Note 752859: sapiconv - a tool for converting the encoding of files
Working with files on presentation server
How to create or write a file
- either GUI_DOWNLOAD function module
Snippet - or CL_GUI_FRONTEND_SERVICES=>GUI_DOWNLOAD method
Snippet - Note: DOWNLOAD and WS_DOWNLOAD function modules are obsolete
How to read a file
- either GUI_UPLOAD function module
Snippet - or CL_GUI_FRONTEND_SERVICES=>GUI_UPLOAD method
- Note: UPLOAD and WS_UPLOAD function modules are obsolete
Checking file existence
- either DX_FILE_EXISTENCE_CHECK function module
- or CL_GUI_FRONTEND_SERVICES=>FILE_EXIST method
How to get list of files in a directory
- either TMP_GUI_DIRECTORY_LIST_FILES function module
- or CL_GUI_FRONTEND_SERVICES=>DIRECTORY_LIST_FILES method
Is there a search help for selecting a file or directory
For selecting a *file", you may use:
- CL_GUI_FRONTEND_SERVICES=>FILE_OPEN_DIALOG method (Basis, 4.6 and after): displays list as a popup with title "Open", and returns the name of selected file
- CL_GUI_FRONTEND_SERVICES=>FILE_SAVE_DIALOG method (Basis, 4.6 and after): displays list as a popup with title "Save", and returns the name of selected file
Snippet - WS_FILENAME_GET function module (Basis, any release)
- F4_FILENAME function module (R/3 and ECC only)
For selecting a directory, you may use:
- CL_GUI_FRONTEND_SERVICES=>DIRECTORY_BROWSE method
Is it possible to work in background?
No, this functionality is not supplied because presentation servers may be switched off at any time without guarantee (see Note 7925 - Graphics, Upload, Download do not work in backgrnd).
Workarounds may be:
- Work with files on application server. The user may copy files from or to the application server when he is connected
- If you take decisions to make the presentation server work as a server (always the same computer, and it would be switched on), consider it is an RFC server: see SDN article Communication to Presentation Server in Background Mode for more information.
Miscellaneous
- CL_GUI_FRONTEND_SERVICES class provides many other methods about files
Copying from server to frontend or vice versa
- CL_GUI_FRONTEND_SERVICES=>FILE_COPY method
- SAP commander
- R/3 and ECC: transaction codes CG3Y (from server to frontend) and CG3Z (from frontend to server)
File compression (zip)
- Class CL_ABAP_ZIP may be used to read or write a zipped file.
- On the application server, OPEN DATASET statement may be used with FILTER addition to compress or uncompress
Note: there is also class CL_ABAP_GZIP, but it can't be used to process files, it may just be used to compress or uncompress data in memory.
Working with special files
Excel file types
See Excel file types (CSV, XML, XSLX, OLE)
OLE/DOI for Office native files (Excel, Word, etc.)
- When a user is connected to SAP via SAPGUI for Windows, you may use OLE ABAP statements (see ABAP documentation), or Desktop Office Integration framework (it also uses OLE).
- It is not possible to use OLE or DOI in a program disconnected from SAPGUI (background job for example).
- It is possible to create Office documents using XML files
5 Comments
Saifur Rahaman
My SIP Project In Tata Steel worked out based on this concept..thnx a lot for keep us update about small but important things
Unknown User (szpe7do)
Do not use SUBST_GET_FILE_LIST! In BASIS 701, SAP changes the code to a single line:
raise access_error.
You won't get very far with that.
Custodio de Oliveira
To read a file from application server in one shot: CL_RSAN_UT_APPSERV_FILE_READER=>APPSERVER_FILE_READ
To write: CL_RSAN_UT_APPSERV_FILE_WRITER=>APPSERVER_FILE_WRITE
Former Member
This document and the code snippet section is very helpful. But the FM is given for F4 help for Application Server file is
F4_DXFILENAME_4_DYNP
. I tried use this FM but it is not working for my case. For this requirement /SAPDMC/LSM_F4_SERVER_FILE FM can be used.Former Member
Issue with this article, please do NOT recommend using function module EPS_GET_DIRECTORY_LISTING. It is an SAP projects(not PS) function module which requires Projects authorizations which are related to configuration and NOT related to business processes.