01-21-2026, 06:01 AM
Code: (Select All)
Option _Explicit
ReDim As String FileList(0), f ' be certain to reset your file listing to 0 files like this, before calling the recursive version
Dim i As Long
Disk.File.List "Z:\", "", -1, FileList() ' get a list of all files in the directory, plus subdirectories
For i = 0 To UBound(FileList)
Print i, FileList(i)
Next
f = ".png,.gif" ' comma separated extensions
Disk.File.List.FilterForExtension FileList(), f 'get your file list before you try to filter it. 
Print "Filtered"
For i = 0 To UBound(FileList)
Print i, FileList(i)
Next
Sub Disk.File.List.FilterForExtension (FileList() As String, filter As String)
'note that I haven't added filers for wild cards and such, this is for set patterns, with a comma delimiter
Dim As String temp, process
ReDim As String filters(1000), tempList(0)
Dim As Long i, j, p, count, listCount
temp = filter
Do
p = InStr(temp, ",")
If p Then
process$ = Left$(temp, p - 1) 'strip off the symbols
temp = Mid$(temp, p + 1)
Else
process$ = temp
End If
count = count + 1 'the number of filters
filters(count) = process$
Loop Until process$ = temp
ReDim tempList(UBound(FileList)) As String
For i = 1 To UBound(FileList)
temp = FileList(i)
For j = 1 To count
If InStr(temp, filters(j)) Then
listCount = listCount + 1
tempList(listCount) = FileList(i)
Exit For 'no need to keep looking, we found an item that matches our search criteria
End If
Next
Next
For i = 1 To listCount: FileList(i) = tempList(i): Next
ReDim _Preserve FileList(listCount) As String
End Sub
Sub Disk.File.List.Recursive (SearchDir As String, Extension As String, ReturnArray() As String)
ReDim TempArray(0) As String
Dim As Long i, totalcount
totalcount = UBound(ReturnArray)
Disk.File.List SearchDir, Extension, -1, TempArray() 'get a temp array of data
For i = 1 To UBound(TempArray)
totalcount = totalcount + 1
If totalcount > UBound(ReturnArray) Then ReDim _Preserve ReturnArray(totalcount + 10000) As String
ReturnArray(totalcount) = TempArray(i)
Next
ReDim _Preserve ReturnArray(totalcount) As String
For i = 1 To UBound(TempArray)
If _DirExists(TempArray(i)) Then Disk.File.List.Recursive TempArray(i), Extension, ReturnArray() 'get a temp array of data
Next
End Sub
Sub Disk.File.List (SearchDir As String, Extension As String, Flag As Long, ReturnArray() As String)
'flags are binary bits which represent the following
'Note that a quick value of -1 will set all bits and return everything for us
'1 -- file listing
'2 -- directory listing
'4 -- sorted (directory before file, like windows explorer does) -- implies 1 + 2 both are wanted.
'8 -- return full path info
Dim As Long FileCount, pass
Dim As String Search, File, Slash
ReDim ReturnArray(1000) As String
If SearchDir = "" Then SearchDir = _CWD$: If Extension = "" Then Extension = "*"
If InStr(_OS$, "WIN") Then Slash = "\" Else Slash = "/"
If Right$(SearchDir, 1) <> "/" _AndAlso Right$(SearchDir, 1) <> "\" Then SearchDir = SearchDir + Slash
Search = SearchDir + Extension
If Flag And 4 Then 'sorted so we get directory listings then files
For pass = 1 To 2 'two passes, first to get directory listings then files
File = _Files$(Search)
Do While Len(File)
If File = ".\" _OrElse File = "..\" Then
Else
If ((pass = 1) _AndAlso _DirExists(SearchDir + File)) _OrElse ((pass = 2) _AndAlso _FileExists(SearchDir + File)) Then
FileCount = FileCount + 1
If FileCount > UBound(ReturnArray) Then ReDim _Preserve ReturnArray(FileCount + 1000) As String
If Flag And 8 Then File = SearchDir + File 'we want the full path info
ReturnArray(FileCount) = File
End If
End If
File = _Files$
Loop
Next
Else 'unsorted so files and directories are simply listed in alphabetical order
File = _Files$(Search) 'one single pass where we just grab all the info at once
Do While Len(File)
If File = ".\" _OrElse File = "..\" Then
Else
If ((Flag And 1) _AndAlso _FileExists(SearchDir + File)) _OrElse ((Flag And 2) _AndAlso _DirExists(SearchDir + File)) Then
FileCount = FileCount + 1
If FileCount > UBound(ReturnArray) Then ReDim _Preserve ReturnArray(FileCount + 1000) As String
If Flag And 8 Then File = SearchDir + File 'we want the full path info
ReturnArray(FileCount) = File
End If
End If
File = _Files$
Loop
End If
ReDim _Preserve ReturnArray(FileCount) As String
End Sub
Add in one more feature to this which others might find useful -- the ability to filter the file list into a set of extensions.
If the _FILES$ command works with more than one set of filters, I don't know the syntax for it and it's not documented anywhere.
We can filter our command to look for all *.bas files, but I know of no way to look for (*.bas, *.bi, *.bm, *.h, *.txt) all in one go. So my work around is to just grab everything and then filter for those extensions all at once with the new command here.
I think the code above should showcase how to make use of it, if you want to give it a go. (Note that I'm not checking for wildcards and such here; this is just a filter for extensions so I can quickly gather other image types or all movie types or all fonts, ect.)

