Android Code Snippet {Class}wildcardlisting - Get List of Files using WildCards III

DonManfred

Expert
Licensed User
Classname: wildcardlisting

Description: Is used to retrieve a list of files that match your wildcard selections from your selected folder/path.

This is an rewitten version of WildCardFilesList II as a Class.
The Class will fire Events when it finishes.

How to use this Class:

Add the file wildcardlisting.bas to your Project using Add exiting module... in the B4A ide


B4X:
Sub Globals
    Dim wcl As wildcardlisting
End Sub
then initialize the object in activity_create for ex
B4X:
wcl.Initialize(Me,"wcl")
'Initializes the object "wildcardlisting".
'vCallback  = Reference to Activityobject in which the Events should be fired
'vEventname = prefix for Events to be used
Create Event subs based on the name you give in Initialize (in this case "wcl")

B4X:
Sub wcl_FilesAndFoldersFinish(DirListing As List, FileListing As List)
    Log("wcl_FilesAndFoldersFinish("&DirListing.Size&","&FileListing.Size&")")
    For i = 0 To DirListing.Size -1
        'Log(DirListing.Get(i))
    Next
    For i = 0 To FileListing.Size -1
        Log(FileListing.Get(i))
    Next
End Sub
Sub wcl_DirListingError(error As String)
    Log("wcl_DirListingError("&error&")")
End Sub
Sub wcl_ListFilesFinish(FileListing As List)
    Log("wcl_ListFilesFinish("&FileListing.Size&")")
    For i = 0 To FileListing.Size -1
        Log(FileListing.Get(i))
    Next
End Sub
Sub wcl_ListFoldersFinish(DirListing As List)
    Log("wcl_ListFoldersFinish("&DirListing.Size&")")
    For i = 0 To DirListing.Size -1
        Log(DirListing.Get(i))
    Next
End Sub
you then can use one or more of the following Sub-calls

B4X:
wcl.ClearLists
' It cleares the Dir- and Filelisting lists which will be used in Events
'
wcl.ListFilesAndFolders(File.DirRootExternal, True, "*.txt",True, True)
' It will fire the Event FilesAndFoldersFinish(DirListing As List, FileListing As List)
' when it finishes
' Lists all found folders and all mathing files
' Path  = Starting path. eg. File.DirRootExternal
' Recursive = use recursive search (true/false)
' Wildcards = wildcards to use to find FILES. It is only effected on files.
'  If using recursive search ALL directorys will be scanned but it
'  will only find those files who matches one of the wildcards.
'  eg: "*.log, *.txt"
'  "*.jpg, *.png"
'  Wildcards is a comma separated list with one or more entries
' Sorted  = The resulting Directory-Listing or File-Listing will be sorted
'  or not (true/false)
' Ascending = The resulting Directory-Listing or File-Listing will be sorted
'  Ascending (true/false)
' The Event DirListingFinish will be fired when it finishes.
' The lists which will returned in this event will be cleared before this sub runs
' The Event DirListingError will be fired when the given path is not Valid
'

wcl.ListFiles(File.DirRootExternal,True, "*.wmv, *.mp4, *.mpg",True, True)
' It will fire the Event ListFilesFinish(FileListing As List)
' when it finishes
' Lists all mathing files
' Path  = Starting path. eg. File.DirRootExternal
' Recursive = use recursive search (true/false)
' Wildcards = wildcards to use to find FILES. It is only effected on files.
'  If using recursive search ALL directorys will be scanned but it
'  will only find those files who matches one of the wildcards.
'  eg: "*.log, *.txt"
'  "*.jpg, *.png"
'  Wildcards is a comma separated list with one or more entries
' Sorted  = The resulting Directory-Listing or File-Listing will be sorted
'  or not (true/false)
' Ascending = The resulting Directory-Listing or File-Listing will be sorted
'  Ascending (true/false)
' The Event ListFilesFinish will be fired when it finishes.
' The list of files which will returned in this event will NOT be cleared before this sub runs
' The Event DirListingError will be fired when the given path is not Valid
'
wcl.ListFolders(File.DirRootExternal,True, True, True)
' It will fire the Event ListFoldersFinish(DirListing As List)
'when it finishes
' Lists all folders
' Path  = Starting path. eg. File.DirRootExternal
' Recursive = use recursive search (true/false)
' Sorted  = The resulting Directory-Listing or File-Listing will be sorted
'  or not (true/false)
' Ascending = The resulting Directory-Listing or File-Listing will be sorted
'  Ascending (true/false)
' The Event DirListingFinish will be fired when it finishes.
' The list which will returned in this event will be cleared before this sub runs
' The Event DirListingError will be fired when the given path is not Valid
'

