SAP ABAP - Read All Files In Application Server Directory

 *&---------------------------------------------------------------------*

*& Report ZREAD_FILE_AL11
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZREAD_FILE_AL11.
constants:
  yes     type flag value 'X',
  no      type flag value space,

  src_dir type epsdirnam value '/usr/sap/share/files'.

*** START OF - Directory
datav_err type i.

datadir_name type epsdirnam,
      t_dir    type table of zsepsfili with header line.  ""as output

datafile_source      type string,
      file_dest        type string,
      file_temp        type string,
      file_path        type string,
      folder(200)      type c,
      add_ext_par(255type c,
      x_rtr(1),
      x_ret2           type bapiret2,
      xt_ret2          type table of bapi_matreturn2 with header line,
      x_msg            type string,
      x_err_copy       type i,
      x_err_move       type i,
      v_type           type sydes_desc.
*** END OF - Directory


start-of-selection.
  cleardir_name.
  dir_name src_dir.
  perform get_directory_list  using    dir_name
                              changing v_err
                                       t_dir[].

  loop at t_dir.
    clearfile_source.
    file_source |{ src_dir }/{ t_dir-name }|.
    try.
        open dataset file_source for input in text mode encoding default
        ignoring conversion errors message x_msg.
      catch cx_sy_file_authority.
        x_err_copy 1" not_authorized
      catch cx_sy_file_open .
        x_err_copy 2" file_open.
    endtry.

*    DELETE DATASET file_source. ""delete file
    close dataset file_source""close file

  endloop.

*&---------------------------------------------------------------------*
form get_directory_list  using    u_dir type epsdirnam
                         changing c_err
                                  c_dir.
*&---------------------------------------------------------------------*
  datadir_list type table of zsepsfili with header line.
  databegin of file,
          dirname(75type c" name of directory. (possibly truncated.)
          name(75)    type c" name of entry. (possibly truncated.)
          type(10)    type c" type of entry.
          len(8)      type p" length in bytes.
          owner(8)    type c" owner of the entry.
          mtime(6)    type p" last modification date, seconds since 1970
          mode(9)     type c" like "rwx-r-x--x": protection mode.
          errno(3)    type c,
          errmsg(40)  type c,
        end of file.

  datafile_mask     type  epsf-epsfilnam value space,
        file_counter  type  epsf-epsfilsiz,
        error_counter type  epsf-epsfilsiz.

  call 'C_DIR_READ_FINISH'                  " just to be sure
        id 'ERRNO'  field file-errno
        id 'ERRMSG' field file-errmsg.

  call 'C_DIR_READ_START'
        id 'DIR'    field u_dir
        id 'FILE'   field file_mask
        id 'ERRNO'  field file-errno
        id 'ERRMSG' field file-errmsg.

  do.
    clear file.
    clear dir_list.
    call 'C_DIR_READ_NEXT'
          id 'TYPE'   field file-type
          id 'NAME'   field file-name
          id 'LEN'    field file-len
          id 'OWNER'  field file-owner
          id 'MTIME'  field file-mtime
          id 'MODE'   field file-mode
          id 'ERRNO'  field file-errno
          id 'ERRMSG' field file-errmsg.

*   handle files > 2147483647 byte (int 4) - hen 9.9.2004
    if file-len > 2147483647.
      dir_list-size  -99.
    else.
      dir_list-size  file-len.
    endif.
    dir_list-name file-name.
    if sy-subrc 0.
      if file-type(1'f' or              " regular file
         file-type(1'F'.
        add to file_counter.
        dir_list-rc   0.
        append dir_list.
      endif.
    elseif sy-subrc 1.
      exit.
    else.
      if error_counter > 1000.
        call 'C_DIR_READ_FINISH'
              id 'ERRNO'  field file-errno
              id 'ERRMSG' field file-errmsg.
        message 'too_many_read_errors' type 'S' display like 'E'.
        exit.
      endif.
      add to error_counter.
      dir_list-rc  18.
      append dir_list.
    endif.
  enddo.

  if dir_list[] is not initial.
    c_dir dir_list[].
    clearc_err.
  else.
    c_err 1.
  endif.

  call 'C_DIR_READ_FINISH'
        id 'ERRNO'  field file-errno
        id 'ERRMSG' field file-errmsg.

endform.

Comments

Popular posts from this blog

IT Asset Management Dengan PHP MySQL

PHP MySql CRUD Dengan Konsep MVC

Cara Sederhana Multi Insert Data Dengan PHP - MySQL