TestUser1
TestUser1

Reputation: 383

Get all the folders and sub-folders in side a directory

I am learning VB.net, I would like to know how to get all the folders and sub-folder s inside a directory and how to add them all to a listbox. I would also like it to list the folders while it is scanning like showing the current folders found. I have tried a few things but they never seem to work. I have tried this:

Sub GetDirectories(ByVal StartPath As String, ByRef DirectoryList As ArrayList)
    Dim Dirs() As String = Directory.GetDirectories(StartPath)
    DirectoryList.AddRange(Dirs)
    For Each Dir As String In Dirs
        GetDirectories(Dir, DirectoryList)
    Next
    For Each item In DirectoryList
        ListBox1.Items.Add(item)
    Next
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim DirList As New ArrayList
    GetDirectories("c:\hexing\", DirList)
End Sub

Upvotes: 9

Views: 45462

Answers (5)

Herbert
Herbert

Reputation: 181

I simplified the solution from supwat some more and ended up with this:

Private Function getAllFiles(ByVal StartPath As String) As List(Of String)

   Dim folders As New List(Of String)
   Dim allfiles As New List(Of String)

   folders.Add(StartPath)

   getSubDirectories(StartPath, folders)

   For Each dir As String In folders
      For Each f As String In Directory.GetFiles(dir)
         allfiles.Add(f)
      Next
   Next

   Return allfiles

End Function

Private Sub getSubDirectories(ByVal StartPath As String, ByRef folders As List(Of String))
    
   For Each Dir As String In Directory.GetDirectories(StartPath)
      folders.Add(Dir)
      getSubDirectories(Dir, folders)
   Next
    
End Sub

use as

Dim allfiles as list(of string) = getAllFiles("startpath")

Upvotes: 0

supwat
supwat

Reputation: 11

I developed my code as sample from "user10795772"(Thanks so much). But I use it to list out all files in all subfolders. Here's my code.

Private Sub GetDirectories(ByVal StartPath As String)
    For Each Dir As String In IO.Directory.GetDirectories(StartPath)
        RichTextBox1.AppendText(Dir + vbCrLf)
       'just add on 30-Nov-2023 for list-out completely
        For Each foundFile As String In My.Computer.FileSystem.GetFiles(Dir)
            RichTextBox1.AppendText(foundFile + vbCrLf)
        Next
       'end of add
        GetDirectories(Dir)
    Next
End Sub

Private Sub gafad(ByVal mfd As String) 'Get all files all directories
    For Each foundFile As String In My.Computer.FileSystem.GetFiles(mfd)
        RichTextBox1.AppendText(foundFile + vbCrLf)
    Next

    For Each fd As String In IO.Directory.GetDirectories(mfd)
        If InStr(fd, "$") = False Then 'Not list for system folder
            RichTextBox1.AppendText(fd + vbCrLf)
            For Each foundFile As String In My.Computer.FileSystem.GetFiles(fd)
                RichTextBox1.AppendText(foundFile + vbCrLf)
            Next
            GetDirectories(fd)
        End If
    Next
End Sub

When use. just Call gafad("Directory")

Upvotes: 0

user10795772
user10795772

Reputation: 21

Simplest is:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    GetDirectories(Label1.Text)
End Sub

Sub GetDirectories(ByVal StartPath As String)
    For Each Dir As String In IO.Directory.GetDirectories(StartPath)
        CheckedListBox1.Items.Add(Dir)
        GetDirectories(Dir)
    Next
End Sub

Upvotes: 2

Vignesh Kumar A
Vignesh Kumar A

Reputation: 28413

Try this

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
    Dim DirList As New ArrayList
    Dim Dirs() As String = Directory.GetDirectories(StartPath)
    DirList.AddRange(Dirs)
    For Each Dir As String In Dirs
        GetDirectories(Dir, DirectoryList)
    Next
    Catch ex As Exception
End Try
End Sub

(Or)

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        For Each Dir As String In Directory.GetDirectories("c:\Program Files")
            ListBox1.Items.Add(Dir)
        Next
End Sub

Edit

According to VB.NET 05, List Folder, SubFolders, and Sub SubFolders:

The most efficient way would be to use recursivity:

 Private Function getAllFolders(ByVal directory As String) As String()
        'Create object
        Dim fi As New IO.DirectoryInfo(directory)
        'Array to store paths
        Dim path() As String = {}
        'Loop through subfolders
        For Each subfolder As IO.DirectoryInfo In fi.GetDirectories()
            'Add this folders name
            Array.Resize(path, path.Length + 1)
            path(path.Length - 1) = subfolder.FullName
            'Recall function with each subdirectory
            For Each s As String In getAllFolders(subfolder.FullName)
                Array.Resize(path, path.Length + 1)
                path(path.Length - 1) = s
            Next
        Next
        Return path
 End Function

Upvotes: 13

MusicLovingIndianGirl
MusicLovingIndianGirl

Reputation: 5947

use the Directory.GetDirectories method.

DirectoryInfo dinfo = new DirectoryInfo("path");

dinfo.GetDirectories();

Upvotes: 3

Related Questions