Dependencies/Libraries: None

Tags: Files, FileList, WildCard, Recursive, File Type, ListFiles, ListFolders, ListDirs, Filtered File Types, Sorted, WildCards

V1.0 09/21/2014
============
- Initial release
 

Attachments

Last edited:

LucaMs

Expert
Licensed User
You should replace line 144:
B4X:
Dim FilesFound As List = File.ListFiles(Path)
with:
B4X:
Dim FilesFound As List : FilesFound.Initialize
FilesFound = File.ListFiles(Path)
 

MarcoRome

Expert
Licensed User
Hi DonManfred and thank you for this great class.
One question. Why when i want only file and i utilize :

B4X:
wcl.ListFiles(File.DirRootExternal,True, "*.jpg, *.txt, *.*",True, True)
i have directory and file ???

Look here:
B4X:
** Activity (main) Resume **
.... 
wcl.Initialize(wcl)
 
 
ListFolders(/storage/sdcard)
Dirlisting.Size = 14
 
 
** Activity (main) Resume **
 
 
wcl_ListFoldersFinish(14)
 
/storage/sdcard/Android
/storage/sdcard/Android/data
/storage/sdcard/Android/data/anywheresoftware.b4a.samples.dbutils
/storage/sdcard/Android/data/anywheresoftware.b4a.samples.dbutils/files
/storage/sdcard/Android/data/b4a.example
/storage/sdcard/Android/data/b4a.example.instamapper
/storage/sdcard/Android/data/b4a.example.instamapper/files
/storage/sdcard/Android/data/b4a.example.instamapper/files/virtual_assets
 /storage/sdcard/Android/data/b4a.example/files
/storage/sdcard/Android/data/b4a.example/files/virtual_assets
/storage/sdcard/Android/data/com.rootsoft.xmlrpc
/storage/sdcard/Android/data/com.rootsoft.xmlrpc/files
/storage/sdcard/Android/data/com.rootsoft.xmlrpc/files/virtual_assets
/storage/sdcard/LOST.DIR
** Activity (main) Resume **
 
 
 
 
wcl.Initialize(wcl)
ListFilesAndFolders(/storage/sdcard,*.*)
Filelisting.Size = 18
Dirlisting.Size = 14
 
 
wcl_FilesAndFoldersFinish(14,18)
/storage/sdcard/Android/data/anywheresoftware.b4a.samples.dbutils/files/1.db
/storage/sdcard/Android/data/anywheresoftware.b4a.samples.dbutils/files/1.db-journal
/storage/sdcard/Android/data/b4a.example.instamapper/files/virtual_assets/main.bal
/storage/sdcard/Android/data/b4a.example/files/speed_control.db
/storage/sdcard/Android/data/b4a.example/files/speed_control2.db
/storage/sdcard/Android/data/b4a.example/files/speed_control2.db-journal
/storage/sdcard/Android/data/b4a.example/files/speed_control3.db
/storage/sdcard/Android/data/b4a.example/files/speed_control3.db-journal
/storage/sdcard/Android/data/b4a.example/files/speed_control4.db
/storage/sdcard/Android/data/b4a.example/files/speed_control5.db
/storage/sdcard/Android/data/b4a.example/files/speed_control5.db-journal
/storage/sdcard/Android/data/b4a.example/files/speed_control7.db
/storage/sdcard/Android/data/b4a.example/files/speed_control8.db
/storage/sdcard/Android/data/b4a.example/files/speed_control8.db-journal
/storage/sdcard/Android/data/b4a.example/files/virtual_assets/1.bal
/storage/sdcard/Android/data/b4a.example/files/virtual_assets/bus_roma_paline.bal
/storage/sdcard/Android/data/b4a.example/files/virtual_assets/speed_control.db
/storage/sdcard/Android/data/b4a.example/files/virtual_assets/speed_control2.db
** Activity (main) Resume **
 
 
 
