Many times, we need to move an input file from one folder (say Input folder) to another folder (say Processed folder) after processing it. In such scenario in ABAP, we generally loop and read through the input file and transfer the input file data to the destination folder.
Demerit:
If the input file has large volume, then this process of moving the file from one folder to another by looping is time consuming and hits the performance of the system.
If the input file has large volume, then this process of moving the file from one folder to another by looping is time consuming and hits the performance of the system.
Another alternative is to create an External Operating System Command in SAP Transaction SM69 and use it in the program by calling the SAP standard FM ‘SXPG_COMMAND_EXECUTE’.
Demerit:
ABAPers usually do not have authorization to this Transaction SM69 and they need to depend on Basis/Security to get a new command created. Basis/Security in turn needs to have all the approvals in place to create these commands. Sometime due to audit issue, the external command cannot be created.
ABAPers usually do not have authorization to this Transaction SM69 and they need to depend on Basis/Security to get a new command created. Basis/Security in turn needs to have all the approvals in place to create these commands. Sometime due to audit issue, the external command cannot be created.
The smartest solution is to generate the UNIX Command in SAP ABAP programmatically and copy the input file instantly into the destination path.
Also Read: Working with Shell Scripts in SAP ABAP Environment
Sample code to write UNIX command in SAP ABAP
* This is a code showing how to create and use COPY command of UNIX in ABAP
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 | PARAMETERS: p_inputTYPElocalfile, p_procTYPElocalfile. * Declare the Types to file data *sys*(200), ' Please note, there are asterisk before and after sys (i.e.*sys*) DATAl_i_outputTYPESTANDARD TABLE OFl_x_outputWITHHEADERLINE. * * Variable for the UNIX command CONCATENATE'cp'p_inputp_proc * For example the Copy command is stored as below * cp ‘/data/interfaces/input/input_file’ ‘/data/interfaces/processed/processed_file’ *• mv filename1 filename2 --- moves a file (i.e. gives it a different name, or moves it into a *• cp filename1 filename2 --- copies a file * Execute the UNIX Copy command. * This command will copy the file from input file path to the processed file path ID'TAB' write:'File is copied successfully using UNIX command in ABAP'. |
Test Run:
Step1: Execute the above program
Step1: Execute the above program
Step2: File is copied successfully
Validation
Step3: Input file which is to be copied
Step4: File which is copied successfully
Benefit of this Tip
1) No need to loop through the file again to copy it to another path.
2) No need to depend on BASIS/Security team to get a new External Operating System Commands created in T Code SM69 and no need to call the standard FM to use that Command.
3) We can write all the commands which are recognized by UNIX.
Image source : www.netcmdlets.com
I have a file in my
D:
drive of my computer and I want to copy this file to an SAP application server so that I am able to see my file with transaction AL11.I know that I can create a file with AL11 but I want do this in ABAP.Of course in my search I find this code but I cannot solve my problem with it.
Jagger6,97077 gold badges3838 silver badges6868 bronze badges
rezareza
3 Answers
To upload the file to the application server, there are three steps to be followed. To open the file use the below statement:
Step1: OPEN DATASET file name FOR INPUT IN TEXT MODE ENCODING DEFAULT.
To write into the application server use.
Step2: TRANSFER name TO file name.
Dont forget to close the file once it is transferred.
Step3: CLOSE DATASET file name.
Plese mark with correct answer, if it helps! :)
Alexander BoosAlexander Boos
If you want to do this using ABAP you could create a small report that uses the function module
maillardmaillardGUI_UPLOAD
to get the file from your local disk into an internal table and then write it to the application server with something like this:45011 gold badge1010 silver badges2020 bronze badges
I used CG3Z transaction and with this transaction I was able to copy a file in the application server directory.
Jagger6,97077 gold badges3838 silver badges6868 bronze badges
rezareza
Not the answer you're looking for? Browse other questions tagged abap or ask your own question.
With A to Z of OLE Excel in ABAP 7.4 we explored a lot about excel file in SAP. GUI_DOWNLOAD/UPLOAD is also very straight forward. But whenever I get requirement to read multiple files from application server, do processing and store multiple files on another server… well it stressed me hard in the beginning. ?
The other day one junior askedme, when we have GUI_DOWNLOAD and UPLOAD, then why do we need to save file inAL11. I had to say one line, so that you can shut down your laptop and goto sleep and when you come in the morning the files would be ready foryou to review in AL11. He did not understand my abstract sentence and I amsure, many of you might also be wondering what I wrote.
Question: Why cannot we do GUI_DOWNLOAD/UPLOAD in background mode?
Answer: Jobs can be scheduled in background mode not only at the current time, but it can also be scheduled for future time. If you schedule a background job which does GUI_DOWNLOAD/UPLOAD, your program would dump or cancel out. The reason being, SAP did not want to take chance. GUI_DOWNLOAD/UPLOAD needs your laptop/computer to be switched on for it to save the data in the presentation server path. Say you scheduled a job which has GUI_DOWNLOAD/UPLOD and switched off your laptop and went to bed. When the job actually triggers, where will it save the file since your laptop is switched off? Do you get the catch 22 situation?
Therefore SAP came with Application File Concept (although not just for background job).
Now, let us do the same exercise. You have OPEN DATASET (for AL11) syntax in your program. You schedule a job. Switch off your laptop and go to sleep. In the middle of the night, the job triggers and it saves the file in Application Server (AL11). It is not dependent on your local laptop or desktop. It is at the Server level and Servers normally never sleep (except at planned outage or downtime). ?
Also Read: Test Driven Development ABAP
Although the File Directory (AL11) concepts are well known to all of us, we always Google for the syntax/tcode/FMs and use them without worrying much about the details and concepts behind the scene (especially lazy programmers like me). ?
So I thought of doing a favor to myself. Next time I need anything related to T-Code AL11 i.e. SAP Directories, I will not Google. Instead, I will come to this SAPYard page directly where I have listed almost everything we do in AL11. This should be a one stop page for not only all ABAP developers, but also the Basis Team and the Functional/Business team should also find it enriching.
In this article we will try to cover everything about AL11. What AL11 is all about? How to create folder in AL11? Ways to download files from AL11 and upload multiple files into AL11?How to delete files etc. As the title suggests, this is A to Z of all AL11 Operations. ?
1. What is AL11 and what is it’s use?
Well, AL11 is used for storing files/data on application server so that anyone (SAP) and even third-party systems (Non-SAP) can pick files from it. It is SAP File Directory as it contains directories, multiple folders and files in them. We can upload/download/modify/delete files/folders in t-code AL11.
Below is a quick lookof AL11 t-code.
It has two columns:
Name of Directory Parameter: Contains the directory names. It can contain multiple folders and files.
Directory: Contains the whole path of the folders.
2. How to create our own directory in AL11?
This is not supposed to be done by any ABAP developer and usually we will not get any authorization to perform this activity.
Just for sake of a complete document, I coaxed one of my Basis friends in this crime. See I have secret contacts in the SAP Administration and Security Team. My Basis friend was kind enough to provide the below steps. Now, I am wondering what would be the return price I need to pay for this favor. Just kidding. Tell me what you want. Open offer for you. ?
Hesaid it is no rocket science.
Directoriesare nothing but folders on Server System paths.
Basis folks and some privileged ones who have the authorization, log into the SAP Server System and create Folders and File. Just like how we create Folders and Files in our Windows/MAC Laptop and PC. As simple as that.
Now suppose we want to create a directory name as ‘ZMM_SOURCE’ on application server, Basis team will login in that Server System and create a folder with the same name which will be our directory.
Below is a screenshot of one such SAP Server System. It looks like normal Windows Folder. Right? But look closely. It is not part of the Windows operating system. It is another Server. If they create a folder in this Server, we can see that same folder in AL11 t-code in SAP.
Nowwe need to configure this directory in AL11.
There is an option in AL11 called name ‘Configure User Directories’
We need to click on that button and need to configure our directory.
Sap Abap Open Folder List
After execution, you can see your directory in AL11.
If you double click on this, you will see one file in it as I already created it.
That’s all. These directories are nothing but folders and as easy as right click and create new folder action of Windows. ?
Also Check – Free Video Course on HANA Models/Views
3. How to create new folder in directory in AL11?
This is also not partof ABAP work. Only Basis teams have authorization to create it. And this isnothing but creating new subfolder under our main folder in application server.Whereas main folder works like directory and subfolder works like folders/subdirectoriesin AL11.
Whatever locations on server system, where we are creating folders are called as Physical Paths of directories.
If main folder/directory is already configured then there is no need to configure the sub directory again after addition of subfolders. Those would be displayed in AL11 directory path automatically.
4. How to write/upload files on specific folder of AL11?
There are 2 ways toupload the file in AL11:
i) Manually via T-Code CG3Z:
After hitting this T-Code CG3Z, you will get below screen.
Source file on front end: Select your file from your laptop or desktop system and put it in this parameter.
Target file on application server: Provide path of file location in the AL11 directory.
Overwrite file: If it is checked, whatever file name you provided in target file parameter, if it exists in AL11, it will be overwriting with provided file else it will create new file there.
I have provided below details:
And got below message after clicking on the uploadbutton:
Let’s check in AL11 folder now. We can see our file test.xls is created here.
Note: We can ONLY read Doc or Txt files directly by double clicking on the file in AL11 but we can’t read XLS or XLSX files directly. XLS File will give below error.
This error comes because .XLS is a proprietary format of Microsoft. So, unless you have some addons installed on SAP Server, you can’t read XLS files from Application Server.
Do not worry. That is not the end of the world. If you really want to read the XLS file, you can download the file to your local machine (laptop/desktop) and then you can open and read it. ?
This brings us to question. How to downloadAL11 files? Please be patient. It will be covered in the later sections in thisarticle.
Here we are still covering how to createFolders/Files in AL11. Let’s look the second way.
ii) By writing ABAP code:
As an ABAP Programmer, usually we do not play with t-codes. We want everything should be done by our code. That gives us more sense of Power. ?
Sometimes we get requirement to upload multiple files on application server in a single go with some conditions. Let us look into one practical example:
Suppose I have a requirement to collect datafrom EKKO and EKPO tables and prepare multiple files for each new PO (EBELN)and store them on AL11 path. Each file should have PO number as file_name.
Sounds interesting yeah… Let’s do this…
First, we will select data from EKKO and EKPOtables and stores them in one internal table.
2 4 6 8 10 12 14 16 18 20 22 24 | <code>***Popoluatetables bukrs, bsart FROMekko WHEREebelnLIKE'45%'. IFgt_ekko[]ISNOTINITIAL. SELECTebeln, matnr, INTOTABLE@DATA(gt_ekpo) FORALLENTRIESIN@gt_ekko ORDER BYPRIMARY KEY. ENDIF.</code> |
There is a table called FILEPATH.
All folders of an application server are stored in the database table FILEPATH. We can pass our directory/folder name there and can confirm whether the folder exists or not.
2 4 6 8 | <code>CONSTANTS:lc_filenameTYPEfilepath-pathintern FROMfilepath WHEREpathintern=@lc_filename.</code> |
As we want to upload multiple files in our folder, we need to call FM FILE_GET_NAME_USING_PATH to get full path of that file and then can open it with Dataset concept:
2 4 6 8 10 12 | <code>CALL FUNCTION'FILE_GET_NAME_USING_PATH' logical_path=lv_logical_path IMPORTING EXCEPTIONS missing_parameter=2 file_system_not_found=4 |
Now using OPENDATASET, we can open the file to write fetched data from EKPO into it.
2 | <code>'Open the file to write. OPENDATASETlv_filepathFOROUTPUTINTEXT MODEENCODINGDEFAULT.</code> |
Mode ‘Output’ is usedfor writing data in application server file (all dataset concepts are explainedin the 5th point below).
Now using TRANSFER andCLOSE DATASET concept, we can pass all relevant data to corresponding files,save them and take exit from the files.
Whole executable codeis provided below:
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 | <code>REPORTzal11_working. CONSTANTS:lc_eTYPEcVALUE'E', lc_textTYPEstringVALUE'No specific data found for the input', lc_msgTYPEstringVALUE'The batch data is/are stored at the directory successfully', lc_filenameTYPEfilepath-pathinternVALUE'ZMM_BATCH_DATA_FOR_ASPECT'. DATA:lv_filepathTYPEstring. *** Popoluate tables bukrs, bsart FROMekko WHEREebelnLIKE'45%'. IFgt_ekko[]ISNOTINITIAL. SELECTebeln, matnr, INTOTABLE@DATA(gt_ekpo) FORALLENTRIESIN@gt_ekko ORDER BYPRIMARY KEY. ENDIF. MESSAGElc_textTYPElc_sDISPLAY LIKElc_e. CONSTANTS:lc_filenameTYPEfilepath-pathinternVALUE'ZMM_BATCH_DATA_FOR_ASPECT'. SELECTSINGLEpathintern INTO@DATA(lv_logical_path) LOOP ATgt_ekpoASSIGNINGFIELD-SYMBOL(<fs_ekpo>). AT NEWebeln. 'Logic to create unique file name with each PO number. CONDENSElv_file_postNO-GAPS. CALL FUNCTION'FILE_GET_NAME_USING_PATH' logical_path=lv_logical_path IMPORTING EXCEPTIONS missing_parameter=2 file_system_not_found=4 OPENDATASETlv_filepathFOROUTPUTINTEXT MODEENCODINGDEFAULT. 'Set intensity of the background color to default. DATA(lv_file)=|EBELN,EBELP,MATNR,BUKRS|. 'set non-intensified background color. TRANSFERlv_fileTOlv_filepath. CLEARlv_file. lv_file=|{<fs_ekpo>-ebeln},{<fs_ekpo>-ebelp},{<fs_ekpo>-matnr},{<fs_ekpo>-bukrs}|. 'Remove spaces. TRANSFERlv_fileTOlv_filepath. AT END OFebeln. CLOSEDATASETlv_filepath. MESSAGElc_msgTYPElc_s. CLEARlv_filepath. ENDLOOP. |
By using COMMAND-BREAK statements (AT END OF), I have achieved to create new file for new PO. The output is likebelow:
As shown above, theseare different files with different POs. If you open any file, you can getheader text line as well as values of cells.
Also Take – End to End Debugging Course for SAP Functionals
5. How to read/download files from AL11?
Above we learned how to write to AL11 file. Now let’s learn how to download them. Here we will discuss 4different approaches:
i) Download via T-Code CG3Y:
After hitting thistcode, you will see below screen:
- Source file on application server: Provide the path of file location from AL11 directorywhich you need to download.
- Target file on front end: Provide the file name from your system (can be new file) andput it in this parameter.
- Overwrite file: Ifit is checked, whatever file name you provided in target file parameter, if itexists in system, it will be overwriting with provided file else it will createnew file there.
- Transfer format for data: Usually ASC format works. Do check your requirement. Youmight need BIN as well.
As I wanted to save/download test excel filefrom application server to my desktop, I have provided below details:
It will create testfile on the desktop and if it is already present then will overwrite it.
ii) Download using list menu:
If the file is not so long, you can directly download the content from AL11. Navigate to the file.Then Open the file. On System Menu, go to List->Save/Send/Local file:
Question: Why is my AL11 exported file truncated?
Answer: In some case, the exported file is truncated. For List export in AL11, the limit is 512 characters. Apply SAP Note 952766 to increase the limit in AL11.
iii) By using FM ARCHIVFILE_SERVER_TO_CLIENT:
The standard Function module ARCHIVFILE_SERVER_TO_CLIENT can be used for downloading file from AL11. Just provide the PATH:the full path to the file on SAP server and the TARGETPATH: the destinationpath.
iv) By writing ABAP code:
Now if we havecomplex requirement like reading/downloading multiple files and do some calculationand massaging of data from the AL11 file then we need to create an ABAPprogram.
For downloading thefile from application server, we must know the below concepts:
a) OPEN DATASET:
2 4 | <code>OPENDATASETdsetFORaccessINmode[position] [error_handling].</code> |
Dset: Containsthe file path of AL11.
Access:It has below options –
- Input: It opens the file for reading. If the file does not exist, the sy-subrc is set to 8.
- Output: Opens the file for writing. If the file exists, the content will be deleted and replaced. If the file does not exist, the file will be created.
- Appending: It opens the file for appending. The new content will be added to the end of existing file or the file will be created if it does not exist. SY-SUBRC = 4 will be triggered if appending is not successful.
- Update: Itopens the file for changes to the existing content.
Mode: You can specify which mode you want to read the file likebinary or text.
ii) TRANSFER DATASET: This statement passes the content of data object dobj tothe file specified in dset.
Syntax:
2 | <code>TRANSFERdobjTOdset[LENGTHlen] |
The file must be openfor writes, appends, or changes. If a closed or invalid file is accessed, ahandleable exception is raised.
iii) CLOSE DATASET: This statement closes the file specified in dset.
Syntax:
2 | <code>CLOSEDATASETdset.</code> |
If the file isalready closed or does not exist, the statement is ignored and the return codesy-subrc is set to 0.
An opened file that was not explicitly closed using CLOSEDATASET is automatically closed when the program is exited.
Did I bore you to death with too much of theories?? ufff. Even I am tired, so I can understand your plight. ?
Let’s bring somecharm to our learning with the below examples:
In the point 4 above, we created multiple files on our application server now. Let’s read them all. Here I have passed only one file for demonstration. You can make it dynamic as well by passing all file names:
2 4 | <code>DATA:v_fileLIKEv_excel_stringVALUE'E:InterfacesZMM_BATCH_DATA_FOR_ASPECT4500000001.csv'.' name of the file *--- read the file from the application server OPENDATASETv_fileFORINPUTINTEXT MODEENCODINGDEFAULT.</code> |
Open the file in READmode. We can read each line from the file in a loop and split the data based ondelimiter to separate the data and store in internal table:
2 4 6 8 10 12 14 16 18 20 | <code>DO. IFsy-subrcISINITIAL. SPLITwa_tabATdelimiterINTOTABLEit_string. LOOP ATit_stringASSIGNINGFIELD-SYMBOL(<fs>). ASSIGN COMPONENTsy-tabixOFSTRUCTUREwa_tab2TOFIELD-SYMBOL(<fs_f>). <fs_f>=<fs>. AT LAST. ENDAT. EXIT. ENDDO.</code> |
After receiving thedata in an internal table, we can CLOSE the file using CLOSE DATSET syntax:
2 | <code>CLOSEDATASETv_file.</code> |
Here I have used ALV factory method to display the results in ALV format. Below is the whole executable program:
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 | <code>REPORTzal11_working. *--- Type structure declaration. ebelnTYPEstring, matnrTYPEstring, END OFty_tab1. *--- work area and internal table declaration. wa_tabTYPEalsmex_tabline, wa_tab2TYPEty_tab1, DATA:v_excel_string(2000)TYPEc, v_fileLIKEv_excel_stringVALUE'E:InterfacesZMM_BATCH_DATA_FOR_ASPECT4500000001.csv',' name of the file t_field(1000)TYPEcOCCURS0.' delimiter with default value space *--- Factory method objects lr_columnstyperef tocl_salv_columns_table, OPENDATASETv_fileFORINPUTINTEXT MODEENCODINGDEFAULT. WRITE:/'error opening file'. DO. IFsy-subrcISINITIAL. SPLITwa_tabATdelimiterINTOTABLEit_string. LOOP ATit_stringASSIGNINGFIELD-SYMBOL(<fs>). ASSIGN COMPONENTsy-tabixOFSTRUCTUREwa_tab2TOFIELD-SYMBOL(<fs_f>). <fs_f>=<fs>. AT LAST. ENDAT. EXIT. ENDDO. DELETEit_tab2INDEX1.'Delete header line. IMPORTINGr_salv_table=lr_table lr_column?=lr_columns->get_column('EBELN'). lr_column->set_short_text('EBELP'). lr_column?=lr_columns->get_column('MATNR'). lr_column->set_short_text('BUKRS'). catchcx_salv_not_found.'#EC NO_HANDLER |
After its execution,you will get below screen:
Also Read: How I created my First OData Service in SAP
6. How to delete files from AL11?
After all you would not want to keep the test files in the application server after testing. So, to delete all unwanted files, you can use below FM: EPS_DELETE_FILE
It has below structures init:
Enter the File name in theFILE_NAME and the directory path (Excluding the file name) in the DIR_NAME andexecute.
After execution, the file will disappear permanently from the AL11 location. As Spiderman’s uncle said, “With more Power comes more Responsibility“. So please use it judiciously.
This FM we can use at program level as well if required to delete files in mass.
We can use DELETE DATASET statement too at program level to delete files from application server.
Syntax:
2 | <code>DELETE DATASETdset.</code> |
7. Standard SAP Report to upload and download files?
Yes, you read it right. SAPhas already provided a report which is used for uploading file frompresentation server to application server and vice versa. Awesome yeah ✌
Report name: CACS_FILE_COPY
When you execute this report,you will get below selection screen.
Now I wanted to store a fileon application server from my desktop:
After successful execution,we can get below pop-up message:
Now let’s check in AL11:
Ohh yeah!!. So easy and gotthe file too in AL11.
8. Get all files from the directory/folder of application server?
If you want to know what filesare present in any AL11 folder, then you can use below 2 FMs:
EPS_GET_DIRECTORY_LISTING or EPS2_GET_DIRECTORY_LISTING
Let’s see how it works.
Here, we have to providedirectory/folder path to get list of files present in it.
As you see in above snapshot, we got 62 files of our directory. If you check the DIR_LIST table, you will get below details:
This functionality you canuse in the program whenever you want to do mass processing like readfile/download file/delete file etc from single directory. It indeed saves timeand good for performance.
Well, what else you want to do with AL11? I am blank now. Hell tired. Honestly, I don’t think you will need to search any other thread in future for any queries related to AL11. This page should be mother of all pages for AL11 and one source of truth for all AL11 requirements.
If I learn something newabout AL11, I will come back and update this page.
Do you have any trick or tipto share for AL11? Feel free to email us at [email protected] [email protected]. Or you mayalso leave your findings in the comment section below.
Sharing is Caring. Would you mind sharing the link so that it reaches maximum audience? And our SAP Fraternity can benefit from our work.
If you want to have real time discussions and resolutions, do join our SAP Technical Telegram Group where we have more than 4885+ active SAP consultants from 6 Continents.
Please Note: You need to install Telegram App on your mobile first and then you can join the group using the above link.
Please SUBSCRIBE to SAPYard’s Youtube Channel for Free End to End SAP Video Course and Training.
Free SAP ABAP for HANA Training Tutorials Exercises.
In this tutorials, you will learn what are the important pre requisites to create an ABAP program in SAP and how to create SAP ABAP program step by step.
Pre-requisites to create SAP ABAP Program
- Name: The program name of SAP Abap should start with a letter Z or Y because, A to X letters are reserved for SAP programs.
- Title: Under title, you need to update the description of an ABAP program.
- Type: Type of program – Executable program, Include Program, Module Pool Program, etc.
- Package: It refers to folder, where the SAP Abap program can be stored and created.
In real time scenarios, the packages are maintained as per modules and sub modules and not for individual programs.
How to Create an ABAP Program in SAP
Step 1: Enter the transaction code “SE38” in the SAP command field.
Step 2: ABAP Editor – Initial Screen appears on the screen. Now enter the following details
- Program: Enter the name of program name in the given box.
- Sub Objects: Choose the source code in the sub-objects.
- Now click on create button to create a new SAP ABAP program.
Step 3: A screen “ABAP Program Attributes ZDEMO_ABAP_Program change” opens. Enter the following details.
- Title: Update the title of ABAP program
- Type: Update the type of program as “Executable Program” which can be tested independently.
- After entering all the details for ABAP program, click on save icon
Step 4: A pop up screen displays “create object directory entry ” click on the local object option.
In Real time, we never select as local object to save the ABAP program ( it is not transportable from development to quality and quality to production).
Now the ABAP Editor for the current program ZDEMO_ABAP_PROGRAM open as follows.
Write the simple program as follows.
After program source code is done, click on save icon or ctrl+s to save the ABAP program.
To check the ABAP source code syntax errors, press ctrl+ f2 or choose icon check
To activate the ABAP program, click on activate icon or ctrl+f3.
After activating the program, you get the following screen. Now press enter to continue
To run the ABAP program, press the function key F8 or click on the icon direct processing.
The output of an ABAP program displays as follows
Successfully we have created new abap program in SAP and executed.