Application Development 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: 

How to read multiple files from a given directory

Former Member
0 Kudos

Hi,

The only input I have is a path, and I need to read all TXT files from this directory. How can I do this ? There is no user interaction to manually select the files, so OPEN_FILE_DIALOG from CL_GUI_FRONTEND_SERVICES doesn't help.

Thanks in advance,

Avraham

1 ACCEPTED SOLUTION

former_member387317
Active Contributor
0 Kudos

Hi Avraham Kahana,

CL_GUI_FRONTEND_SERVICES=>FILE_OPEN_DIALOG

pass parameter

MULTISELECTION = 'X'

so in FILE_TABLE (IFILE internal table) you will get list of files you have selected through file browser...

then read those files using the file path... one by one using table entries of IFILE TABLE...

CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_OPEN_DIALOG
    EXPORTING
      WINDOW_TITLE            = 'Local File for Conversion'
*       default_extension       =
*       DEFAULT_FILENAME        =
      FILE_FILTER             = CL_GUI_FRONTEND_SERVICES=>FILETYPE_TEXT " DAT or TXT etc
      INITIAL_DIRECTORY       = 'C:'
       MULTISELECTION          = 'X'
    CHANGING
      FILE_TABLE              = IFILE
      RC                      = RC
      USER_ACTION             = ACTION
    EXCEPTIONS
      FILE_OPEN_DIALOG_FAILED = 1
      CNTL_ERROR              = 2
      ERROR_NO_GUI            = 3
      OTHERS                  = 4

Hope it will solve your problem..

Thanks & Regards

ilesh 24x7

ilesh Nandaniya

8 REPLIES 8

former_member387317
Active Contributor
0 Kudos

Hi Avraham Kahana,

CL_GUI_FRONTEND_SERVICES=>FILE_OPEN_DIALOG

pass parameter

MULTISELECTION = 'X'

so in FILE_TABLE (IFILE internal table) you will get list of files you have selected through file browser...

then read those files using the file path... one by one using table entries of IFILE TABLE...

CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_OPEN_DIALOG
    EXPORTING
      WINDOW_TITLE            = 'Local File for Conversion'
*       default_extension       =
*       DEFAULT_FILENAME        =
      FILE_FILTER             = CL_GUI_FRONTEND_SERVICES=>FILETYPE_TEXT " DAT or TXT etc
      INITIAL_DIRECTORY       = 'C:'
       MULTISELECTION          = 'X'
    CHANGING
      FILE_TABLE              = IFILE
      RC                      = RC
      USER_ACTION             = ACTION
    EXCEPTIONS
      FILE_OPEN_DIALOG_FAILED = 1
      CNTL_ERROR              = 2
      ERROR_NO_GUI            = 3
      OTHERS                  = 4

Hope it will solve your problem..

Thanks & Regards

ilesh 24x7

ilesh Nandaniya

0 Kudos

you didn't read what I wrote

there is no user interaction to select the files, so this method doesn't help me.

0 Kudos

Hi,

Check this link

[read multiple files from a given directory|]

0 Kudos

You could use CALL METHOD cl_gui_frontend_services=>DIRECTORY_LIST_FILES to get the list of all the files in a directory and use any upload FM's to read the multiple files

venkat_o
Active Contributor
0 Kudos

Hi Avraham, Try this way.


 REPORT ztest_program.
 DATA: wa_pfile TYPE file_table.
 DATA: it_pfiles TYPE STANDARD TABLE OF file_table,
      count   TYPE i,
      dir     TYPE string VALUE 'C:\temp\'.
 CALL METHOD cl_gui_frontend_services=>directory_list_files
   EXPORTING
     directory                   = dir
     files_only                  = 'X'
   CHANGING
     file_table                  = it_pfiles
     count                       = count
   EXCEPTIONS
     cntl_error                  = 1
     directory_list_files_failed = 2
     wrong_parameter             = 3
     error_no_gui                = 4
     not_supported_by_gui        = 5
     OTHERS                      = 6.
 LOOP AT it_pfiles INTO wa_pfile.
   WRITE wa_pfile.
 ENDLOOP.
Thanks Venkat.O

SuhaSaha
Advisor
Advisor
0 Kudos

>

> The only input I have is a path, and I need to read all TXT files from this directory. How can I do this ? There is no user interaction to manually select the files, so OPEN_FILE_DIALOG from CL_GUI_FRONTEND_SERVICES doesn't help.

Hello Avraham,

As suggested you can use CL_GUI_FRONTEND_SERVICES=>DIRECTORY_LIST_FILES.

But for selecting .TXT files only you need to add a small trick


  DATA: IT_EGLIST TYPE STANDARD TABLE OF FILE_TABLE,
        V_EGCNT   TYPE I.
CALL METHOD CL_GUI_FRONTEND_SERVICES=>DIRECTORY_LIST_FILES
    EXPORTING
      DIRECTORY                   = V_DIR
      FILTER                      = '*.TXT'
      FILES_ONLY                  = 'X'
    CHANGING
      FILE_TABLE                  = IT_EGLIST
      COUNT                       = V_EGCNT
    EXCEPTIONS
      CNTL_ERROR                  = 1
      DIRECTORY_LIST_FILES_FAILED = 2
      WRONG_PARAMETER             = 3
      ERROR_NO_GUI                = 4
      NOT_SUPPORTED_BY_GUI        = 5
      OTHERS                      = 6.
  IF SY-SUBRC = 0.
  ENDIF.

0 Kudos

worked fine

Former Member
0 Kudos

Hi,

Try this link

[;

[;

Regards,

Pydi Reddy.

Edited by: pydi reddy on Oct 14, 2009 11:26 AM