2011年6月14日 星期二

Working with files



Added by Sandra Rossi , last edited by Sandra Rossi on Nov 04, 2009 (view change) SHOW COMMENT
Labels: (None)

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.
http://wiki.sdn.sap.com/wiki/download/attachments/33030193/AL11.jpg
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).
a.How to create or write to a file
Use ABAP statements OPEN DATASET with FOR OUTPUT keyword, TRANSFER, CLOSE DATASET.
a.How to read a file
Use ABAP statements OPEN DATASET with FOR INPUT keyword, READ DATASET and CLOSE DATASET.
a.Checking file existence
  • either OCS_GET_FILE_INFO function module
  • or "OPEN DATASET" ABAP statement
a.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.
a.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
a.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.

Working with files on presentation server

b.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
b.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
b.Checking file existence
either DX_FILE_EXISTENCE_CHECK function module or CL_GUI_FRONTEND_SERVICES=>FILE_EXIST method
b.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
b.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
b.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.
b.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