Home » File/Folder Handling » How to list all sub directories in a given directory
How to list all sub directories in a given directory
Posted on January 4, 2009
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | 'File/Folder Handling - How to list all sub directories in a given directory Public Function GetDirectories(path As String, Optional Attributes As VbFileAttribute, _ Optional IncludePath As Boolean) As String() Dim result() As String Dim DirName As String, lngCount As Long, Path2 As String Const ALLOC_CHUNK = 50 ReDim result(ALLOC_CHUNK) As String ' Build the path name + backslash. Path2 = path If Right$(Path2, 1) <> "\" Then Path2 = Path2 & "\" DirName = Dir$(Path2 & "*.*", vbDirectory Or Attributes) Do While Len(DirName) If DirName = "." Or DirName = ".." Then ' Exclude the "." and ".." entries. ElseIf (GetAttr(Path2 & DirName) And vbDirectory) = 0 Then ' This is a regular file. Else ' This is a directory. lngCount = lngCount + 1 If lngCount > UBound(result) Then ' Resize the result array if necessary. ReDim Preserve result(lngCount + ALLOC_CHUNK) As String End If ' Include the path if requested. If IncludePath Then DirName = Path2 & DirName result(lngCount) = DirName End If DirName = Dir$ Loop ' Trim the result array. ReDim Preserve result(lngCount) As String GetDirectories = result End Function 'How can I call this function 'Sub ListAllDirectories() ' Dim results() As String ' Dim intCount As Integer ' results = GetDirectories("C:\Temp") ' For intCount = LBound(results) To UBound(results) ' Debug.Print results(intCount) ' Next intCount 'End Sub |
Enjoy this article?
Filed under: File/Folder Handling
Leave a comment