_FILES$

From QB64 Phoenix Edition Wiki
Revision as of 23:58, 2 January 2024 by A740g (talk | contribs) (Initial _FILES$ documentation)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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."
  • _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


Examples

Example 1
Prints the names of all ".bas" files in the parent directory.
OPTION _EXPLICIT

DIM f AS STRING: f = _FILES$("../*.bas")

DO
    PRINT f

    f = _FILES$
LOOP WHILE LEN(f) > 0

END
Example 2
Recursively prints the directory tree.
$CONSOLE:ONLY
OPTION _EXPLICIT

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 3, directory

END

SUB PrintDirectory (L AS LONG, directory AS STRING)
    DIM entry(0 TO 0) AS STRING, n AS _UNSIGNED LONG

    DIM CL AS LONG: CL = L
    IF CL > _WIDTH THEN CL = _WIDTH

    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

    IF CL > 2 THEN LOCATE , CL - 2 ELSE LOCATE , CL
    PRINT directory

    DIM i AS _UNSIGNED LONG

    WHILE i < n
        LOCATE , CL: PRINT entry(i)

        $IF WINDOWS THEN
            IF entry(i) <> ".\" AND entry(i) <> "..\" AND RIGHT$(entry(i), 1) = "\" THEN PrintDirectory CL + 2, directory + entry(i)
        $ELSE
                IF entry(i) <> "./" AND entry(i) <> "../" AND RIGHT$(entry(i), 1) = "/" THEN PrintDirectory CL + 2, directory + entry(i)
        $END IF 

        i = i + 1
    WEND
END SUB


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link