Function FindFolders(strStartDir As String, strResults() As String) As Long 'NOTE: this is non recursive, so the results will only show for the given folder
On Error GoTo ErrHandler
Dim wfd As WIN32_FIND_DATA
Dim nFind As LongPtr
Dim strDirectoryName As String
ReDim strResults(0) '0 will not be used
'if there is already a wildcard then leave the StartDir be (example for user passed is C:\blah\T* = all folders that start with a T
If InStr(1, strStartDir, "*") < 1 Then
If Right(strStartDir, 1) <> "\" Then strStartDir = strStartDir & "\"
strStartDir = strStartDir & "*"
End If
nFind = FindFirstFile(strStartDir, wfd) 'api call
If (wfd.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) > 0 Then 'if this is a directory
strDirectoryName = Left(wfd.cFileName, InStr(1, wfd.cFileName, Chr(0)) - 1)
If strDirectoryName <> "." And strDirectoryName <> ".." Then
ReDim Preserve strResults(UBound(strResults) + 1)
strResults(UBound(strResults)) = strDirectoryName
End If
End If
Do While FindNextFile(nFind, wfd)
If (wfd.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) > 0 Then 'if this is a directory
strDirectoryName = Left(wfd.cFileName, InStr(1, wfd.cFileName, Chr(0)) - 1)
If strDirectoryName <> "." And strDirectoryName <> ".." Then
ReDim Preserve strResults(UBound(strResults) + 1)
strResults(UBound(strResults)) = strDirectoryName
End If
End If
Loop
ErrHandler:
FindClose nFind
FindFolders = ErrorHandler(err, "FindFolders")
End Function
Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.
”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.
Our community of experts have been thoroughly vetted for their expertise and industry experience.