Android Code Snippet Get List of Files using WildCards

Subname: WildCardFilesList

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

Dependencies/Libraries: None

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Used just to create test files
    CreateTestFiles
    'Call like this
    Dim ml As List = WildCardFilesList(File.DirInternal, ".png, .mov, .db", True, True)
    'Used just to log the results
    For l = 0 To ml.Size -1
        Log(ml.Get(l))
    Next
End Sub
 
'This is the only sub needed, the rest is just for a sample
Sub WildCardFilesList(FilesPath As String, WildCards As String, Sorted As Boolean, Ascending As Boolean) As List
    If File.IsDirectory("", FilesPath) Then
        Dim FilesFound As List = File.ListFiles(FilesPath)
        Dim GetCards() As String = Regex.Split(",", WildCards)
        Dim FilteredFiles As List : FilteredFiles.Initialize
        For i = 0 To FilesFound.Size -1
            For l = 0 To GetCards.Length -1
                Dim TestItem As String = FilesFound.Get(i)
                If TestItem.EndsWith(GetCards(l).Trim) Then
                    FilteredFiles.Add(TestItem.Trim)
                End If
            Next
        Next
        If Sorted Then
            FilteredFiles.SortCaseInsensitive(Ascending)
        End If
        Return FilteredFiles
    Else
        Msgbox("You must pass a valid Directory.", "NOTICE")
    End If
End Sub
 
'Used just to create sample files
Sub CreateTestFiles
    File.WriteString(File.DirInternal, "1hsjdfh.jpg", "junk text")
    File.WriteString(File.DirInternal, "2hsjdfh.jpg", "junk text")
    File.WriteString(File.DirInternal, "3hsjdfh.jpg", "junk text")
    File.WriteString(File.DirInternal, "4hsjdfh.jpg", "junk text")
    File.WriteString(File.DirInternal, "5hsjdfh.jpg", "junk text")
    File.WriteString(File.DirInternal, "1hsjdfh.png", "junk text")
    File.WriteString(File.DirInternal, "2hsjdfh.png", "junk text")
    File.WriteString(File.DirInternal, "1hsjdfh.mov", "junk text")
    File.WriteString(File.DirInternal, "2hsjdfh.mov", "junk text")
    File.WriteString(File.DirInternal, "3hsjdfh.mov", "junk text")
    File.WriteString(File.DirInternal, "1hsjdfh.db", "junk text")
    File.WriteString(File.DirInternal, "2hsjdfh.db", "junk text")
    File.WriteString(File.DirInternal, "3hsjdfh.db", "junk text")
    File.WriteString(File.DirInternal, "4hsjdfh.db", "junk text")
    File.WriteString(File.DirInternal, "5hsjdfh.db", "junk text")
End Sub

The Results:
** Activity (main) Create, isFirst = true **
1hsjdfh.db
1hsjdfh.mov
1hsjdfh.png
2hsjdfh.db
2hsjdfh.mov
2hsjdfh.png
3hsjdfh.db
3hsjdfh.mov
4hsjdfh.db
5hsjdfh.db

Tags: Files, FileList, WildCard, File Type, ListFiles, Filtered File Types, Sorted, WildCards
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
You have missed to use

Subname: ....
Description: ...

Could you please add them to the top of your post? THX
 

Dave O

Well-Known Member
Licensed User
Longtime User
Margret, thanks! This is just what I needed for filtering a list of files to only include the PNG files.
 
Top