Get list of sub-directories in VBA

后端 未结 4 1718
日久生厌
日久生厌 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
    慢半拍i (楼主)
    2020-11-22 16:09

    Here is a VBA solution, without using external objects.

    Because of the limitations of the Dir() function you need to get the whole content of each folder at once, not while crawling with a recursive algorithm.

    Function GetFilesIn(Folder As String) As Collection
      Dim F As String
      Set GetFilesIn = New Collection
      F = Dir(Folder & "\*")
      Do While F <> ""
        GetFilesIn.Add F
        F = Dir
      Loop
    End Function
    
    Function GetFoldersIn(Folder As String) As Collection
      Dim F As String
      Set GetFoldersIn = New Collection
      F = Dir(Folder & "\*", vbDirectory)
      Do While F <> ""
        If GetAttr(Folder & "\" & F) And vbDirectory Then GetFoldersIn.Add F
        F = Dir
      Loop
    End Function
    
    Sub Test()
      Dim C As Collection, F
    
      Debug.Print
      Debug.Print "Files in C:\"
      Set C = GetFilesIn("C:\")
      For Each F In C
        Debug.Print F
      Next F
    
      Debug.Print
      Debug.Print "Folders in C:\"
      Set C = GetFoldersIn("C:\")
      For Each F In C
        Debug.Print F
      Next F
    End Sub
    

    EDIT

    This version digs into subfolders and returns full path names instead of returning just the file or folder name.

    Do NOT run the test with on the whole C drive!!

    Function GetFilesIn(Folder As String, Optional Recursive As Boolean = False) As Collection
      Dim F As String
      Set GetFilesIn = New Collection
      F = Dir(Folder & "\*")
      Do While F <> ""
        GetFilesIn.Add JoinPaths(Folder, F)
        F = Dir
      Loop
    
      If Recursive Then
        Dim SubFolder, SubFile
        For Each SubFolder In GetFoldersIn(Folder)
          If Right(SubFolder, 2) <> "\." And Right(SubFolder, 3) <> "\.." Then
            For Each SubFile In GetFilesIn(CStr(SubFolder), True)
              GetFilesIn.Add SubFile
            Next SubFile
          End If
        Next SubFolder
      End If
    End Function
    
    Function GetFoldersIn(Folder As String) As Collection
      Dim F As String
      Set GetFoldersIn = New Collection
      F = Dir(Folder & "\*", vbDirectory)
      Do While F <> ""
        If GetAttr(Folder & "\" & F) And vbDirectory Then GetFoldersIn.Add JoinPaths(Folder, F)
        F = Dir
      Loop
    End Function
    
    Function JoinPaths(Path1 As String, Path2 As String) As String
      JoinPaths = Replace(Path1 & "\" & Path2, "\\", "\")
    End Function
    
    Sub Test()
      Dim C As Collection, F
    
      Debug.Print
      Debug.Print "Files in C:\"
      Set C = GetFilesIn("C:\")
      For Each F In C
        Debug.Print F
      Next F
    
      Debug.Print
      Debug.Print "Folders in C:\"
      Set C = GetFoldersIn("C:\")
      For Each F In C
        Debug.Print F
      Next F
    
      Debug.Print
      Debug.Print "All files in C:\"
      Set C = GetFilesIn("C:\", True)
      For Each F In C
        Debug.Print F
      Next F
    End Sub
    

提交回复
热议问题