Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleList all of a directory's subdirectories
Keywordslist, directory, subdirectories
CategoriesFiles and Directories
 
Use the Dir command. Note that Dir is not recursive so you cannot use it to search a subdirectory before you finish searching the parent directory. To work around this, the program stores directory names in a collection. When it finishes searching one directory, it examines the next one in the collection.
 
Option Explicit
Private Sub CmdStart_Click()
Static running As Boolean

Dim AllDirs As New Collection
Dim next_dir As Integer
Dim dir_name As String
Dim sub_dir As String
Dim i As Integer
Dim txt As String

    If running Then
        running = False
        CmdStart.Enabled = False
        CmdStart.Caption = "Stopping"
    Else
        running = True
        MousePointer = vbHourglass
        CmdStart.Caption = "Stop"
        OutText.Text = ""
        DoEvents
        
        next_dir = 1
        AllDirs.Add StartText.Text ' Start here.
        Do While next_dir <= AllDirs.Count
            ' Get the next directory to search.
            dir_name = AllDirs(next_dir)
            next_dir = next_dir + 1
            
            ' Read directories from dir_name.
            sub_dir = Dir$(dir_name & "\*", vbDirectory)
            Do While sub_dir <> ""
                ' Add the name to the list if
                ' it is a directory.
                If UCase$(sub_dir) <> "PAGEFILE.SYS" And _
                    sub_dir <> "." And sub_dir <> ".." _
                Then
                    sub_dir = dir_name & "\" & sub_dir
                    On Error Resume Next
                    If GetAttr(sub_dir) And vbDirectory _
                        Then AllDirs.Add sub_dir
                End If
                sub_dir = Dir$(, vbDirectory)
            Loop
            DoEvents
            If Not running Then Exit Do
        Loop
    
        ' Update the display.
        txt = ""
        For i = 1 To AllDirs.Count
            txt = txt & AllDirs(i) & vbCrLf
        Next i
        OutText.Text = txt
        MousePointer = vbDefault
        CmdStart.Caption = "Start"
        CmdStart.Enabled = True
        running = False
    End If
End Sub
 
Formatted by Neil Crosby
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated