FindFirstFile Function

Declare Function FindFirstFile Lib "kernel32.dll" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long

Platforms: Win 32s, Win 95/98, Win NT

FindFirstFile begins a file search and provides information about the first matching file. The function searches for files based only on a filename with wildcards (* or ?). The search only looks in a single directory for the file(s), but it will identify any directory names in that directory that match the search string. Identifying information about the file is put into the variable passed as lpFindFileData. The function returns a "search handle" which can be used to look for additional matching files (via FindNextFile), or -1 if no files match the search (or if an error occured).

lpFileName
The file search string to look for, including the complete path. It can contain the wildcards * or ?.
lpFindFileData
Receives identifying information about the first file that matches the search string.

Example:

' Search for all files that match "C:\MyProgram\user*.*".  Display
' the filename of each file that matches the string.
Dim hsearch As Long  ' handle to the file search
Dim findinfo As WIN32_FIND_DATA  ' receives info about matching files
Dim success As Long  ' will be 1 if successive searches are successful, 0 if not
Dim buffer As Long  ' string buffer to use to process the filename(s)
Dim retval As Long  ' generic return value

' Begin a file search:
hsearch = FindFirstFile("C:\MyProgram\user*.*", findinfo)
If hsearch = -1 Then  ' no files match the search string
  Debug.Print "(no files matched search parameter)"
  End  ' abort program
End If

' Display name of each file that matches the search.  Note that the name is displayed, the
' next file (if any) is found, and then the loop restarts.  This way the first file
' (found above) will also be displayed.
Do  ' begin loop
  ' Extract the filename from the fixed-length string:
  buffer = Left(findinfo.cFileName, InStr(findinfo.cFileName, vbNullChar) - 1)
  Debug.Print buffer  ' display this filename
  
  ' Get the next matching file and loop if it exists:
  success = FindNextFile(hsearch, findinfo)
Loop Until success = 0  ' keep looping until no more matching files are found

' Close the file search handle
retval = FindClose(hsearch)

See Also: FindClose, FindNextFile
Category: Files

Go back to the alphabetical Function listing.
Go back to the Reference section index.


This page is copyright © 2000 Paul Kuliniewicz. Copyright Information.
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/f/findfirstfile.html