Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Get all directories and subdirectories

Get all directories and subdirectories

Scheduled Pinned Locked Moved Visual Basic
tutorialquestion
13 Posts 4 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Colin Angus Mackay

    Use the DirectoryInfo and FileInfo classes. --Colin Mackay--

    EuroCPian Spring 2004 Get Together[^] "You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar

    L Offline
    L Offline
    ltt19
    wrote on last edited by
    #3

    Could you give me an example?? it will be very helpfull Thanks in advance

    C 1 Reply Last reply
    0
    • L ltt19

      Hi, Does anybody knows how to get all driections and subdirectoires of a hdd? I took a look at System.IO.IsolatedStorage, but I could not specify the hdd. If someone has any ideas.... Thanks

      D Offline
      D Offline
      Dr_X
      wrote on last edited by
      #4

      This code is for VB. VB.Net is quite similar though. Set a reference to Microsoft Scripting Runtime

      Dim srt As New Scripting.FileSystemObject
      Dim drv As Drive
      Dim fld As Folder
      Dim f As File

      Set drv = srt.Drives("C:\")
      Set fld = drv.RootFolder
      For Each f In fld.Files
      Debug.Print f.Name
      Next

      If you want to loop through each directory as well you will need to create another function to return the folder object. To check to see if a folder has sub folder use the following code: fld.SubFolders Michael

      L 1 Reply Last reply
      0
      • L ltt19

        Could you give me an example?? it will be very helpfull Thanks in advance

        C Offline
        C Offline
        Colin Angus Mackay
        wrote on last edited by
        #5

        All the information is available on MSDN. It is an excellent resource. Iterating over the subdirectories[^] Getting the files in a directory[^] FileInfo class[^] DirectoryInfo class[^] This should help. --Colin Mackay--

        EuroCPian Spring 2004 Get Together[^] "You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar

        L 1 Reply Last reply
        0
        • L ltt19

          Hi, Does anybody knows how to get all driections and subdirectoires of a hdd? I took a look at System.IO.IsolatedStorage, but I could not specify the hdd. If someone has any ideas.... Thanks

          J Offline
          J Offline
          Jim Taylor
          wrote on last edited by
          #6

          This is some code I have used. It writes all files in a directory and sub directories to an xml document. The functions are recusrsive. Modify to suite your needs as appropriate.

                     'Create a new xml textwriter and write to a stringwriter object
                      Dim sw As New StringWriter
                      Dim writer As New XmlTextWriter(sw)
                      'Write document element
                      writer.WriteStartElement("Files")
                      'Get the files here
                      ListFoldersAndFiles(Server.MapPath("MyFolder"), writer)
                      writer.WriteEndElement()
                      'Write end document element
                      'Clean up objects
                      writer.Flush()
                      writer.Close()
                      writer = Nothing
                      Dim xmldoc As New XmlDocument
                      xmldoc.LoadXml(sw.ToString())
          
          
          Private Sub ListFoldersAndFiles(ByVal pth As String, ByRef writer As XmlTextWriter)
              AddFiles(pth, writer)
              AddFolders(pth, writer)
          End Sub
          
          Public Sub AddFiles(ByVal pth As String, ByRef writer As XmlTextWriter)
              Dim sFiles() As String = Directory.GetFiles(pth)
              Dim iFiles As Integer
              For iFiles = 0 To UBound(sFiles)
                  writer.WriteStartElement("Files")
                  writer.WriteStartElement("Path")
                  writer.WriteString(pth)
                  writer.WriteEndElement()
                  writer.WriteStartElement("Filename")
                  writer.WriteString(Path.GetFileName(sFiles(iFiles)))
                  writer.WriteEndElement()
                  writer.WriteEndElement()
              Next
          End Sub
          
          Public Sub AddFolders(ByVal pth As String, ByRef writer As XmlTextWriter)
              Dim sDirectories() As String = Directory.GetDirectories(pth)
              Dim iDirectories As Integer
              Dim sDirectoryName As String
              For iDirectories = 0 To UBound(sDirectories)
                  AddFolders(sDirectories(iDirectories), writer)
                  AddFiles(sDirectories(iDirectories), writer)
              Next
          End Sub
          

          Jim

          L 1 Reply Last reply
          0
          • C Colin Angus Mackay

            All the information is available on MSDN. It is an excellent resource. Iterating over the subdirectories[^] Getting the files in a directory[^] FileInfo class[^] DirectoryInfo class[^] This should help. --Colin Mackay--

            EuroCPian Spring 2004 Get Together[^] "You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar

            L Offline
            L Offline
            ltt19
            wrote on last edited by
            #7

            Hi, I have already seen some these web pages, but by problem is to look at all subdirectories, I am going to make a kind of loop over each directory. I hope that it help me. Thanks, you helped me a lot too.

            C 1 Reply Last reply
            0
            • D Dr_X

              This code is for VB. VB.Net is quite similar though. Set a reference to Microsoft Scripting Runtime

              Dim srt As New Scripting.FileSystemObject
              Dim drv As Drive
              Dim fld As Folder
              Dim f As File

              Set drv = srt.Drives("C:\")
              Set fld = drv.RootFolder
              For Each f In fld.Files
              Debug.Print f.Name
              Next

              If you want to loop through each directory as well you will need to create another function to return the folder object. To check to see if a folder has sub folder use the following code: fld.SubFolders Michael

              L Offline
              L Offline
              ltt19
              wrote on last edited by
              #8

              There are some classes in System.IO that do the same that you are doing. The problem is to loop over all directories with this classes. I will try to do a loop with your code. Thanks again!!!

              D 1 Reply Last reply
              0
              • J Jim Taylor

                This is some code I have used. It writes all files in a directory and sub directories to an xml document. The functions are recusrsive. Modify to suite your needs as appropriate.

                           'Create a new xml textwriter and write to a stringwriter object
                            Dim sw As New StringWriter
                            Dim writer As New XmlTextWriter(sw)
                            'Write document element
                            writer.WriteStartElement("Files")
                            'Get the files here
                            ListFoldersAndFiles(Server.MapPath("MyFolder"), writer)
                            writer.WriteEndElement()
                            'Write end document element
                            'Clean up objects
                            writer.Flush()
                            writer.Close()
                            writer = Nothing
                            Dim xmldoc As New XmlDocument
                            xmldoc.LoadXml(sw.ToString())
                
                
                Private Sub ListFoldersAndFiles(ByVal pth As String, ByRef writer As XmlTextWriter)
                    AddFiles(pth, writer)
                    AddFolders(pth, writer)
                End Sub
                
                Public Sub AddFiles(ByVal pth As String, ByRef writer As XmlTextWriter)
                    Dim sFiles() As String = Directory.GetFiles(pth)
                    Dim iFiles As Integer
                    For iFiles = 0 To UBound(sFiles)
                        writer.WriteStartElement("Files")
                        writer.WriteStartElement("Path")
                        writer.WriteString(pth)
                        writer.WriteEndElement()
                        writer.WriteStartElement("Filename")
                        writer.WriteString(Path.GetFileName(sFiles(iFiles)))
                        writer.WriteEndElement()
                        writer.WriteEndElement()
                    Next
                End Sub
                
                Public Sub AddFolders(ByVal pth As String, ByRef writer As XmlTextWriter)
                    Dim sDirectories() As String = Directory.GetDirectories(pth)
                    Dim iDirectories As Integer
                    Dim sDirectoryName As String
                    For iDirectories = 0 To UBound(sDirectories)
                        AddFolders(sDirectories(iDirectories), writer)
                        AddFiles(sDirectories(iDirectories), writer)
                    Next
                End Sub
                

                Jim

                L Offline
                L Offline
                ltt19
                wrote on last edited by
                #9

                Hi, you are using XML, but the "idea" is the same, it could help me!!!, so I will try to do it with the System.IO classes, if not your code will be very usefull! Thanks!

                1 Reply Last reply
                0
                • L ltt19

                  There are some classes in System.IO that do the same that you are doing. The problem is to loop over all directories with this classes. I will try to do a loop with your code. Thanks again!!!

                  D Offline
                  D Offline
                  Dr_X
                  wrote on last edited by
                  #10

                  Here is something I wrote a few years back in VB6 to simple create a text file of all of my MP3s. It doesn't filter them by *.MP3 since that is all that is in that particular shared drive but you get the idea. I haven't had to mess around with the system.io yet. But the below does work. Give it a try.

                  Dim fso As New FileSystemObject
                  Dim List As TextStream

                  Const C_PLUS As String = "+ "
                  Dim i As Integer ' Indent

                  Private Sub Main()
                  Dim d As Folder

                  Set d = fso.GetFolder("N:\")
                  Set List = fso.CreateTextFile(d.Path & "NewSongList.txt", True)

                  DoFolders d
                  List.Close

                  End Sub

                  Private Sub DoFolders(fldr As Folder)
                  Dim d As Folder
                  If i = 0 Then
                  List.WriteLine Space(i) & C_PLUS & fldr.Path
                  Else
                  List.WriteLine Space(i) & C_PLUS & fldr.Name
                  End If
                  i = i + 2
                  DoFiles fldr
                  For Each d In fldr.SubFolders
                  DoFolders d
                  Next
                  i = i - 2
                  End Sub

                  Private Sub DoFiles(fldr As Folder)
                  Dim f As File

                  For Each f In fldr.Files
                  List.WriteLine Space(i + 2) & f.Name
                  Next
                  End Sub

                  Michael

                  L 1 Reply Last reply
                  0
                  • D Dr_X

                    Here is something I wrote a few years back in VB6 to simple create a text file of all of my MP3s. It doesn't filter them by *.MP3 since that is all that is in that particular shared drive but you get the idea. I haven't had to mess around with the system.io yet. But the below does work. Give it a try.

                    Dim fso As New FileSystemObject
                    Dim List As TextStream

                    Const C_PLUS As String = "+ "
                    Dim i As Integer ' Indent

                    Private Sub Main()
                    Dim d As Folder

                    Set d = fso.GetFolder("N:\")
                    Set List = fso.CreateTextFile(d.Path & "NewSongList.txt", True)

                    DoFolders d
                    List.Close

                    End Sub

                    Private Sub DoFolders(fldr As Folder)
                    Dim d As Folder
                    If i = 0 Then
                    List.WriteLine Space(i) & C_PLUS & fldr.Path
                    Else
                    List.WriteLine Space(i) & C_PLUS & fldr.Name
                    End If
                    i = i + 2
                    DoFiles fldr
                    For Each d In fldr.SubFolders
                    DoFolders d
                    Next
                    i = i - 2
                    End Sub

                    Private Sub DoFiles(fldr As Folder)
                    Dim f As File

                    For Each f In fldr.Files
                    List.WriteLine Space(i + 2) & f.Name
                    Next
                    End Sub

                    Michael

                    L Offline
                    L Offline
                    ltt19
                    wrote on last edited by
                    #11

                    Hi, Yes, the idea is great, I am going to use your example, but I will try to use the system.io class, if I can“t, I will use your example as it is..... Thanks, it is what I was looking for....

                    1 Reply Last reply
                    0
                    • L ltt19

                      Hi, I have already seen some these web pages, but by problem is to look at all subdirectories, I am going to make a kind of loop over each directory. I hope that it help me. Thanks, you helped me a lot too.

                      C Offline
                      C Offline
                      Colin Angus Mackay
                      wrote on last edited by
                      #12

                      ltt19 wrote: but by problem is to look at all subdirectories, I am going to make a kind of loop over each directory That sounds about right. For each directory you then have to loop over the subdirectories, for each of the subdirectories you have to loop over the subdirectories, and so on... --Colin Mackay--

                      EuroCPian Spring 2004 Get Together[^] "You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar

                      1 Reply Last reply
                      0
                      • L ltt19

                        Hi, Does anybody knows how to get all driections and subdirectoires of a hdd? I took a look at System.IO.IsolatedStorage, but I could not specify the hdd. If someone has any ideas.... Thanks

                        L Offline
                        L Offline
                        ltt19
                        wrote on last edited by
                        #13

                        I did it, it works, for me, the code is above, if you want to make any changes... Thanks for everybody who helped me. Here is the code, using a ListBox named as 'lst' Dim folders As New System.Collections.Specialized.StringCollection Private Function GetAllDirectories(ByVal Name As String) On Error Resume Next Dim finder As System.IO.Directory Dim cdir As New System.Collections.Specialized.StringCollection Dim cdir2 As New System.Collections.Specialized.StringCollection Dim cancel As Boolean = False Dim dir As String cdir.AddRange(finder.GetDirectories(Name)) folders.AddRange(finder.GetDirectories(Name)) Do While cancel = False For Each dir In cdir folders.AddRange(finder.GetDirectories(dir)) cdir2.AddRange(finder.GetDirectories(dir)) Next If cdir2.Count = 0 Then cdir = Nothing cdir2 = Nothing Dim i As Integer For i = 0 To folders.Count lst.Items.Add(folders(i).ToString) Next cancel = True Exit Do Else cdir.Clear() Dim y As Integer For y = 0 To cdir2.Count cdir.Add(cdir2(y)) Next cdir2.Clear() dir = Nothing y = Nothing End If Loop MessageBox.Show(lst.Items.Count) End Function Private Function GetAllFiles() On error resume next Dim files As System.IO.Directory Dim dir As String For Each dir In folders lst.Items.AddRange(files.GetFiles(dir)) Next End Function

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups