Search folders for file?
Can anyone tell me where I can get a vbscript to search a folder and sub folder for a file? I've tried looking on MS script center but could find one.
Also are there any other places to download scripts from other the MS script center?
Also are there any other places to download scripts from other the MS script center?
0 Comments
[ + ] Show comments
Answers (4)
Please log in to answer
Posted by:
aogilmor
19 years ago
Posted by:
WiseUser
19 years ago
Here's a very crude one I just wrote - it doesn't search for folders (only files), it only returns the first file that it finds, and it doesn't work on the root of a drive. But it should point you in the right direction...
Set oFso = CreateObject("Scripting.FileSystemObject")
Msgbox sFindFile("hosts", "c:\Windows", True)
Function sFindFile(sFile, sFolder, bRecurse)
Set oFolder = oFso.GetFolder(sFolder)
Set cFiles = oFolder.Files
For Each oFile in cFiles
If LCase(oFile.Name) = LCase(sFile) Then
sFindFile = oFile.Path : Exit Function
End If
Next
Set cSubFolders = oFolder.SubFolders
If bRecurse Then
For Each oSub in cSubFolders
sFindFile = sFindFile(sFile, oSub.Path, bRecurse)
If sFindFile <> "" Then Exit Function
Next
End If
End Function
Set oFso = Nothing
Msgbox sFindFile("hosts", "c:\Windows", True)
Function sFindFile(sFile, sFolder, bRecurse)
Set oFolder = oFso.GetFolder(sFolder)
Set cFiles = oFolder.Files
For Each oFile in cFiles
If LCase(oFile.Name) = LCase(sFile) Then
sFindFile = oFile.Path : Exit Function
End If
Next
Set cSubFolders = oFolder.SubFolders
If bRecurse Then
For Each oSub in cSubFolders
sFindFile = sFindFile(sFile, oSub.Path, bRecurse)
If sFindFile <> "" Then Exit Function
Next
End If
End Function
Set oFso = Nothing
![](/build/static/general/appdeploy_logo.png)
so that the conversation will remain readable.