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: 

Getting file details from App server

Former Member
0 Kudos

Hi,

I need to get all the files in a particular directory in the App Server,

Example "Log" directory, Need to get all the files in this folder ,

check the dates, and for any file older than 7 days, need to delete the file.

I am able to get the file names, but not the date and time when it was created.

Used the FM "RZL_READ_DIR_LOCAL", So I can get the file names, but not when it was created.

Appreciate any help.

Thank you

Lalitha

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi

U can use this code instead of that fm module:

FORM SUBST_GET_FILE_LIST TABLES FILE_LIST STRUCTURE  RSFILLST
                         USING DIRNAME
                               FILENM    LIKE  RSMRGSTR-NAME
                               RC        LIKE  SY-SUBRC.


  DATA: PATTERN      LIKE  RSMRGSTR-NAME.
  DATA: SAP_YES(1)  VALUE 'X',
        SAP_NO(1)   VALUE ' '.
  DATA: NO_CS VALUE ' '.

*DATA FILE like FILE_LIST.
  DATA: BEGIN OF FILE,
          DIRNAME(75) TYPE 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
          FMODE(9)    TYPE C, " like "rwx-r-x--x": protection mode.
          USEABLE(1)  TYPE C,
          SUBRC(4)    TYPE C,
          ERRNO(3)    TYPE C,
          ERRMSG(40)  TYPE C,
          MOD_DATE    TYPE D,
          MOD_TIME(8) TYPE C,            " hh:mm:ss
          SEEN(1)     TYPE C,
          CHANGED(1)  TYPE C,
        END OF FILE.

  DATA: ERRCNT(2) TYPE P VALUE 0.

  CALL 'C_DIR_READ_FINISH'             " just to be sure
      ID 'ERRNO'  FIELD FILE_LIST-ERRNO
      ID 'ERRMSG' FIELD FILE_LIST-ERRMSG.

  CALL 'C_DIR_READ_START' ID 'DIR'    FIELD DIRNAME
                          ID 'FILE'   FIELD FILENM
                          ID 'ERRNO'  FIELD FILE-ERRNO
                          ID 'ERRMSG' FIELD FILE-ERRMSG.
  IF SY-SUBRC <> 0.
    RC = SY-SUBRC.
    EXIT.
  ENDIF.

  DO.
    CLEAR FILE.
    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-FMODE
      ID 'ERRNO'  FIELD FILE-ERRNO
      ID 'ERRMSG' FIELD FILE-ERRMSG.
    FILE-DIRNAME = DIRNAME.
    MOVE SY-SUBRC TO FILE-SUBRC.
    CASE SY-SUBRC.
      WHEN 0.
        CLEAR: FILE-ERRNO, FILE-ERRMSG.
        CASE FILE-TYPE(1).
          WHEN 'F'.                    " normal file.
          WHEN 'f'.                    " normal file.
          WHEN OTHERS. " directory, device, fifo, socket,...
            MOVE SAP_NO  TO FILE-USEABLE.
        ENDCASE.
        IF FILE-LEN = 0.
          MOVE SAP_NO TO FILE-USEABLE.
        ENDIF.
      WHEN 1.
        EXIT.
      WHEN OTHERS.                     " SY-SUBRC >= 2
        ADD 1 TO ERRCNT.
        IF ERRCNT > 10.
          EXIT.
        ENDIF.
        IF SY-SUBRC = 5.
          MOVE: '???' TO FILE-TYPE,
                '???' TO FILE-OWNER,
                '???' TO FILE-FMODE.
        ELSE.
        ENDIF.
        MOVE SAP_NO TO FILE-USEABLE.
    ENDCASE.

    PERFORM P6_TO_DATE_TIME_TZ(RSTR0400) USING FILE-MTIME
                                               FILE-MOD_TIME
                                               FILE-MOD_DATE.


*   * Does the filename contains the requested pattern?
*   * Then store it, else forget it.
    IF PATTERN = NO_CS.
      MOVE-CORRESPONDING FILE TO FILE_LIST.
      APPEND FILE_LIST.
    ELSE.
      IF FILE-NAME CP PATTERN.
        MOVE-CORRESPONDING FILE TO FILE_LIST.
        APPEND FILE_LIST.
      ENDIF.
    ENDIF.
  ENDDO.

  CALL 'C_DIR_READ_FINISH'
      ID 'ERRNO'  FIELD FILE_LIST-ERRNO
      ID 'ERRMSG' FIELD FILE_LIST-ERRMSG.
  IF SY-SUBRC <> 0.
  ENDIF.

ENDFORM.

Max

P.s.: it's the same used by AL11

4 REPLIES 4

Former Member
0 Kudos

