Advertise Information
Trapped in somebody else's frames?  BreakOut!!!

Display all directories in a listbox, and the selected directory's files in a second listbox

This example will walk you through the necessary procedures to locate each directory on a given drive (drive c: in this example), display those directories in a listbox (named list1), and finally display in a second listbox (named list2) all the files in the directory that was selected in the directory listbox (list1).

To try this example, start a new Visual Basic project. Create two listboxes on the form (named list1 and list2). You will also need to place a command button on the form using command1 as its name. Place the two SUBs listed below into the form's general declarations section. The SUBs contain embedded comments. The first of these two SUBs will locate all the sub-directories beneath the passed path (placing them inside list1). The second SUB will locate all the files within the directory that is selected (clicked on) in list1, placing them inside list2.

Sub ListSubDirs (Path)
    On Error Resume Next
    Dim Count, D(), I, DirName      ' Declare variables.
    DirName = Dir(Path, 16)         ' Get first directory name.
    Do While DirName <> ""
        ' A file or directory name was returned
        If DirName <> "." And DirName <> ".." Then
            ' Not a parent or current directory entry so process it
            If GetAttr(Path + DirName) = 16 Then
                ' This is a directory
                If (Count Mod 10) = 0 Then
                    ' Resize the array
                    ReDim Preserve D(Count + 10)
                End If
                Count = Count + 1   ' Increment counter.
                D(Count) = DirName  ' Add directory name to array
            End If
        End If
        DirName = Dir   ' Get another directory name.
    Loop
    For I = 1 To Count
        ' Put each directory's name in the list box.
        List1.AddItem Path & D(I)
        ' The following is the recursive call
        ' Find this directory's sub-directories
        ListSubDirs Path & D(I) & "\"
    Next I
    DoEvents
End Sub

Sub ListFiles (Path)
    list2.Clear
    On Error Resume Next
    Dim Count, D(), I, DirName    ' Declare variables.
    DirName = Dir(Path, 6)        ' Get first file name.
    Do While DirName <> ""
        If DirName <> "." And DirName <> ".." Then
            list2.AddItem DirName ' Put Filename in list box.
        End If
        DirName = Dir             ' Get another file name.
    Loop
End Sub

The following two event procedures govern the control of the program. The Command1_Click SUB fires when the command button is clicked. It passes the root directory of drive c: (C:\) to the ListSubDirs SUB which calls itself recursively in order to return all directories. The second event procedure fires when a directory listed in list1 is clicked and will display the files within that directory in list2.

Sub Command1_Click ()
    screen.MousePointer = 11 ' indicate processing is taking place
    ListSubDirs "C:\"        ' Call ListSubDirs
    screen.MousePointer = 0  ' indicate processing is done
End Sub

Sub List1_Click ()
    screen.MousePointer = 11 ' indicate processing is taking place
    ' Call ListFiles function to show files in selected directory
    ListFiles (List1.Text) + "\"
    screen.MousePointer = 0  ' indicate processing is done
End Sub

Please note that this routine is not the fastest you'll find around. Give the program time to run. For additional information on the DIR function and the attributes mask, search the Visual Basic help file under DIR.