Click to See Complete Forum and Search --> : Writing a program like window's "Find"


Atlantisoft
March 11th, 2000, 02:06 AM
I need to make my program search the computer for sertain files with sertain specifics. Can someone tell me the basic idea of how to do this? The only hard part I'm having with it is I don't know how to check each and every single folder and subfolder.

Ernsti
March 11th, 2000, 05:37 AM
I copied that peace of code from another vb-forum, but I forgot from where. It shows you how to search subfolders recursively.


Check if file exist and when found show the location


'btw: it will only find the first one!
'just change the code to find all
'use as:

'MsgBox FindFile("c:\", vFile$) & vFile$

public Function FileExist(Path$) as Integer
Dim x

x = FreeFile

on error resume next
Open Path$ for input as x
If Err = 0 then
FileExist = true
else
FileExist = false
End If
Close x

End Function

public Function FindFile(byval Path as string, byval File as string) as string
Dim DirName as string, LastDir as string

If File = "" then Exit Function
If Right(Path, 1) <> "\" then Path = Path & "\"

DirName = Dir(Path & "*.*", vbDirectory)
Do While Not FileExist(Path & File)

If DirName = "" then Exit Do
DoEvents
If DirName <> "." And DirName <> ".." then
If (GetAttr(Path & DirName) And vbDirectory) = vbDirectory then
LastDir = DirName
DirName = FindFile(Path & DirName & "\", File)
If DirName <> "" then
Path = DirName
Exit Do
End If
DirName = Dir(Path, vbDirectory)
Do Until DirName = LastDir Or DirName = ""
DirName = Dir
Loop
If DirName = "" then Exit Do
End If
End If
DirName = Dir
Loop

If FileExist(Path & File) then FindFile = Path

End Function