Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FileExists method

Status
Not open for further replies.

Cimso

Technical User
Sep 25, 2001
22
US
How would I go about searching a whole drive for a file? Can it be done with the FileExists method?
 
I got this from
Dim TheFileName As String



TheFilename = sysFileFind("C", "NWIND.MDB")

If Len(TheFileName) > 0 Then

MsgBox "NWIND.MDB exists: " & TheFileName

Else

MsgBox "NWIND.MDB was not found on drive C"

End if

Should you have knowledge of the folder where NWIND.MDB is supposed to be located, you could use sysFileFind to scan that folder and any of its subfolders. For example:

Dim TheFileName As String



TheFilename = sysFileFind("C:\Program Files", "NWIND.MDB")

If Len(TheFileName) > 0 Then

MsgBox "NWIND.MDB exists: " & TheFileName

Else

MsgBox "NWIND.MDB was not found in C:\Program Files or any of its subfolders."

End if

Declare

Public Declare Function SearchTreeForFile Lib "imagehlp.dll" (ByVal lpRoothPath As String, ByVal lpInputName As String, ByVal lpOutputName As String) As Long

Public Function sysFileFind(ByVal WhichRootPath As String, ByVal WhichFileName As String) As String

Code

Dim iNull As Integer

Dim lResult As Long

Dim sBuffer As String

'internal error handler

On Error GoTo L_FILEFINDERROR

'allocate buffer

sBuffer = String$(1024, 0)

'find file

lResult = SearchTreeForFile(WhichRootPath, WhichFileName, sBuffer)

'if file was found, trim to null

'and return fully qualified path,

'otherwise return zero length string

If lResult Then

iNull = InStr(sBuffer, vbNullChar)

If Not iNull Then

sBuffer = Left$(sBuffer, iNull - 1)

End If

sysFileFind = sBuffer

Else

sysFileFind = ""

End If

Exit Function

L_FILEFINDERROR:

'on error, returns string containing error number and description

MsgBox "Function sysFilefind has returned error code.", vbInformation, "sysFileFind::violation"

sysFileFind = Format(Err.Number) & " - " & Err.Description

End Function

Tip Submitted By: Giorgio Vidali
 
I looked for "sysFileFind" in the MSDN Library Visual Studio 6.0, and I also looked for it at and couldn't find it in either place.
 
Is there a diffent way to search the entire hard drive for a certain file or files?
 
'Remember To add Reference to Microsoft Scripting Runtime
'in references
'Author:Sachin
Option Explicit
Dim fso As FileSystemObject
Private Sub Command1_Click()
Set fso = New FileSystemObject
fso_OpenTextFile "c:\1.htm", ForReading

If fso.FileExists("c:\1.htm") Then
MsgBox "exists"
Else
MsgBox "Doesn't exists"
End If

End Sub

 
Option Explicit
Dim fso As FileSystemObject
Private Sub Command1_Click()
Set fso = New FileSystemObject
fso_OpenTextFile "c:\1.htm", ForReading

If fso.FileExists("c:\1.htm") Then
MsgBox "exists"
Else
MsgBox "Doesn't exists"
End If

End Sub

 
Yes, it can be done by including FileExists in a recursive function. Here's a (rather ropey) example:
[tt]
Option Explicit
Public Function SeekFile(strFile As String, strStartPath As String) As String
' Remember To add Reference to Microsoft Scripting Runtime
Dim fso As FileSystemObject
Dim StartFolder As Folder
Dim SearchFolder As Folder
Dim FolderName As String

SeekFile = ""
Set fso = New FileSystemObject
Set StartFolder = fso.GetFolder(strStartPath)
FolderName = StartFolder
' Add trailing backslash if not root folder
If Not StartFolder.IsRootFolder Then FolderName = FolderName + "\"

' Look for file in currently selected folder
If fso.FileExists(FolderName & strFile) Then
SeekFile = StartFolder 'Aha, we've found it
Else
' We didn't find it, so look in all subfolders of
' the current folder
For Each SearchFolder In StartFolder.SubFolders
SeekFile = SeekFile(strFile, SearchFolder.Path)
If SeekFile <> &quot;&quot; Then
Exit For
End If
Next
End If
End Function
 
'a bit slow but...
'also finds only first match

Option Explicit

Sub main()
Dim oDrive As Drive
Dim oFolder As Folder
Dim oFSO As FileSystemObject

Set oFSO = New FileSystemObject
Set oDrive = oFSO.GetDrive(&quot;C:&quot;)
Set oFSO = Nothing

Set oFolder = oDrive.RootFolder
Set oDrive = Nothing

MsgBox FindFile(oFolder, &quot;Copy of mypage.html&quot;)
Set oFolder = Nothing
End Sub

Private Function FindFile(ByVal oFolder As Folder, ByVal sFile As String) As String
Dim oFile As File
Dim oSubFolder As Folder

For Each oFile In oFolder.Files
If LCase$(oFile.Name) = LCase$(sFile) Then
FindFile = oFile.Path
Set oFile = Nothing
Set oFolder = Nothing
Exit Function
End If
Next oFile
Set oFile = Nothing

For Each oSubFolder In oFolder.SubFolders
FindFile = FindFile(oSubFolder, sFile)
If FindFile <> &quot;&quot; Then
Set oSubFolder = Nothing
Set oFolder = Nothing
Exit Function
End If
Next oSubFolder
Set oSubFolder = Nothing

Set oFolder = Nothing
End Function

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top