Android Code Snippet Get List of Files using WildCards II

Subname: WildCardFilesList2

Description: Is used to retrieve a list of files that match your wildcard selections from your selected folder/path.
This is an extended Sub based on WildCardFilesList. Thank you @margret for the inspiration. I found this on Stackoverflow and thought "Is that possible with B4A-regex? Let´s try". Here is the result :)

Dependencies/Libraries: None

B4X:
Sub WildCardFilesList2(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)
                Dim mask As String = GetCards(l).Trim
                Dim pattern As String = "^"&mask.Replace(".","\.").Replace("*",".+").Replace("?",".")&"$"
                If Regex.IsMatch(pattern,TestItem) = True 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

Example:
B4X:
    Dim flist As List = WildCardFilesList2(File.DirRootExternal,"*.log.0, *.txt",True, True)
    For i = 0 To flist.Size -1
        Dim filename As String = flist.Get(i)
        Log(flist.Get(i))
    Next

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

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

V1.1 09/18/2014
============
- changed the generation of the pattern (thanks @Mahares for reporting the error)
 
Last edited:

Mahares

Expert
Licensed User
Longtime User
The code snippet may still need to be tweaked. When I use the below wild card, I get all the files with db extension and also all files with mdb extensions. Only the ones with the db extension should show up.
B4X:
Dim flist As List = WildCardFilesList2(File.DirRootExternal,"*.db",True, True)
 

DonManfred

Expert
Licensed User
Longtime User
Thank you for your reply, @Mahares
Please try to change the pattern-codeline in the sub to
B4X:
Dim pattern As String = "^"&mask.Replace(".","\.").Replace("*",".+").Replace("?",".")&"$"
i´m just waked up and need more coffee... But this could be the solution... Will try it this evening after work.
 
Last edited:

LucaMs

Expert
Licensed User
Longtime User
A few small suggestions (note: I still have to drink a decaffeinated coffee, Speedy :D)

Still to optimize: declare TestItem, mask and pattern on the outside of the cycles, after Dim FilteredFiles;

passing a wildcard = "*", the function returns directories instead of files;
it may be recursive (on request - Boolean option);

add "Return FilteredFiles" after the MsgBox to avoid the warning in compilation.


[P.S. comment lines to help -(intellisense)]
 
Last edited:

Mahares

Expert
Licensed User
Longtime User
@DonManfred: The change you made works very well. I tested your snippet with the following statements and all worked very well: Vive l'Allemagne!
B4X:
Dim flist As List = WildCardFilesList2(File.DirRootExternal,"*.db",True, True)  'works
    Dim flist As List = WildCardFilesList2(File.DirRootExternal,"*.d*",True, True)  'works
    Dim flist As List = WildCardFilesList2(File.DirRootExternal,"*.d?",True, True)   'works
    Dim flist As List = WildCardFilesList2(File.DirRootExternal,"*en.d?",True, True) 'works
    Dim flist As List = WildCardFilesList2(File.DirRootExternal,"*e*.d?",True, True) 'works
    Dim flist As List = WildCardFilesList2(File.DirRootExternal,"*e*.*",True, True) 'works
    Dim flist As List = WildCardFilesList2(File.DirRootExternal,"*.*",True, True) 'works
    Dim flist As List = WildCardFilesList2(File.DirRootExternal,"*.xyz",True, True) 'works

I simply added some of the below code to display a message when there is no match.
B4X:
If flist.Size=0 Then 
       Log("no files matching the pattern were found.")
    Else
       For i = 0 To flist.Size -1
           Dim filename As String = flist.Get(i)
            Log(filename)
       Next
    End If
 

DonManfred

Expert
Licensed User
Longtime User
@LucaMs i´ll have a look at your suggestions this evening. Thanks you for your answer!
a short answer:
- Recursion could be give a HUGE list (depends on the mask)
- i´ll add more comments
- the DIMs outside loop; must have a look at them. Normally it should be ok like it is... The sub is a quick hack i wrote yesterday in late evening
- wildcard = "*": you mean you want to return just folders? It is better to use a boolean more (onlyfiles true/false) i think
 

LucaMs

Expert
Licensed User
Longtime User
@LucaMs i´ll have a look at your suggestions this evening. Thanks you for your answer!
a short answer:
- Recursion could be give a HUGE list (depends on the mask)
- i´ll add more comments
- the DIMs outside loop; must have a look at them. Normally it should be ok like it is... The sub is a quick hack i wrote yesterday in late evening
- wildcard = "*": you mean you want to return just folders? It is better to use a boolean more (onlyfiles true/false) i think

You could allow the user whether or not to use recursion, by another Boolean parameter like "Sorted".

Dim inside the loop works without problems, I know, but so you create new variables each time and in this case it is not necessary, indeed, it is a waste (although in this routine is little waste, I wrote that just because the code is more correct, given that it can serve as an example).

No, I do not want to just get folders, I say that the routine as it is returns also folders, I do not know if this is your intention.


[P.S. "Vive" a world without borders and many differences :)]
 
Last edited:
Top