VBA subdirectory search Excel 2007 challenge

mprule

New Member
Joined
Mar 18, 2010
Messages
3
Hello to All,
first I would like to thank everybody using this website and posting resolutions to problems. It has helped me many times in similar problems I had. Now to my current problem.
I have a list of file names in Row A on a spreadsheet in Excel 2007. I have in Row B the starting directory path to start the search for the filename. I need to find a way in VBA to list the path of that file in Row C in the excel sheet if the file was found. The challenge is that I have to search every subdirectory within the given start path. So basically the example is:
Row A shows file name ABC.gif, Row B shows starting path as C:/pictures and Row C would post the directory found like C:/pictures/gif
Row A can contain 1 to 2000 files names.

Does anybody have a quick solution to this for VBA? Any help or suggestions is appreciated.

thanks a bunch.
Michael
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Do you want to return just the first found directory for each file? ( there may be more that one occurence of a file in a directory structure maybe )
 
Upvote 0
Welcome to MrExcel.

Here is a UDF:

Code:
Private Declare Function SearchTreeForFile Lib "imagehlp" (ByVal RootPath As String, ByVal InputPathName As String, ByVal OutputPathBuffer As String) As Long
Private Const MAX_PATH = 260
 
Function FindFile(Path As String, Filename As String) As String
    Dim tempStr As String, Ret As Long
    tempStr = String(MAX_PATH, 0)
    If Right(Path, 1) <> "\" Then Path = Path & "\"
    Ret = SearchTreeForFile(Path, Filename, tempStr)
    If Ret <> 0 Then
        FindFile = Left$(tempStr, InStr(1, tempStr, Chr$(0)) - 1)
    Else
        FindFile = "Not found!"
    End If
End Function

adapted from post #7 at:

http://www.bigresource.com/Tracker/Track-vb-LEh9svy7Ns/
 
Upvote 0
Thanks so much Andrew you gave me the right starting point and it worked brilliantly. I posted the code I tweaked if anybody runs into the same problem.
The output of the following code is: I put file path into Rows A, file names into Rows B, the code puts the path of found files into Row C.

Sub findfile()
Dim tempStr As String, Ret As Long
Dim findfile As String

endrange = Range("b65000").End(xlUp).Row
For i = 1 To endrange
Path = Range("A" & i)
Filename = Range("B" & i)
tempStr = String(MAX_PATH, 0)
If Right(Path, 1) <> "\" Then Path = Path & "\"
Ret = SearchTreeForFile(Path, Filename, tempStr)
If Ret <> 0 Then
findfile = Left$(tempStr, InStr(1, tempStr, Chr$(0)) - 1)
Range("c" & i) = findfile
Else
findfile = "Not found!"
End If
Next i
End Sub

Thanks again for the quick solution.
 
Upvote 0

Forum statistics

Threads
1,224,532
Messages
6,179,388
Members
452,908
Latest member
MTDelphis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top