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

Disk.File.List "", "" 'get a list of all files in the root directory
Disk.Dir.List "C:", "U*" 'get a list of all subdirectories that start with "U" from the root directory

For i = 1 To UBound(FileList)
    Print i, FileList(i)
Next

Sleep
_Delay .5
Cls
_KeyClear

For i = 1 To UBound(DirList)
    Print i, DirList(i)
Next
Sleep
System

Sub Disk.File.List (SearchDirectory As String, Extension As String)
    Dim As Long FileCount
    Dim As String Search, File, slash
    ReDim _Preserve FileList(1000) As String
    If SearchDirectory = "" Then SearchDirectory = _CWD$
    $If WIN Then
        slash = "\"
    $Else
        Slash = "/"
    $End If
    If Right$(SearchDirectory, 1) <> "/" _AndAlso Right$(SearchDirectory, 1) <> "\" Then SearchDirectory = SearchDirectory + slash
    If Extension = "" Then Extension = "*"
    Search = SearchDirectory + Extension
    File = _Files$(Search)
    Do While Len(File)
        File = SearchDirectory + File
        If _FileExists(File) Then
            FileCount = FileCount + 1
            If FileCount > UBound(FileList) Then ReDim _Preserve FileList(FileCount + 1000) As String
            FileList(FileCount) = File
        End If
        File = _Files$
    Loop
    ReDim _Preserve FileList(FileCount) As String
End Sub

Sub Disk.Dir.List (SearchDirectory As String, SearchFor As String)
    Dim As Long DirCount
    Dim As String Search, Dir, Slash
    ReDim _Preserve DirList(1000) As String
    If SearchDirectory = "" Then SearchDirectory = _CWD$
    $If WIN Then
        Slash = "\"
    $Else
        Slash = "/"
    $End If
    If Right$(SearchDirectory, 1) <> "/" _AndAlso Right$(SearchDirectory, 1) <> "\" Then SearchDirectory = SearchDirectory + Slash
    Search = SearchDirectory + SearchFor
    Dir = _Files$(Search)
    Do While Len(Dir)
        Dir = SearchDirectory + Dir
        If _DirExists(Dir) Then
            DirCount = DirCount + 1
            If DirCount > UBound(DirList) Then ReDim _Preserve DirList(UBound(DirList) + 1000) As String
            DirList(DirCount) = Dir
        End If
        Dir = _Files$
    Loop
    ReDim _Preserve DirList(DirCount) As String
End Sub

Updated to make use of the new _FILES$ command so this doesn't need any external libraries or anything to run.
What it does is give you a quick listing of files or directory into a shared set of arrays called FileList() and DirList().

Usage is rather simple as shown in the demo here.  If anyone has questions, feel free to ask.  Wink



EDIT:  I'd suggest using the version below for extended flexibility and control over your return data. https://qb64phoenix.com/forum/showthread...2#pid39082

In my personal opinion, the version in post #7 is much better than this version, especially once you start mixing and matching how the flag return values work for you.  Wink
Reply


Messages In This Thread
File List and Directory List (as array) - by SMcNeill - 04-21-2025, 08:26 PM

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)