Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
File List and Directory List (as array)
#3
Code: (Select All)
$Console:Only
ReDim As String FileList(0)

Disk.File.List "", "", -1, FileList() 'get a list of all files in the root directory
For i = 1 To UBound(FileList)
    Print i, FileList(i)
Next

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 ((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
                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 ((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
            File = _Files$
        Loop
    End If
    ReDim _Preserve ReturnArray(FileCount) As String
End Sub

Note:  I'd suggest folks make use of this version instead of the above.  It's a much more powerful version and can do multiple things for us that the above couldn't do.  This will get files, get directories, allow you to choose between full paths or short paths, and will even sort them if you desire.  What more could you possibly want from a file listing tool?  Big Grin
Reply


Messages In This Thread
RE: File List and Directory List (as array) - by SMcNeill - 04-22-2025, 02:51 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Windows Font List SMcNeill 27 6,197 01-20-2026, 05:50 PM
Last Post: SMcNeill
  Book List to Folders SMcNeill 5 1,520 11-27-2023, 06:51 PM
Last Post: Dimster
  Word Count / Word List Generator SMcNeill 0 679 04-20-2022, 02:35 AM
Last Post: SMcNeill

Forum Jump:


Users browsing this thread: 1 Guest(s)