_FILES$
Jump to navigation
Jump to search
The _FILES$ function returns a file or directory name that matches the specified pattern.
Syntax
- e$ = _FILES$[(filespec$)]
Parameters
- filespec$ is an optional string expression that specifies a file name or path; may include wildcard characters.
Description
- If you omit filespec$ when you first call _FILES$, QB64-PE generates the error message, "Illegal Function Call."
- If filespec$ is an empty string, then it is assumed to be "*" internally.
- _FILES$ returns the first file or directory name that matches the filespec$ you specify. To retrieve additional file or directory names that match the filespec$ pattern, call _FILES$ again with no argument. When no file or directory names match, _FILES$ returns an empty string.
- You do not have to retrieve all the file names that match a given filespec$ before calling _FILES$ again with a new filespec$.
- _FILES$ is not case sensitive in Windows. However, it is case sensitive in Linux and macOS.
- Because file and directory names are retrieved in no particular order, you may want to store file names in a dynamic array and sort the array.
- Directory names returned, ends with a backslash on Windows and a forward-slash on Linux and macOS.
Availability
-
none
-
v3.11.0
-
yes
-
yes
-
yes
Examples
- Example 1
- Prints the names of all ".bas" files in the parent directory.
$CONSOLE:ONLY OPTION _EXPLICIT DIM f AS STRING: f = _FILES$("../*.bas") DO WHILE LEN(f) > 0 PRINT f f = _FILES$ LOOP END |
- Example 2
- Recursively prints the directory tree.
$CONSOLE:ONLY OPTION _EXPLICIT CONST NODE_FILE = CHR$(195) + CHR$(196) + _CHR_SPACE CONST NODE_FILE_END = CHR$(192) + CHR$(196) + _CHR_SPACE CONST NODE_DIR = CHR$(179) + _CHR_SPACE + _CHR_SPACE CONST NODE_DIR_END = _CHR_SPACE + _CHR_SPACE + _CHR_SPACE DIM directory AS STRING: directory = COMMAND$ IF NOT _DIREXISTS(directory) THEN directory = _CWD$ $IF WINDOWS THEN IF RIGHT$(directory, 1) <> "\" THEN directory = directory + "\" $ELSE IF RIGHT$(directory, 1) <> "/" THEN directory = directory + "/" $END IF PrintDirectory directory, "" END SUB PrintDirectory (directory AS STRING, prefix AS STRING) DIM entry(0 TO 0) AS STRING, n AS _UNSIGNED LONG DIM e AS STRING: e = _FILES$(directory) DO entry(n) = e n = n + 1 IF n > UBOUND(entry) THEN REDIM _PRESERVE entry(0 TO n) AS STRING e = _FILES$ LOOP WHILE LEN(e) > 0 PRINT prefix; directory DIM i AS _UNSIGNED LONG WHILE i < n DIM isLast AS _BYTE: isLast = (i = n - 1) DIM connector AS STRING IF isLast THEN connector = NODE_FILE_END ELSE connector = NODE_FILE END IF PRINT prefix; connector; entry(i) $IF WINDOWS THEN IF entry(i) <> ".\" AND entry(i) <> "..\" AND RIGHT$(entry(i), 1) = "\" THEN IF isLast THEN PrintDirectory directory + entry(i), prefix + NODE_DIR_END ELSE PrintDirectory directory + entry(i), prefix + NODE_DIR END IF END IF $ELSE IF entry(i) <> "./" AND entry(i) <> "../" AND RIGHT$(entry(i), 1) = "/" THEN IF isLast THEN PrintDirectory directory + entry(i), prefix + NODE_DIR_END ELSE PrintDirectory directory + entry(i), prefix + NODE_DIR END IF END IF $END IF i = i + 1 WEND END SUB |
See also
- Featured in our "Keyword of the Day" series
- FILES
- _CWD$, _STARTDIR$
- _DIR$
- _FULLPATH$
- _DIREXISTS, _FILEEXISTS