This isn't tested, but is based on my post on recursively deleting all files and subfolder for a given path:
http://www.developerfusion.co.uk/forums/thread/151330/
I modified it to return a path to the file if found, or and empty string if not found.
Private Function FindInSubFolder(ByVal StartPath As String, ByVal FileToFind As String) As String
Dim myfolder As DirectoryInfo = New DirectoryInfo(StartPath)
Dim mySubfolders() As DirectoryInfo = myfolder.GetDirectories()
Dim strFiles() As FileInfo = myfolder.GetFiles()
Dim strFoundFileName As String = ""
FindInSubFolder = ""
For Each myItem As FileInfo In strFiles
If myItem.Name = FileToFind Then
FindInSubFolder = myItem.FullName
Exit Function
End If
Next
'this point is reached when the file isn't found in the current folder.
For Each myItem As DirectoryInfo In mySubfolders
'recursivly search each subfolder
strFoundFileName = FindInSubFolder(myItem.FullName, FileToFind)
If strFoundFileName.Length > 0 Then
FindInSubFolder = strFoundFileName
Exit Function
End If
Next
MsgBox("File Not Found")
End Function
However there is also the filesystem.getfiles method that will return a readonly collection of file names from the current folder and all subfolders matching the file pattern you supply. That method would return all occurances of a file, including multiple file pathnames if it exist in more than one folder by the same name. See this:
http://msdn2.microsoft.com/en-us/library/t71ykwhb(VS.80).aspx
Hope this helps!
Enter your message below
Sign in or Join us (it's free).