How to get list of subfolders from a specific folder

  • Thread starter Thread starter John
  • Start date Start date
Hi John

The following function will return all the subfolders as an array of
strings:

Public Function ListSubfolders(ByVal strPath As String) As Variant
Dim strList As String
Dim strNext As String
' add a trailing backslash if necessary
If Right(strPath, 1) <> "\" Then strPath = strPath & "\"
strNext = Dir(strPath, vbDirectory)
Do Until strNext = ""
' look only at folders, not files
If (GetAttr(strPath & strNext) And vbDirectory) = vbDirectory Then
' ignore the old DOS . and .. folders
If strNext <> "." And strNext <> ".." Then
strList = strList & strNext & vbNullChar
End If
End If
strNext = Dir
Loop
' convert the list into an array
ListSubfolders = Split(Left(strList, Len(strList) - 1), vbNullChar)
End Function

To use it, simply call the function and assign the result to a variant. For
example:

Dim aFolders As Variant, i As Integer
aFolders = ListSubfolders("C:\My Documents")
For i = LBound(aFolders) To UBound(aFolders)
Debug.Print aFolders(i)
Next
 
Additionally, Albert Kallal (ACCESS MVP) posted code to do just this type of
thing back in April 2005 in another newsgroup:
----------------
Here is a nice short routine that will "walk" the dir structure...

Sub dirTest()

Dim dlist As New Collection
Dim startDir As String
Dim i As Integer

startDir = "C:\access\"
Call FillDir(startDir, dlist)

MsgBox "there are " & dlist.Count & " in the dir"

' lets printout the stuff into debug window for a test

For i = 1 To dlist.Count
Debug.Print dlist(i)
Next i

End Sub


Sub FillDir(startDir As String, dlist As Collection)

' build up a list of files, and then
' add add to this list, any additinal
' folders

Dim strTemp As String
Dim colFolders As New Collection
Dim vFolderName As Variant

strTemp = Dir(startDir)

Do While strTemp <> ""
dlist.Add startDir & strTemp
strTemp = Dir
Loop

' now build a list of additional folders
strTemp = Dir(startDir & "*.", vbDirectory)

Do While strTemp <> ""
If (strTemp <> ".") And (strTemp <> "..") Then
colFolders.Add strTemp
End If
strTemp = Dir
Loop

' now process each folder (recursion)
For Each vFolderName In colFolders
Call FillDir(startDir & vFolderName & "\", dlist)
Next vFolderName

End Sub


--
Albert D. Kallal (Access MVP)
--

Ken Snell
<MS ACCESS MVP>

Graham Mandeno said:
Hi John

The following function will return all the subfolders as an array of
strings:

Public Function ListSubfolders(ByVal strPath As String) As Variant
Dim strList As String
Dim strNext As String
' add a trailing backslash if necessary
If Right(strPath, 1) <> "\" Then strPath = strPath & "\"
strNext = Dir(strPath, vbDirectory)
Do Until strNext = ""
' look only at folders, not files
If (GetAttr(strPath & strNext) And vbDirectory) = vbDirectory Then
' ignore the old DOS . and .. folders
If strNext <> "." And strNext <> ".." Then
strList = strList & strNext & vbNullChar
End If
End If
strNext = Dir
Loop
' convert the list into an array
ListSubfolders = Split(Left(strList, Len(strList) - 1), vbNullChar)
End Function

To use it, simply call the function and assign the result to a variant.
For example:

Dim aFolders As Variant, i As Integer
aFolders = ListSubfolders("C:\My Documents")
For i = LBound(aFolders) To UBound(aFolders)
Debug.Print aFolders(i)
Next

--
Good Luck!

Graham Mandeno [Access MVP]
Auckland, New Zealand

John said:
How do I get a list of subfolders from C:\My Documents\
 
Back
Top