Copyright 2024 - BV TallVision IT

 Types of files, reading and writing, filenames and file paths, getting a directory listing on Unix. Anything to do with actual files is available here.

A simple file editor could aid the interface process or allow easy changes/problem fixing on data that was e.g. delivered as an .XML file. This article holds a ready-to-use simple file editor where a file is opened, edited through the SO10 all standard text editor and stored again. 

No one will be surprised that SAP itself runs in a Unix environment. To read and write files originating from Unix, the statements READ DATASET and TRANSFER can be used. The ABAP data is placed in a certain structure or even in an internal table that can be written to the sequential file as a whole.

Would you like to make your software platform independant ? The Abap language doesn't have many platform dependent actions, however where file interfacing is concerned, alternative approaches may be required. Check out what platform you are working on (server-side and frontend).

CSV files are golden: they are the simplest form to pass data and every system can understand it. The first line holds the column names, data lines hold the field contents. As they are so common, I've devised a small local class that makes handling them a bit easier. Local class lcl_CSV_utility:

Unix and Windows/Dos are not aligned where text files are concerned - Unix systems have a single character end-of-line and Windows works with a 2-character end-of-line / carriage-return combination. Like the type-writer in the 40's. When your Unix file is to be processed in Windows or vice-versa, here's what you can do..

When using filenames (check out transaction FILE - client independant or SF01 - client dependant), you should also check out the standard SAP functionality for logical file paths and logical file names. It can be reached via transaction code FILE. A logical file path is a data record that contains a path name. A program would select the file path rather than use a hard-coded file path.

If you need to place a file on the server, or copy it from server to presentation server (PC), there are 2 very handy transactions available for you: CG3Z (from PC to backend) and CG3Y (from backend to PC). 

BTW: if it's text files that have long lines (more than 255 characters per line) - the ASC format will cut your data. Try BIN instead !

How can you get a directory listing from Unix/WindowsNT backend files ? Or presentation server files ? First of all: you'll need to differentiate between presentation server files and server files. Then the server type can be important, Unix or Windows NT ? Here's an overview of getting directory listings or file listings for each.

Transaction AL11 will allow you to browse through the system's backend files and directories. If you haven't seen this be sure to try it. The directories it presents originate from directory parameters, e.g. DIR_HOME would typically be the home directory like /usr/sap/DEV/DVSOMESYSTEMID/work. Would you like to use these directories without hardcoding them ? Here's how: the DIR_* parameters are in fact system parameters which can be viewed with RSPARAM.

There is a way to execute server commands in Unix, and there is one for the windows environment too. The SXPG_COMMAND_EXECUTE function module.

Sometimes the operating system will calculate the timestamp of a file or directory with the number of seconds that have passed. Great stuff or course, but how do you calculate with that ? Can it be converted into a date and time again ? Of course it can. 

If your interface produces files (whether it's inbound or outbound) - are the files removed ? Are you stumbling across old log files that have no use other than occupy space ? This article describes how a turn-key utility report can be used to schedule cleaning your (server) directories. 

Whenever a file name or path name is entered in a selection screen, it could be quite convenient if variables like {S} for system could be used. This is quite easy to implement, but I found that I was doing this in slightly deviating ways for several developments. Hence an example that can easily be incorporated in your coding. 

If the process that uses a file is not a background process, files on the presentation server can be addressed as well. Function modules GUI_UPLOAD and GUI_DOWNLOAD allow getting front end data or storing it. Of course these function modules will need a filename to store or retrieve with, which can be obtained with the class builder (trx SE24) for class CL_GUI_FRONTEND_SERVICES.. Maybe it's time to do your coding in Abap Objects ?

Sometimes the Byte Order Bitmark is required - as Code Page for files. The Byte Order Bitmark or BOM is a special character that is required at the beginning of a UTF-8 file. 

Both OPEN DATASET and funtion module GUI_DOWNLOAD operate with codepages, effectively the character-translation table for your character sets. The SAP system will have a default codepage in case no codepage is defined, so normally you won't know there is one and you won't need to specify one. But what if special characters don't show as expected in your downloaded file ?

EXtensible Markup Language or XML is a way of capturing data in a very readable manner to the human eye. Summarized:

  • XML is a markup language (much like HTML)
  • XML was designed to hold/carry data (not to display data)
  • XML tags are not predefined. You must define your own tags

You need to read a big PDF file which is handed to you by some webserver as some-one uploaded it. It contains a resume or maybe a pricelist that needs to be available from SAP. So how ? From the backend, there is a toolset available for you to accomplish this. Read on !

Files can be zipped and unzipped, compressed or un-compressed with class CL_ABAP_ZIP or class cl_abap_gzip. The format (for class CL_ABAP_GZIP is GZIP and this setup works for Text files and Binary files. Method COMPRESS_BINARY will pick up a RAW value (like RAWSTRING) and return it in condensed form. The same can be done in reverse and for texts.

Another way of storing is EXPORT and IMPORT, with the option TO DATASET (a sequential file) TO DATABASE ( (to a special SAP table) and even TO MEMORY, "to R/3 work memory". This last option can be used to pass data in between successive transactions. Releasing the reserved memory can be done with FREE MEMORY or DELETE FROM DATABASE.

An important point of difference in conversion programs is for example the format of the data to be read: the conversion program reads: READ DATASET .. IN BINARY MODE or IN TEXT MODE; a major difference in the sequential file structure: Files that are created in a BINARY MODE have a fixed record length, TEXT MODE files have a record boundary that is physically an ENTER, so one line per record.

Special characters that are placed in a file can get misinterpreted, because the world is expected to support 100.000+ characters, which are covered in so-called code pages. A code page is effectively the "translation table" for a set of characters. Some you you know ASCII or EBCDIC, which is code page league. 

The only way to ensure your file is not appended to while you are reading (e.g.) is by letting the operating system (Unix) do its magic, e.g. a rename. Unix statements or commands can be executed with the following C-routine:

CALL 'SYSTEM' 
  ID 'COMMAND' 
  FIELD 'mv /usr/sap/temporary
            /usr/sap/definite'.

If you don't handle things like this with care, serious trouble could be inflicted...

When working files for interfacing, it could be a requirement to FTP something to it's final (or semi final) destination. Often a basis theme, but it can also be arranged from an Abap: