none
Can't iterate through all subfolders of a directory? Say it ain't so! RRS feed

  • Question

  • Hello, I'm using .net 2.0 and Visual Basic.  Thanks, in advance, for your interest.

     

    I've looked through the MSDN and the message boards and I can't believe there's no method for iterating through all files in a directory and all files in all directories within that directory.  But this seems to be the case!  I'll have to develop it myself, but I'm kind of stuck on how I should go about doing this.  Of course we can iterate through all files in a directory...

     

    Code Snippet

     

    Dim ofs As New DirectoryInfo(Server.MapPath(""))
    Dim oFile As FileInfo
        For Each oFile In ofs.GetFiles
    'Do stuff with the files
    Next

     

     

    And we can iterate through all the subdirectories of a directory...

     

    Code Snippet

     

    Dim ofs As New DirectoryInfo(Server.MapPath(""))
    Dim thisDirectory As DirectoryInfo
    For Each thisDirectory In ofs.GetDirectories
        'Do stuff in the directory
    Next

     

     

    And we can combine these....

     

    Code Snippet

     

    Dim ofs As New DirectoryInfo(Server.MapPath(""))
    Dim thisDirectory As DirectoryInfo
    For Each thisDirectory In ofs.GetDirectories
        Dim innerOfs As New DirectoryInfo(Server.MapPath(thisDirectory))

        For Each oFile In innerOfs.GetFiles
             'Do stuff with the files
        Next

    Next

     

     

    But this only gets the first layer of subdirectories of our directory and it completely misses the files in the root directory itself.  Before I spend HOURS manually werstling with code to test whether I'm at the last child of a directory and all that jazz, please let me know if I'm missing something... if there's a better way.

     

    Thank you very much,
    =Adam=

    Wednesday, August 22, 2007 3:06 PM

Answers

  •  

    you need a recursive method, so for each directory, call the method, then for each file in the current directory, get the files

     

    you could of course use the overload to get all directories of a "root" directory:

     

    Dim dirs() as string = System.IO.Directory.GetDirectories(path, pattern, System.IO.SearchOptions.AllDirectories)

     

     

    Does this help? I believe this maybe what you are after - to get all directories in a given directory - in which case the class and method above does this

    Wednesday, August 22, 2007 3:20 PM
    Moderator
  • Excellent!  That worked beautifully!  Here is a code snippet that adds the document location strings to a list item (documentLocations) in case anyone is searching for this in the future...


    Code Snippet

     

    Dim rootDirectory As String = Server.MapPath("")
    Dim oFile As FileInfo
    Dim dirs() As String = System.IO.Directory.GetDirectories(Server.MapPath(""), "*", SearchOption.AllDirectories)
    Dim thisDirectoryName As String
    For Each thisDirectoryName In dirs
         Dim thisDirectory As New DirectoryInfo(thisDirectoryName)
         For Each oFile In thisDirectory.GetFiles
              ' #######  Replace with whatever code you need #######

              documentLocations.Add((((thisDirectoryName + "/" + oFile.Name).Replace(rootDirectory, System.Configuration.ConfigurationManager.AppSettings("filesLocation")).Replace("\", "/")).Replace("//", "/")).Replace("http:/", "http://"))

               ' ##############################################

         Next
    Next

     

     

     

     

     

    Wednesday, August 22, 2007 4:36 PM

All replies

  •  

    you need a recursive method, so for each directory, call the method, then for each file in the current directory, get the files

     

    you could of course use the overload to get all directories of a "root" directory:

     

    Dim dirs() as string = System.IO.Directory.GetDirectories(path, pattern, System.IO.SearchOptions.AllDirectories)

     

     

    Does this help? I believe this maybe what you are after - to get all directories in a given directory - in which case the class and method above does this

    Wednesday, August 22, 2007 3:20 PM
    Moderator
  • Excellent!  That worked beautifully!  Here is a code snippet that adds the document location strings to a list item (documentLocations) in case anyone is searching for this in the future...


    Code Snippet

     

    Dim rootDirectory As String = Server.MapPath("")
    Dim oFile As FileInfo
    Dim dirs() As String = System.IO.Directory.GetDirectories(Server.MapPath(""), "*", SearchOption.AllDirectories)
    Dim thisDirectoryName As String
    For Each thisDirectoryName In dirs
         Dim thisDirectory As New DirectoryInfo(thisDirectoryName)
         For Each oFile In thisDirectory.GetFiles
              ' #######  Replace with whatever code you need #######

              documentLocations.Add((((thisDirectoryName + "/" + oFile.Name).Replace(rootDirectory, System.Configuration.ConfigurationManager.AppSettings("filesLocation")).Replace("\", "/")).Replace("//", "/")).Replace("http:/", "http://"))

               ' ##############################################

         Next
    Next

     

     

     

     

     

    Wednesday, August 22, 2007 4:36 PM
  • Although a very old post but you never know what act, assistance and aid by you guys might make you earn an excellent respect from hearts of any future users!

    Thanx buddies!

    Just the code I was looking for!


    Best Regards, Faraz A Qureshi

    Sunday, September 21, 2014 7:23 PM
  • A matchless advice for sure brother!

    Excellent!


    Best Regards, Faraz A Qureshi

    Sunday, September 21, 2014 7:26 PM