Getting list of all subfolders in a directory

Hello. I am trying to find out how to write some code that'll allow me to get the folder names of every folder existing in another folder. I know how to get the names of directories immediately within the main folder, but how do I make it so it'll return the names of every folder in there (including subdirectories for all the found subdirectories) until there are no more folders in existence.

I thought about just writing something so it'll put all the immediate subdirectories in a list, and run through that list one by one accumulating more folder names until it gets to the end of that list, but I guess I was hoping there was some other ways I wasn't seeing. Thank you for your help.


Answer this question

Getting list of all subfolders in a directory

  • giorgos_gs

    Your going to need to write some recursion code to go to the nth level but

    System.IO.Directory.GetDirectories Method

    http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpref/html/frlrfsystemiodirectoryclassgetdirectoriestopic.asp

    is what you need to get a list of directories. Then loop through each of these and so on until you have looped through them all and there are no new folders.


  • MrudulaYamini

    Thanks, I just decided to use

    dim FolderList as object = my.computer.filesystem.getdirectories(MainFolder, FileIO.SearchOption.Alldirectories)

    and I sort through the object as needed for each folder name.

    Thank you very much for all your help and have a great day!

  • rs2005itw

    I use something like this for my purpose,

    Dim mydirectories() As String = IO.Directory.GetDirectories("c:\my documents", "*", IO.SearchOption.AllDirectories)

    Dim mylist As String = ""

    For Each Item As String In mydirectories

    mylist = mylist & vbCrLf & Item

    Next

    TextBox1.Text = mylist

    so it picks a folder, search all subdirectories, put it in mylist and displays it in a textbox.


  • OrpheusTheBard

    Obviously if you want to recurse - create a method that does something similar and then call this method for subdirectory folder and so on.


  • Getting list of all subfolders in a directory