Files$()

Syntax

Files$(FileSpec$)

Remarks

Returns the first filename that matches the file specification FileSpec$. If you specify a period (.) (Windows) or colon (:) (Macintosh) as the FileSpec$, Files$() returns the current path. In Windows, you can use the MS-DOS wildcard characters — the asterisk (*) and the question mark (?) — to specify files. On the Macintosh, you use MacID$() to specify files of a certain type.

Argument

Explanation

FileSpec$

The file specification. If you omit the file specification, the next file that matches the most recently used FileSpec$ is returned.

In Windows, you can specify a path as part of the file specification (for example, C:\DOCS\*.TXT). On the Macintosh, you produce the same result by changing the current folder with ChDir and then using MacID$() to specify files of a certain type (for example, MacID$("TEXT")).


By specifying FileSpec$ on the first iteration and omitting it thereafter, you can use Files$() to generate a list of files that match FileSpec$ (as in the final example in this reference entry). If no files match, an empty string ("") is returned.

Note that if the path or filename returned by Files$() contains one or more spaces, the entire return value is enclosed in quotation marks.

Examples

This Windows example returns the first file ending with the .DOC filename extension found in the current folder:


a$ = Files$("*.DOC")

The following is the same example, rewritten for the Macintosh. W6BN is the four-character sequence that specifies the Word Document file type.


a$ = Files$(MacID$("W6BN"))

The following examples return the current path (for example, "C:\WINWORD" or "HD:WORD 6:"):


CurDir$ = Files$(".")    'Windows example
CurDir$ = Files$(":")    'Macintosh example

If NOTE.DOC exists in the current folder, the following example opens the document. If NOTE.DOC does not exist, a message box is displayed.


If Files$("NOTE.DOC") <> "" Then
    FileOpen "NOTE.DOC"
Else
    MsgBox "File not found in current folder."
End If

The following example fills an array with the names of all files in the current folder. The instructions first count the files to determine the size of the array. Then they define the array, fill it with the filenames, and sort the elements. You could use this array to present a list of files in a user-defined dialog box or to open and perform an operation on each file in the array. On the Macintosh, substitute MacID$("****") for "*.*" in the file specifications.


temp$ = Files$("*.*")
count = - 1
While temp$ <> ""
    count = count + 1
    temp$ = Files$()
Wend
If count > -1 Then
    Dim list$(count)
    list$(0) = Files$("*.*")
    For i = 1 To count
        list$(i) = Files$()
    Next i
    SortArray list$()
    MsgBox Str$(count + 1) + " files; 1st in array is " + list$(0)
Else
    MsgBox "No files in current folder."
End If

See Also

FileFind, MacID$()