J
John
How do I get a list of subfolders from C:\My Documents\
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\