实现此功能可用标准函数,本文仅供学习娱乐用。
REPORT ZSDR00038 MESSAGE-ID S1.
CONSTANTS CNS_SEP_STR TYPE CHAR01 VALUE ''''.
DATA: BEGIN OF SEARCHPOINTS OCCURS 10,
DIRNAME TYPE DIRNAME_AL11, " name of directory.
SP_NAME TYPE FILENAME_AL11, " name of entry. (may end with *)
SP_CS(10) TYPE C, " ContainsString pattern for name
END OF SEARCHPOINTS.
DATA: CNS_SERVER_PATH LIKE RLGRAP-FILENAME VALUE '/usr/sap/trans/EDI/ORDER_IN'.
TYPES: BEGIN OF TS_FILE,
DIRNAME TYPE DIRNAME_AL11, " name of directory
NAME TYPE FILENAME_AL11, " name of entry
TYPE(10) TYPE C, " type of entry.
LEN(8) TYPE P, " length in bytes.
OWNER TYPE FILEOWNER_AL11, " owner of the entry.
MTIME(6) TYPE P, " last mod.date, sec since 1970
MODE(9) TYPE C, " like "rwx-r-x--x": prot. 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 TS_FILE.
DATA: FILE TYPE TS_FILE,
FILE_LIST TYPE STANDARD TABLE OF TS_FILE WITH HEADER LINE.
TYPES: NAME_OF_DIR(1024) TYPE C.
DATA: BEGIN OF DIRECTORY_STACK OCCURS 5,
NAME TYPE NAME_OF_DIR,
END OF DIRECTORY_STACK.
DATA: SRT(1) VALUE 'T'
, NO_CS VALUE ' ' " no MUST_ContainString
, ALL_GEN VALUE '*' " generic filename shall select all
, STRLEN LIKE SY-FDPOS
, FCODE(4) TYPE C.
DATA:BEGIN OF FILE_TAB,
FILENAME TYPE CHAR255,
CONTENT TYPE STRING,
END OF FILE_TAB .
DATA:BEGIN OF TYP_CONT,
CONT TYPE C LENGTH 1000,
END OF TYP_CONT .
DATA:BEGIN OF TYP_GLN,
LOCATION_1 TYPE BBBNR,
LOCATION_2 TYPE BBSNR,
LOCATION_3 TYPE BUBKZ,
END OF TYP_GLN.
START-OF-SELECTION .
*init data.
PERFORM FRM_FILL_DATA.
*get file list FROM SERVER DIREC OR CALL THE WEB SERVICES .
PERFORM FILL_FILE_LIST USING SEARCHPOINTS-DIRNAME
SEARCHPOINTS-SP_NAME
SEARCHPOINTS-SP_CS.
*crate so by conditions.
PERFORM FRM_CREATE_SO .
FORM FILL_FILE_LIST USING A_DIR_NAME TYPE DIRNAME_AL11
A_GENERIC_NAME TYPE FILENAME_AL11
A_MUST_CS TYPE C.
DATA:
ERRCNT(2) TYPE P VALUE 0.
IF A_DIR_NAME IS INITIAL.
MESSAGE E220. " 'Place cursor on valid line !'.
ENDIF.
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 A_DIR_NAME
ID 'FILE' FIELD A_GENERIC_NAME
ID 'ERRNO' FIELD FILE-ERRNO
ID 'ERRMSG' FIELD FILE-ERRMSG.
IF SY-SUBRC <> 0.
* check if the directory was already pushed to the stack; if
* yes: remove from stack
DATA: L_NR TYPE I,
L_DIRSTACK LIKE LINE OF DIRECTORY_STACK.
L_NR = LINES( DIRECTORY_STACK ).
IF L_NR > 0.
READ TABLE DIRECTORY_STACK INTO L_DIRSTACK INDEX L_NR.
IF L_DIRSTACK-NAME = A_DIR_NAME.
DELETE DIRECTORY_STACK INDEX L_NR.
ENDIF.
ENDIF.
MESSAGE E204 WITH FILE_LIST-ERRMSG FILE-ERRMSG.
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-MODE
ID 'ERRNO' FIELD FILE-ERRNO
ID 'ERRMSG' FIELD FILE-ERRMSG.
FILE-DIRNAME = A_DIR_NAME.
MOVE SY-SUBRC TO FILE-SUBRC.
CASE SY-SUBRC.
WHEN 0.
CLEAR: FILE-ERRNO, FILE-ERRMSG.
CASE FILE-TYPE(1).
WHEN 'F'. " normal file.
PERFORM FILENAME_USEABLE USING FILE-NAME CHANGING FILE-USEABLE.
WHEN 'f'. " normal file.
PERFORM FILENAME_USEABLE USING FILE-NAME CHANGING FILE-USEABLE.
WHEN OTHERS. " directory, device, fifo, socket,...
MOVE ABAP_FALSE TO FILE-USEABLE.
ENDCASE.
IF FILE-LEN = 0.
MOVE ABAP_FALSE TO FILE-USEABLE.
ENDIF.
WHEN 1.
"No more slots available.
EXIT.
WHEN 5.
"Only NAME is valid due to internal error.
CLEAR: FILE-TYPE, FILE-LEN, FILE-OWNER, FILE-MTIME, FILE-MODE,
FILE-ERRNO, FILE-ERRMSG.
FILE-USEABLE = ABAP_FALSE.
WHEN OTHERS.
"possible other return codes (sapaci2.c)
"3 ... Internal error.
"4 ... NAME is truncated (Warning only)
ADD 1 TO ERRCNT.
IF ERRCNT > 10.
EXIT.
ENDIF.
"don't list files with error
IF FILE-SUBRC = 3.
CONTINUE.
ENDIF.
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 A_MUST_CS = NO_CS.
MOVE-CORRESPONDING FILE TO FILE_LIST.
APPEND FILE_LIST.
ELSE.
IF FILE-NAME CS A_MUST_CS.
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.
WRITE: / 'C_DIR_READ_FINISH', 'SUBRC', SY-SUBRC.
ENDIF.
IF SRT = 'T'.
SORT FILE_LIST BY MTIME DESCENDING NAME ASCENDING.
ELSE.
SORT FILE_LIST BY NAME ASCENDING MTIME DESCENDING.
ENDIF.
DELETE FILE_LIST WHERE TYPE IS INITIAL .
DELETE FILE_LIST WHERE TYPE+0(1) NE 'f'.
* LOOP AT FILE_LIST .
* WRITE FILE_LIST-NAME.
* ENDLOOP .
ENDFORM. "FILL_FILE_LIST
*&---------------------------------------------------------------------*
*& Form filename_useable
*&---------------------------------------------------------------------*
FORM FILENAME_USEABLE USING P_NAME TYPE FILENAME_AL11
CHANGING P_USEABLE TYPE ABAP_BOOL.
IF P_NAME(4) = 'core'.
P_USEABLE = ABAP_FALSE.
ELSE.
P_USEABLE = ABAP_TRUE.
ENDIF.
ENDFORM. "FILENAME_USEABLE
*&---------------------------------------------------------------------*
*& Form FRM_FILL_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM FRM_FILL_DATA .
SEARCHPOINTS-DIRNAME = CNS_SERVER_PATH.
SEARCHPOINTS-SP_NAME = '*'.
ENDFORM.