If you debug AL11 you will see it used Unix commands. You will need to replicate this.

Former Member
0 Kudos

Hello,

Check this FM.

<b>EPS_GET_DIRECTORY_LISTING</b>

Regards,

Vasanth

Former Member
0 Kudos

Hi

U can use this code instead of that fm module:

FORM SUBST_GET_FILE_LIST TABLES FILE_LIST STRUCTURE  RSFILLST
                         USING DIRNAME
                               FILENM    LIKE  RSMRGSTR-NAME
                               RC        LIKE  SY-SUBRC.


  DATA: PATTERN      LIKE  RSMRGSTR-NAME.
  DATA: SAP_YES(1)  VALUE 'X',
        SAP_NO(1)   VALUE ' '.
  DATA: NO_CS VALUE ' '.

*DATA FILE like FILE_LIST.
  DATA: BEGIN OF FILE,
          DIRNAME(75) TYPE 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
          FMODE(9)    TYPE C, " like "rwx-r-x--x": protection mode.
          USEABLE(1)  TYPE C,
          SUBRC(4)    TYPE C,
          ERRNO(3)    TYPE C,
          ERRMSG(40)  TYPE C,
          MOD_DATE    TYPE D,
          MOD_TIME(8) TYPE C,            " hh:mm:ss
          SEEN(1)     TYPE C,
          CHANGED(1)  TYPE C,
        END OF FILE.

  DATA: ERRCNT(2) TYPE P VALUE 0.

  CALL 'C_DIR_READ_FINISH'             " just to be sure
      ID 'ERRNO'  FIELD FILE_LIST-ERRNO
      ID 'ERRMSG' FIELD FILE_LIST-ERRMSG.

  CALL 'C_DIR_READ_START' ID 'DIR'    FIELD DIRNAME
                          ID 'FILE'   FIELD FILENM
                          ID 'ERRNO'  FIELD FILE-ERRNO
                          ID 'ERRMSG' FIELD FILE-ERRMSG.
  IF SY-SUBRC <> 0.
    RC = SY-SUBRC.
    EXIT.
  ENDIF.

  DO.
    CLEAR FILE.
    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-FMODE
      ID 'ERRNO'  FIELD FILE-ERRNO
      ID 'ERRMSG' FIELD FILE-ERRMSG.
    FILE-DIRNAME = DIRNAME.
    MOVE SY-SUBRC TO FILE-SUBRC.
    CASE SY-SUBRC.
      WHEN 0.
        CLEAR: FILE-ERRNO, FILE-ERRMSG.
        CASE FILE-TYPE(1).
          WHEN 'F'.                    " normal file.
          WHEN 'f'.                    " normal file.
          WHEN OTHERS. " directory, device, fifo, socket,...
            MOVE SAP_NO  TO FILE-USEABLE.
        ENDCASE.
        IF FILE-LEN = 0.
          MOVE SAP_NO TO FILE-USEABLE.
        ENDIF.
      WHEN 1.
        EXIT.
      WHEN OTHERS.                     " SY-SUBRC >= 2
        ADD 1 TO ERRCNT.
        IF ERRCNT > 10.
          EXIT.
        ENDIF.
        IF SY-SUBRC = 5.
          MOVE: '???' TO FILE-TYPE,
                '???' TO FILE-OWNER,
                '???' TO FILE-FMODE.
        ELSE.
        ENDIF.
        MOVE SAP_NO TO FILE-USEABLE.
    ENDCASE.

    PERFORM P6_TO_DATE_TIME_TZ(RSTR0400) USING FILE-MTIME
                                               FILE-MOD_TIME
                                               FILE-MOD_DATE.


*   * Does the filename contains the requested pattern?
*   * Then store it, else forget it.
    IF PATTERN = NO_CS.
      MOVE-CORRESPONDING FILE TO FILE_LIST.
      APPEND FILE_LIST.
    ELSE.
      IF FILE-NAME CP PATTERN.
        MOVE-CORRESPONDING FILE TO FILE_LIST.
        APPEND FILE_LIST.
      ENDIF.
    ENDIF.
  ENDDO.

  CALL 'C_DIR_READ_FINISH'
      ID 'ERRNO'  FIELD FILE_LIST-ERRNO
      ID 'ERRMSG' FIELD FILE_LIST-ERRMSG.
  IF SY-SUBRC <> 0.
  ENDIF.

ENDFORM.

Max

P.s.: it's the same used by AL11

Former Member
0 Kudos

Lalitha,

Check the function module EPS_GET_FILE_ATTRIBUTES

It gives the attributes of a file like date, time etc.

You can check function modules under the function group EPSF

Rgds,

Naren