Option Explicit
Private Const MAX_PATH = 260
Private Const FILE_ATTRIBUTE_DIRECTORY = &H10
Public Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Public Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
cAlternate As String * 14
End Type
Public Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
Public Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Public Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Private Dirs() As String
Public Function FindDirectories(ByVal Path As String, DirName As String) As Variant
'Nucleus
ReDim Dirs(1 To 1)
Call EnumDirs(Path, DirName)
If Len(Dirs(1)) Then FindDirectories = Dirs Else FindDirectories = Null
Erase Dirs
End Function
Private Sub EnumDirs(ByVal Path As String, DirName As String)
Dim hFirstFound As Long
Dim hFound As Long
Dim WFD As WIN32_FIND_DATA
Dim fname As String
If Right$(Path, 1) <> "\" Then Path = Path & "\"
hFirstFound = FindFirstFile(Path & "*.*", WFD)
hFound = (hFirstFound > 0)
Do While hFound
fname = Left(WFD.cFileName, InStr(1, WFD.cFileName, Chr(0)) - 1)
If WFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY Then
If fname <> "." And fname <> ".." Then
Call EnumDirs(Path & fname, DirName)
If fname = DirName Then
If Len(Dirs(1)) Then ReDim Preserve Dirs(1 To UBound(Dirs) + 1)
Dirs(UBound(Dirs)) = Path & fname
End If
End If
End If
hFound = FindNextFile(hFirstFound, WFD)
Loop
End Sub
Public Function EnumerateFiles(ByVal Path As String) As Variant
Dim hFirstFound As Long
Dim hFound As Long
Dim WFD As WIN32_FIND_DATA
Dim fname As String
Dim Files() As String: ReDim Files(1 To 1)
If Right$(Path, 1) <> "\" Then Path = Path & "\"
hFirstFound = FindFirstFile(Path & "*.*", WFD)
hFound = (hFirstFound > 0)
Do While hFound
fname = Left(WFD.cFileName, InStr(1, WFD.cFileName, Chr(0)) - 1)
If fname <> "." And fname <> ".." Then
If Len(Files(1)) Then ReDim Preserve Files(1 To UBound(Files) + 1)
Files(UBound(Files)) = Path & fname
End If
hFound = FindNextFile(hFirstFound, WFD)
Loop
If Len(Files(1)) Then EnumerateFiles = Files Else EnumerateFiles = Null
End Function