CALL "C$LIST-DIRECTORY" USING OP-CODE, parameters
|OP-CODE PIC 99 COMP-X||
Indicates which C$LIST-DIRECTORY operation to perform. The operations are described below.
Parameters vary depending on the op-code chosen.
Parameters provide information and hold results for the op-code specified. These parameters are described below.
C$LIST-DIRECTORY allows you to get the names of files residing in a given directory. It accomplishes this through three distinct operations. The first operation opens the specified directory. The second operation returns the filenames in the list, one-at-a-time. The third operation closes the directory and deallocates all memory used by the routine. C$LIST-DIRECTORY has the following operation codes (defined in acucobol.def):
C$LIST-DIRECTORY using listdir-open, "@[DISPLAY]:C:\path", pattern
See CBL_COPY_FILE for more information.
If the call to LISTDIR-OPEN is successful, RETURN-CODE contains a handle to the list. The value in RETURN-CODE should be moved to a data item that is USAGE HANDLE. That data item should be passed as the directory handle to the other C$LISTDIRECTORY operations. If the call to LISTDIR-OPEN fails (if the directory does not exist, contains no files, or you do not have permission to read the directory), RETURN-CODE is set to a NULL handle.
The call to LISTDIR-NEXT can include an additional argument, LISTDIR-FILE-INFORMATION (defined in "acucobol.def"), which receives information about the returned file name. This is an optional group item which returns information about the following data items:
B = block device
C = character device
D = directory
F = regular file
P = pipe (FIFO)
S = socket
U = unknown
The following example lists the contents of a directory with repeated calls C$LISTDIRECTORY:
WORKING-STORAGE SECTION. copy "def/acucobol.def". 01 pattern pic x(5) value "*.vbs". 01 directory pic x(20) value "/virusscan". 01 filename pic x(128). 01 mydir usage handle. PROCEDURE DIVISION. MAIN. * CALL LISTDIR-OPEN to get a directory handle. call "C$LIST-DIRECTORY" using listdir-open, directory, pattern. move return-code to mydir. if mydir = 0 stop run end-if. * CALL LISTDIR-NEXT to get the names of the files. * Repeat this operation until a filename containing only * spaces is returned. The filenames are not necessarily * returned in any particular order. Filenames may be * sorted on some machines and not on others. perform with test after until filename = spaces call "C$LIST-DIRECTORY" using listdir-next, mydir, filename end-perform. * CALL LISTDIR-CLOSE to close the directory and deallocate * memory. Omitting this call will result in memory leaks. call "C$LIST-DIRECTORY" using listdir-close, mydir. stop run.