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 n number of files from application server

Former Member
0 Kudos

Hi Experts,

I have to read n number of files from a specific directory in the application server.

Plz let me know how to do that.

Thanks,

kumar

1 ACCEPTED SOLUTION

ferry_lianto
Active Contributor
0 Kudos

Hi,

Welcome to SDN.

Please check this sample code.


REPORT ZDIRTEST.

PARAMETER: P_DIR TYPE RSMRGSTR-PATH DEFAULT '/usr/sap/trans/data' LOWER CASE.

DATA: WA_FILES  TYPE RSFILLST,
      IT_FILES  LIKE TABLE OF WA_FILES,
      WA_LINES  TYPE I.
                                                                        
START-OF-SELECTION.
                                               
  CALL FUNCTION 'SUBST_GET_FILE_LIST'
    EXPORTING
      DIRNAME      = P_DIR
      FILENM       = '*'
    TABLES
      FILE_LIST    = IT_FILES
    EXCEPTIONS
      ACCESS_ERROR = 1
      OTHERS       = 2. 

  IF  SY-SUBRC  <>   0.
    WRITE: / 'Error reading files'.
  ELSE.
    DESCRIBE TABLE IT_FILES LINES WA_LINES.
    WRITE: / 'Numbers of files:', WA_LINES.
    LOOP AT IT_FILES INTO WA_FILES.
      WRITE: / WA_FILES-NAME.
    ENDLOOP.
  ENDIF.

Regards,

Ferry Lianto

2 REPLIES 2

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

How about something like this.



report zrich_0001 .


data: begin of itab occurs 0,
      rec(1000) type c,
      end of itab.
data: wa(1000) type c.

data: p_file type localfile.
data: ifile type table of  salfldir with header line.

parameters: p_num type i.
parameters: p_path type salfile-longname
                    default '/usr/sap/TST/DVEBMGS01/data/'.


call function 'RZL_READ_DIR_LOCAL'
     exporting
          name           = p_path
     tables
          file_tbl       = ifile
     exceptions
          argument_error = 1
          not_found      = 2
          others         = 3.

loop at ifile.

* Only read the allowed number of files.
  if sy-tabix > p_num.
    exit.
  endif.

* Build path
  concatenate p_path ifile-name into p_file.

* open, and read dataset, append to itab and close
  clear itab.  refresh itab.
  open dataset p_file for input in text mode.
  if sy-subrc = 0.
    do.
      read dataset p_file into wa.
      if sy-subrc <> 0.
        exit.
      endif.
      itab-rec = wa.
      append itab.
    enddo.
  endif.
  close dataset p_file.

* Write out itab.
  loop at itab.
    write:/ itab.
  endloop.

endloop.

Regards,

Rich Heilman

ferry_lianto
Active Contributor
0 Kudos

Hi,

Welcome to SDN.

Please check this sample code.


REPORT ZDIRTEST.

PARAMETER: P_DIR TYPE RSMRGSTR-PATH DEFAULT '/usr/sap/trans/data' LOWER CASE.

DATA: WA_FILES  TYPE RSFILLST,
      IT_FILES  LIKE TABLE OF WA_FILES,
      WA_LINES  TYPE I.
                                                                        
START-OF-SELECTION.
                                               
  CALL FUNCTION 'SUBST_GET_FILE_LIST'
    EXPORTING
      DIRNAME      = P_DIR
      FILENM       = '*'
    TABLES
      FILE_LIST    = IT_FILES
    EXCEPTIONS
      ACCESS_ERROR = 1
      OTHERS       = 2. 

  IF  SY-SUBRC  <>   0.
    WRITE: / 'Error reading files'.
  ELSE.
    DESCRIBE TABLE IT_FILES LINES WA_LINES.
    WRITE: / 'Numbers of files:', WA_LINES.
    LOOP AT IT_FILES INTO WA_FILES.
      WRITE: / WA_FILES-NAME.
    ENDLOOP.
  ENDIF.

Regards,

Ferry Lianto