Until the job comes from the customer, I think many ABAP developers superficially know the AL11 tcode. When I started to use it in detail, I noticed that the documents on this subject are outdated. Also, I couldn’t find a blog that explains the AL11 process step by step. In this blog post, I will try to share step-by-step information about AL11.
The “AL11” Tcode in the SAP system is a tool used to inspect and manage data and structures in the file system. This Tcode provides access to and the ability to manipulate data in the file system within the SAP system. AL11 Tcode is commonly used to inspect, backup, restore, and perform other file operations in the SAP system.
One of the biggest advantages of AL11 is the ability to process files via ABAP by storing them in the SAP system instead of locally saving files received from third-party systems.
Generally, three operations are carried out via AL11:
1) File Viewing
2) File Uploading
3) File Deletion
Let’s briefly talk about the “CG3Z” and “CG3Y” Tcodes, which are used for file operations in the SAP system. These Tcodes are commonly used for managing and processing data within the file system, including tasks such as file transfer, upload, download, and deletion. “CG3Z” is typically used for uploading files into the SAP system, while “CG3Y” is used for exporting files from the SAP system.
CG3Y and CG3Z:
EPS2_GET_DIRECTORY_LISTING is a function used in the SAP system to list the files in a specific directory within the file system. This function is used to retrieve a list of files located in the specified directory. EPS2_GET_DIRECTORY_LISTING is used to examine the contents of a specific directory in the file system and retrieve details such as the names, sizes, dates, and other properties of the files within that directory. This information allows users to manage and process files in file operations and data integration processes. The EPS2_GET_DIRECTORY_LISTING function is commonly used when performing file-based operations within the SAP system, serving as a useful tool for file management and processing.
DATA: lt_dir_list TYPE eps2filis .
DATA: lv_xstring TYPE xstring .
DATA: lv_mess TYPE string .
DATA(lv_path) = CONV eps2filnam( '\\test\path\' ). " Path in Al11
CONCATENATE lv_path sy-sysid '\in' INTO lv_path .
CALL FUNCTION 'EPS2_GET_DIRECTORY_LISTING' " this function reads file names from directory to table
EXPORTING
iv_dir_name = lv_path
TABLES
dir_list = lt_dir_list
EXCEPTIONS
invalid_eps_subdir = 1
sapgparam_failed = 2
build_directory_failed = 3
no_authorization = 4
read_directory_failed = 5
too_many_read_errors = 6
empty_directory_list = 7
OTHERS = 8.
IF sy-subrc <> 0 OR lines( lt_dir_list ) EQ 0.
WRITE 'Directory ist leer'.
ENDIF.
In ABAP programming language, the “OPEN DATASET” command is used for file operations. This command is used to open the specified file for reading, writing, or other operations. Here is how the “OPEN DATASET” command is used:
1. Opening a File:
OPEN DATASET <file_name> FOR INPUT|OUTPUT|APPENDING|... [IN LEGACY TEXT ENCODING|IN BYTE MODE] [MESSAGE <message_variable>].
- <file_name>: The name of the file to be processed.
- FOR INPUT: Open the file for reading.
- FOR OUTPUT: Open the file for writing. If the file does not exist, it is created; if it exists, it is overwritten.
- FOR APPENDING: Open the file for appending data at the end.
- IN LEGACY TEXT ENCODING: Opens the file with legacy text encoding.
- IN BYTE MODE: Opens the file in byte mode.
- MESSAGE <message_variable>: Allows returning a message based on the operation.
2. Closing a File:
CLOSE DATASET <file_name>.
- <file_name>: The name of the file to be closed.
3. Writing to a File:
TRANSFER <data> TO <file_name>.
- <data>: The data to be written to the file.
- <file_name>: The name of the file where the data will be written.
4. Reading from a File:
READ DATASET <file_name> INTO <data_variable>.
- <file_name>: The name of the file to be read.
- <data_variable>: The variable where the read data will be stored.
These commands are fundamental for file operations in the ABAP programming language and provide a wide range of functionality for file manipulation.
Let’s complete the explanation by providing a complete code example.
DATA: lt_dir_list TYPE eps2filis .
DATA: lv_xstring TYPE xstring .
DATA: lv_mess TYPE string .
DATA(lv_path) = CONV eps2filnam( '\\test\path\' ). " Path in Al11
CONCATENATE lv_path sy-sysid '\in' INTO lv_path .
CALL FUNCTION 'EPS2_GET_DIRECTORY_LISTING' " this function reads file names from directory to table
EXPORTING
iv_dir_name = lv_path
TABLES
dir_list = lt_dir_list
EXCEPTIONS
invalid_eps_subdir = 1
sapgparam_failed = 2
build_directory_failed = 3
no_authorization = 4
read_directory_failed = 5
too_many_read_errors = 6
empty_directory_list = 7
OTHERS = 8.
IF sy-subrc <> 0 OR lines( lt_dir_list ) EQ 0.
WRITE 'Directory ist leer'.
ENDIF.
LOOP AT lt_dir_list INTO DATA(ls_dir_file).
CLEAR : lv_xstring , lv_mess .
DATA(lv_file) = lv_path && '\' && ls_dir_file-name .
OPEN DATASET lv_file FOR INPUT IN BINARY MODE MESSAGE lv_mess .
READ DATASET lv_file INTO lv_xstring .
CLOSE DATASET lv_file .
APPEND VALUE #( file_name = ls_dir_file-name
file_content = lv_xstring
customer = 'CustomerName'
path = lv_path ) TO mt_files. " mt_files is coming from class as attribute
ENDLOOP.
In the code example above:
1) We retrieve the names of the files located in the directory ‘\\test\path\’ using the ‘EPS2_GET_DIRECTORY_LISTING’ function and store them in the table ‘lt_dir_list’.
2) By looping through the ‘lt_dir_list’ table, we retrieve the file names along with their respective directories.
3) We open, read, and close the file using the OPEN, READ, CLOSE DATASET commands, and after reading the content, we store it in ‘lv_xstring’.
4) Finally, we transfer the read data into a table.