searching subdirectories

  • 13 years ago

       I'm running into a brick wall with this one.  scenario,  I have a large set of folders with a large number of pdf files contained within.  I can predict the name of the file but I don't know where it is in the subdirectory.  The end goal is for the server to find the file based on the name, and a starting directory path, then display that file in the adobe active x plug in the browser.   I can get it to work if I move all the pdf files into one large file, but then I run into an issue with some files having the same name which I've solved by appending a letter to the end of the name and using vbTextCompare to find all the similar files.  that works fine, but I don't want to have to duplicate all that data just for a search. 

    Can anyone point me in the right direction?

    thanks

  • 13 years ago

    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!

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“If Java had true garbage collection, most programs would delete themselves upon execution.” - Robert Sewell