Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Process every file in a directory

Former Member
0 Likes
968

Is there a way to "read" every file (csv) file in a directory on the server (a background job)? I will never know the number of files on the server during any given run. Is there a way to "loop" through a directory or bring the filenames into a table and loop through that table? Basically I will have many csv files, each one being an order, and I will be processing these files (orders) each night. I realize that I will need to use a dataset but how do I get to the files?

Davis

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
771

Hi,

Please check this sample code.


REPORT  ZDIRFILES.

PARAMETER: p_fdir type pfeflnamel DEFAULT '/usr/sap/tmp'.

data: begin of it_filedir occurs 10.
        include structure salfldir.
data: end of it_filedir.

START-OF-SELECTION.
* Get Current Directory Listing for OUT Dir
  call function 'RZL_READ_DIR_LOCAL'
    exporting
      name     = p_fdir
    tables
      file_tbl = it_filedir.

* List of files are contained within table it_filedir
  loop at it_filedir.
* Here you process the file individually
    write: / it_filedir-NAME.
    ...  

  endloop.

  ...

Regards,

Ferry Lianto

Is there a way to "read" every file (csv) file in a directory on the server (a background job)? I will never know the number of files on the server during any given run. Is there a way to "loop" through a directory or bring the filenames into a table and loop through that table? Basically I will have many csv files, each one being an order, and I will be processing these files (orders) each night. I realize that I will need to use a dataset but how do I get to the files?

Davis

3 REPLIES 3
Read only

Former Member
0 Likes
772

Hi,

Please check this sample code.


REPORT  ZDIRFILES.

PARAMETER: p_fdir type pfeflnamel DEFAULT '/usr/sap/tmp'.

data: begin of it_filedir occurs 10.
        include structure salfldir.
data: end of it_filedir.

START-OF-SELECTION.
* Get Current Directory Listing for OUT Dir
  call function 'RZL_READ_DIR_LOCAL'
    exporting
      name     = p_fdir
    tables
      file_tbl = it_filedir.

* List of files are contained within table it_filedir
  loop at it_filedir.
* Here you process the file individually
    write: / it_filedir-NAME.
    ...  

  endloop.

  ...

Regards,

Ferry Lianto

Read only

0 Likes
771

Ferry, that is beautiful! I could give you a hug for that!!!

A quick follow up. Can you move files and create directories in ABAP?

This would create the directory but what about moving/renaming a file?

call function 'GUI_CREATE_DIRECTORY'
     exporting
dirname = '//<ip_address>/qfilesvr400/<host>/usr/sap/TST/SYS/Folder1'
     exceptions
          failed  = 1
          others  = 2.

Davis.

Message was edited by:

Davis

Read only

Former Member
0 Likes
771

Hi,

You can use SYSTEM command/function.


DATA: UNIX_COMMAND(500).

DATA: BEGIN OF ITAB OCCURS 0,
        LINE(255),
      END OF ITAB.
                                                                        
CONCATENATE 'mv' SOURCE_FILE DEST_FILE INTO UNIX_COMMAND
                                       SEPARATED BY SPACE.
                                                                        
CALL 'SYSTEM' ID 'COMMAND' FIELD COMMAND ID 'ITAB' FIELD TABM-*SYS*.

Regards,

Ferry Lianto