✒️ABAP Los archivos en el servidor y las sentencias OPEN y READ
ABAP Los archivos en el servidor y las sentencias OPEN y READ
File Processing on the Server
In a real SAP work environment, utilizing an application server for file operations is common. This is primarily due to two key reasons:
- It's common for the input of a process running in the SAP system to be the output of another process, so there is a need to have a centralized work in an environment where files are stored in shared folders accessible to various company resources.
- Sometimes, files generated by SAP system processes are too large to be stored locally on computers.
ABAP provides a set of statements for handling files on the application server. The main statements for file management on the application server are:
- OPEN DATASET: for file opening.
- READ DATASET: for reading files on the server.
- TRANSFER: for transferring data from files to the server.
- DELETE: for deleting files from the server.
- CLOSE: for closing files on the server.
Using transaction AL11, we can access SAP directories on the server and even open files. By entering the transaction, all available directories are visible. Double-clicking on any directory reveals its files, and if they are text files, their contents can be viewed with another double-click. Creating a new directory typically requires permissions from SAP system administrators.
Opening Files on the Server
To access the files on the server the OPEN DATASET statement can be used for file opening. The syntax is as follows:
OPEN DATASET <file> FOR <ACCESS> IN <MODE>.
- File: Specify the file name along with the directory. This information can be entered as a literal in single quotes or in a variable. If no location is provided, it will attempt to find it on the application server.
- Access: Specifies the opening method, either reading or writing.
- Mode: Indicates the file's opening mode, either text or binary.
If the file opens successfully, the system variable SY-SUBRC will be "0"; otherwise, it will be “8”.
OPEN DATASET 'example.txt' FOR INPUT IN TEXT MODE.
Access Statements
- Reading Files: To open a file for reading, use the FOR INPUT clause as follows:
OPEN DATASET <file> FOR INPUT IN TEXT MODE.
OPEN DATASET 'read_file.txt' FOR INPUT IN TEXT MODE.
The file must exist; otherwise, SY-SUBRC will be 8. If the file exists and is open, the system positions itself at the beginning of the file. It's advisable to close all open files before reopening with the OPEN DATASET statement.
- Writing Files: To open a file for writing, use the FOR OUTPUT clause as follows:
OPEN DATASET <file> FOR OUTPUT IN TEXT MODE.
OPEN DATASET 'write_file.txt' FOR OUTPUT IN TEXT MODE.
If the file does not exist, it is created automatically. If the file exists but is closed, the content is overwritten. If the file exists and is open, the system positions itself at the beginning of the file.
- Appending Information to Files: To open a file for appending information, use the FOR APPENDING clause as follows:
OPEN DATASET <file> FOR APPENDING IN TEXT MODE.
OPEN DATASET 'append_info_to_file.txt' FOR APPENDING IN TEXT MODE.
If the file does not exist, it is created automatically. If the file exists but is closed, the system opens it and positions itself at the end. If the file exists and is open, the system positions itself at the end of the file.
In all the above cases, SY-SUBRC is always "0".
There are two file opening modes on the SAP application server:
Mode Statements
- TEXT MODE: To open a file in text mode, use the IN TEXT MODE clause. When reading or writing in text mode, use the reserved word ENCODING DEFAULT. Example:
OPEN DATASET <file> FOR INPUT IN TEXT MODE ENCODING DEFAULT.
OPEN DATASET 'input_file_text_mode.txt' FOR INPUT IN TEXT MODE ENCODING DEFAULT.
- BINARY MODE: To open a file in binary mode, use the IN BINARY MODE clause. When reading or writing in binary mode, data is transferred byte by byte. Example:
OPEN DATASET <file> FOR INPUT IN BINARY MODE.
OPEN DATASET 'input_file_binary_mode.txt' FOR INPUT IN BINARY MODE.
Reading Files from the Server
We'll use the READ DATASET statement for reading files from the SAP application server. The syntax is as follows:
READ DATASET <file> INTO <structure>.
To determine the variable for storing the read data, understand the structure of the file to be read. If the reading is successful, SY-SUBRC is "0"; otherwise, it is 8.
E.g
*&---------------------------------------------------------------------*
*& Report ZTEST_ABAP_JEGA_23
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ztest_abap_jega_23.
DATA: file TYPE string VALUE '/usr/sap/trans/usuarios.txt',
wa_structure TYPE string,
it_data LIKE TABLE OF wa_structure.
OPEN DATASET file FOR INPUT IN TEXT MODE ENCODING DEFAULT.
DO.
READ DATASET file INTO wa_structure.
IF sy-subrc = 0.
APPEND wa_structure TO it_data.
ELSE.
EXIT.
ENDIF.
ENDDO.
CLOSE DATASET file.
cl_demo_output=>display_data( it_data ).
 
 
 
Sobre el autor
Publicación académica de Jaime Eduardo Gomez Arango, en su ámbito de estudios para la Carrera Consultor ABAP.
Jaime Eduardo Gomez Arango
Profesión: Ingeniero de Sistemas y Computación - España - Legajo: SW34C
✒️Autor de: 99 Publicaciones Académicas
🎓Cursando Actualmente: Consultor ABAP Nivel Avanzado
🎓Egresado del módulo:
Disponibilidad Laboral: FullTime
Presentación:
Ingeniero de sistemas y computación con 8 años de experiencia el desarrollo frontend & backend (react/node) y en cloud (aws), actualmente desarrollando habilidades en sap btp, ui5, abap y fiori.
Certificación Académica de Jaime Gomez