Get list of sub-directories in VBA

后端 未结 4 1719
日久生厌
日久生厌 2020-11-22 15:31
  • I want to get a list of all sub-directories within a directory.
  • If that works I want to expand it to a recursive function.

However my initial

4条回答
  •  萌比男神i
    2020-11-22 15:58

    You would be better off with the FileSystemObject. I reckon.

    To call this you just need, say: listfolders "c:\data"

    Sub listfolders(startfolder)
    ''Reference Windows Script Host Object Model
    ''If you prefer, just Dim everything as Object
    ''and use CreateObject("Scripting.FileSystemObject")
    Dim fs As New FileSystemObject
    Dim fl1 As Folder
    Dim fl2 As Folder
    
    Set fl1 = fs.GetFolder(startfolder)
    
    For Each fl2 In fl1.SubFolders
        Debug.Print fl2.Path
        listfolders fl2.Path
    Next
    
    End Sub
    

提交回复
热议问题