wcl.Initialize(wcl)
ListFiles(/storage/sdcard,*.jpg, *.txt, *.*)
Filelisting.Size = 18
 
wcl_ListFilesFinish(18)
/storage/sdcard/Android/data/anywheresoftware.b4a.samples.dbutils/files/1.db
/storage/sdcard/Android/data/anywheresoftware.b4a.samples.dbutils/files/1.db-journal
/storage/sdcard/Android/data/b4a.example.instamapper/files/virtual_assets/main.bal
/storage/sdcard/Android/data/b4a.example/files/speed_control.db
/storage/sdcard/Android/data/b4a.example/files/speed_control2.db
/storage/sdcard/Android/data/b4a.example/files/speed_control2.db-journal
/storage/sdcard/Android/data/b4a.example/files/speed_control3.db
/storage/sdcard/Android/data/b4a.example/files/speed_control3.db-journal
/storage/sdcard/Android/data/b4a.example/files/speed_control4.db
/storage/sdcard/Android/data/b4a.example/files/speed_control5.db
/storage/sdcard/Android/data/b4a.example/files/speed_control5.db-journal
/storage/sdcard/Android/data/b4a.example/files/speed_control7.db
/storage/sdcard/Android/data/b4a.example/files/speed_control8.db
/storage/sdcard/Android/data/b4a.example/files/speed_control8.db-journal
/storage/sdcard/Android/data/b4a.example/files/virtual_assets/1.bal
/storage/sdcard/Android/data/b4a.example/files/virtual_assets/bus_roma_paline.bal
/storage/sdcard/Android/data/b4a.example/files/virtual_assets/speed_control.db
/storage/sdcard/Android/data/b4a.example/files/virtual_assets/speed_control2.db
Thank you
 

DonManfred

Expert
Licensed User
The event
wcl_ListFoldersFinish is raised every time cause the class needs to get all folders to find the files.

If you dont need the folders ignore the event
wcl_ListFoldersFinish

BTW: if you dont want to get this event fired... just remove that event sub. it will then not be fired in future
 
Last edited:

Robert Valentino

Well-Known Member
Licensed User
Found this Bug in code

B4X:
Private Sub GetDirs(Path As String, Recursive As Boolean, WildCards As String, _
  Sorted As Boolean, Ascending As Boolean, vListFiles As Boolean, vListDirs As Boolean)
   'Log("wcl.GetDirs("&Path&")")
   Dim GetCards() As String = Regex.Split(",", WildCards)
  Dim mask As String
   Dim pattern As String
  If File.IsDirectory("", Path) Then
       Dim FilesFound As List = File.ListFiles(Path)                                       ' NOTE: I Found that this line can return a List that is NOT initialized if Directory NOT Accessable

       if  FilesFound.IsInitialized = false then FilesFound.Initialize                   ' NOTE:  Added this line to protect from that

       For i = 0 To FilesFound.Size -1
           If File.IsDirectory(Path,FilesFound.Get(i)) Then
               'Log(Path&"/"&FilesFound.Get(i))
               If vListDirs Then
                   DirListing.Add(Path&"/"&FilesFound.Get(i))
               End If
               If Recursive Then
                   GetDirs(Path&"/"&FilesFound.Get(i), Recursive, WildCards, Sorted, Ascending, vListFiles, vListDirs)
               End If
           Else
               'Log(Path&"/"&FilesFound.Get(i))
           If vListFiles Then
                   For l = 0 To GetCards.Length -1
                 Dim TestItem As String = FilesFound.Get(i)
                       mask = GetCards(l).Trim
                       pattern = "^"&mask.Replace(".","\.").Replace("*",".+").Replace("?",".")&"$"
                       If Regex.IsMatch(pattern,TestItem) = True Then
                           FileListing.Add(Path&"/"&FilesFound.Get(i))
                       End If
               Next
               End If
           End If
       Next
   Else
       If SubExists(CallBack, EventName & "_DirListingError") Then
           CallSub2(CallBack, EventName & "_DirListingError", "The given path ''"&Path&"'' must be a valid directory.")
       Else
           Log("Event sub DirListingError not found")
       End If
   End If
End Sub
 
Last edited:
Top