Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Listing Subdirectories

Status
Not open for further replies.

Domino2

Technical User
Jun 8, 2008
475
GB
I am using code to identify folders and their documents. This code works on a crude form of splitting/listing folders and documents, but how can I split subdirectories and their documents in another group? Thanks

Private Sub Command0_Click()
Dim fso As Object
Dim fld As Object
Dim sfl As Object
Dim fil As Object
Dim gg As Integer

gg = 1
Me.List1.RowSource = "": Me.List1b.RowSource = ""
Me.List2.RowSource = ""

Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder("K:\Documents1")
' Loop through the folders
For Each sfl In fld.SubFolders
Me.List1.AddItem sfl.Name & " " & gg


' Loop through the files in the subfolder
For Each fil In sfl.Files
Me.List2.AddItem fil.Name & " " & gg
Next fil
gg = gg + 1
Next sfl
End Sub
 
Domino2, you need to create a Recursive Directory Scan, using .SubFolders won't work.

I'm working on a similar project at the moment (someone has pinched my ext hdd from my pc so I'm having to cobble Excel VBA together) but the basics using a text box with the directory path called fname are...

Code:
Private Sub ListFiles(ByVal start_dir As String, ByVal pattern As String, ByVal lst, ByVal lstN, ByVal lstD, ByVal lstE, ByVal lstFS)

Dim dir_names() As String
Dim num_dirs As Integer
Dim i As Integer
Dim fname As String
Dim attr As Integer

    fname = Dir(start_dir & "\" & pattern, vbNormal)
    Do While fname <> ""
        
        List1.AddItem start_dir & "\" & fname
        
        List1b.AddItem fname
        
        List2.AddItem UserForm1.GetFolderOwner("", start_dir & "\" & fname)

        fname = Dir()
        DoEvents
    Loop

    fname = Dir(start_dir & "\*.*", vbDirectory)
    Do While fname <> ""
        ' Skip this dir and its parent.
        attr = 0    ' In case there's an error.
        attr = GetAttr(start_dir & "\" & fname)
        If fname <> "." And fname <> ".." And _
            (attr And vbDirectory) <> 0 _
        Then
            num_dirs = num_dirs + 1
            ReDim Preserve dir_names(1 To num_dirs)
            dir_names(num_dirs) = fname
        End If
        fname = Dir()
    Loop

For i = 1 To num_dirs
        ListFiles start_dir & "\" & dir_names(i), pattern, List1, List1b, List2
    Next i

It'll need a bit of scratching but should set you off in the right area.

Cheers,

Jon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top