PDA

View Full Version : Search for file name in all drives



YasserKhalil
09-28-2017, 08:46 AM
Hello everyone
I have a file name "THISISTESTFILE.xlsm" in different locations on all the drives and I need to list all the paths for that file name ..
Is it possible as I have searched and found nothing?

YasserKhalil
09-28-2017, 09:01 AM
I have posted the same at this link
http://www.eileenslounge.com/viewtopic.php?f=30&t=27950

austenr
09-28-2017, 03:08 PM
try this:


Public Function FileExists(Filespec As String) As Boolean
Dim sPath As String, sFile As String
Dim bFound As Boolean

On Error GoTo ERR_FileExists

If Len(Filespec) > 0 Then
If Len(Dir$(Filespec)) > 0 Then bFound = True
End If

EXIT_FileExists:
FileExists = bFound
Exit Function

ERR_FileExists:
ShowError "FileExists"
Resume EXIT_FileExists

End Function

Public Function FolderExists(FullFolder As String) As Boolean
Dim sFilespec As String, bok As Boolean

' FullFolder' means that the full path including the Folder
' must be given.
' Returns true if the Folder name was returned.
' NOT true if an empty string was returned.

If Len(FullFolder) > 0 Then
sFilespec = Dir(FullFolder, vbDirectory)
If Len(sFilespec) > 0 Then bok = True E
End If
FolderExists = bok

End Function

YasserKhalil
09-28-2017, 03:12 PM
Thanks a lot
Can you fix the code between code tags?

Paul_Hossler
09-28-2017, 06:54 PM
This searches all mapped drives and looks in each folder/sub-folder for the file

I just used Debug.Print when I found one




Option Explicit
Const LookFor As String = "TestBook1.xlsm"

Dim oFSO As Object

Sub LookForFile()
Dim oDrive As IDrive
Dim oFolder As IFolder

Set oFSO = CreateObject("Scripting.FileSystemObject")
For Each oDrive In CreateObject("Scripting.FileSystemObject").Drives

If oDrive.IsReady Then

Call FileExists(oDrive.RootFolder)

For Each oFolder In oDrive.RootFolder.SubFolders
Call FileExists(oFolder)
Next
End If
Next
End Sub

Sub FileExists(oFolder As IFolder)
Dim oSubFolder As IFolder
Dim oFile As IFile

DoEvents
Debug.Print "Searching " & oFolder.Path
On Error GoTo ErrHandler

If oFSO.FileExists(oFolder.Path & "\" & LookFor) Then
Debug.Print oFolder.Path & "\" & LookFor
End If

For Each oSubFolder In oFolder.SubFolders
Call FileExists(oSubFolder)
Next

Exit Sub
ErrHandler:
If Err.Number = 70 Then
Exit Sub
Else
Resume Next
End If

End Sub

YasserKhalil
09-28-2017, 10:41 PM
Thank you very much for replies
@austenr I can't adapt those UDFs for my purpose
@Hossler thanks for working code .. but it took too long time
If you navigated to the other thread you will find similar solution .. I am searching for quick solution may be depending on Shell or using Windows Search feature in VBA

austenr
09-29-2017, 01:58 PM
try this script.


https://blogs.technet.microsoft.com/heyscriptingguy/2016/06/27/use-windows-powershell-to-search-for-files/

austenr
09-29-2017, 02:00 PM
command line solution:

for file search
find / -xdev -name settings.xml --> whole computer
find ./ -xdev -name settings.xml --> current directory & its sub directory

for files with extension type

find . -type f -name "*.iso"

mdmackillop
09-29-2017, 02:11 PM
There are third party applications such as this (https://www.jam-software.com/ultrasearch/) but I doubt they will search network drives

YasserKhalil
09-29-2017, 02:59 PM
Thank you very much everyone
I was hoping to find solution using VBA by using Shell or something similar ..