Home » File/Folder Handling » How to list all sub directories names from the given directory
How to list all sub directories names from the given directory
Posted on July 30, 2011
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 | 'File/Folder Handling - How to list all sub directories names from the given directory Function GetDirectories(path As String, Optional Attributes As VbFileAttribute, _ Optional IncludePath As Boolean) As String() Dim result() As String Dim dirname As String, count 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. count = count + 1 If count > UBound(result) Then ' Resize the result array if necessary. ReDim Preserve result(count + ALLOC_CHUNK) As String End If ' Include the path if requested. If IncludePath Then dirname = path2 & dirname result(count) = dirname End If dirname = Dir$ Loop ' Trim the result array. ReDim Preserve result(count) As String GetDirectories = result End Function Private Sub Command1_Click() Dim a() As String a = GetDirectories("C:\Temp") For i = LBound(a) To UBound(a) Debug.Print a(i) Next i End Sub |
Enjoy this article?
Filed under: File/Folder Handling
Leave a comment