Previous Topic: FindFirst - Return the Name of the First File in the SearchNext Topic: FindNext


FindFirstFile - Search for Files in a Directory

Valid on NetWare, Symbian OS, UNIX, Windows and Windows CE

The FindFirstFile function searches for files in a specified directory.

This file content function has the format:

FindFirstFile(namespec as String, filename as String) as Integer
namespec

Identifies the directory path or the file path with wildcard file specification. Do not use a literal string. For example,

NOT:
Dim filename as string
Dim findHandle as integer
findHandle=FindFirstFile("c:\windows\*.dll",filename).

BUT:
Dim filename as string
Dim filehandle as integer
Dim path as string
Path="c:\windows\*.dll"
Filehandle=FindFirstFile(path,filename)

filename

An output parameter that contains the name of the file that has been found.

On successful completion, the function returns a find handle that you use as input to FindNextFile and FindClose; otherwise, the function returns 0. If the function fails it returns 0.

Example: FindFirstFile function

This example recursively lists the contents of directories passed as arguments.

Function contents(root as string) As Boolean
	Dim fileName As String
	Dim iHandle As Integer
	Dim rootRoot As String
	
	rootRoot = Mid(root, 0, len(root)-1)
	iHandle = FindFirstFile(root, fileName)) Then
	If iHandle = 0 Then
		Print("No files found at """ + root + """")
	Else
		Repeat
			If Is_Dir(rootRoot + fileName) Then
				Print("""" + rootRoot + fileName + """ is a directory")
				if Not(fileName = ".") AND Not(fileName = "..") Then
					contents(rootRoot + fileName + "\*")
				End If
			Else
				Print("""" + rootRoot + fileName + """ is a file")
			End If
		Until Not(FindNextFile(iHandle, fileName))

		FindClose(iHandle)
	End If
	contents = TRUE
End Function

Dim i As Integer
ClrScr()
if Argc() = 1 Then
	Exit
End If
For i=1 To Argc()-1
	Print(NEWLINE$ + "Recursive List of """ + Argv(i) + """")
	Contents(Argv(i))
Next i

Invokation: dmscript sample.dms c:\* -w_dms