Programmer Reference : Common File System : Searching directories : Opening a directory for searching
Opening a directory for searching
Open a specific directory for searching by sending the opendir:pattern:mode: message to the CfsDirectoryDescriptor class. This answers a CfsDirectoryDescriptor instance that is similar to the CfsFileDescriptor instance used to access regular files.
The opendir:pattern:mode: message takes three arguments. The first argument is a string specifying the path of the directory to be searched.
The second argument is a pattern for matching entry names. The $* wildcard character matches 0 or more characters, the $? wildcard character matches any single character. A pattern of '*' always matches all files, regardless of platform.
The third argument specifies the type of files to match. It is a logical inclusive-OR of one or more of the following constants, which are defined in the CfsConstants pool dictionary:
Table 8. Directory search modes
Pool variable
Description
FREG
If specified, matches regular files.
FDIR
If specified, matches directories.
FSPECIAL
If specified, matches files that are neither regular files nor directories.
The following are some examples of opening directory streams:
| dd |
"Get search handle for files of all types in the current working directory"
(dd := CfsDirectoryDescriptor
opendir: '.'
pattern: '*'
mode: FREG | FDIR | FSPECIAL) isCfsError
ifTrue: [^self error: dd message].
dd closedir.
| dd |
"Get search handle for all regular files ending in '.bat'"
(dd := CfsDirectoryDescriptor
opendir: 'c:\'
pattern: '*.bat'
mode: FREG) isCfsError
ifTrue: [self error: dd message].
dd closedir.
| dd |
"Get search handle for all special files in the root of drive C:"
(dd := CfsDirectoryDescriptor
opendir: 'c:\'
pattern: '*'
mode: FSPECIAL) isCfsError
ifTrue: [^self error: dd message].
dd closedir.
Pattern matching uses platform-specific optimizations on platforms with direct support for pattern-matched searches.
Last modified date: 05/12/2020