Input/Output Processes : Alternate I/O Sources : Reading the contents of a directory
Reading the contents of a directory
Sometimes, rather than reading the contents of a file, you want to read a list of the files in a directory. You can use the OS–DIR option of the INPUT FROM statement for this purpose.
Each line read from OS–DIR contains three values:
The simple (base) name of the file.
The full pathname of the file.
A string value containing one or more attribute characters. These characters indicate the type of the file and its status. Every file has one of the following attribute characters:
F — Regular file or FIFO pipe
D — Directory
S — Special device
X — Unknown file type
In addition, the attribute string for each file might contain one or more of the following attribute characters:
H — Hidden file
L — Symbolic link
P — Pipe file
The tokens are returned in the standard ABL format that can be read by the IMPORT or SET statements.
The following example uses the OS–GETENV function to find the path of the DLC directory. It then uses the OS–DIR option of INPUT FROM to read the contents of the directory:
i-osdir.p
DEFINE VARIABLE attr-list AS CHARACTER NO-UNDO
FORMAT "x(4)" LABEL "Attributes".
DEFINE VARIABLE file-name AS CHARACTER NO-UNDO
FORMAT "x(16)" LABEL "File".
DEFINE VARIABLE search-dir AS CHARACTER NO-UNDO.
search-dir = OS-GETENV("DLC").
INPUT FROM OS-DIR(search-dir).
REPEAT:
SET file-name ^ attr-list
WITH WIDTH-CHARS 70 USE-TEXT TITLE "Contents of " + search-dir.
END.
INPUT CLOSE.
In i-osdir.p, only the base name of the file and attribute string are read from OS–DIR. The caret (^) is used in the SET statement to skip over the pathname of the file.
For more information on the OS–DIR option, see the INPUT FROM Statement reference entry in OpenEdge Development: ABL Reference.
You can find additional information on a single file by using the FILE–INFO system handle. To use the FILE–INFO handle, first assign the pathname of an operating system file to the FILE–INFO:FILE–NAME attribute. You can then read other FILE–INFO attributes. For example, the i-osfile.p procedure prompts for the pathname of a file and then uses the FILE–INFO handle to get information on that file.
i-osfile.p
DEFINE VARIABLE os-file AS CHARACTER NO-UNDO FORMAT "x(60)" LABEL "File".
REPEAT:
SET os-file WITH FRAME osfile-info.
FILE-INFO:FILE-NAME = os-file.
DISPLAY FILE-INFO:FULL-PATHNAME FORMAT "x(60)" LABEL "Full Path"
FILE-INFO:PATHNAME FORMAT "x(60)" LABEL "Path"
FILE-INFO:FILE-TYPE LABEL "Type"
WITH FRAME osfile-info SIDE-LABELS TITLE "OS File Info".
END.
For more information, see the FILE–INFO System Handle reference entry in OpenEdge Development: ABL Reference.