Android Question List file like abc*.txt

Vinians2006

Active Member
Licensed User
Longtime User
Hi friends!
Like title says, I need to know how to filter file list returned by File.ListFiles so I choose a mask like MSWindows command prompt. I already created a basic function so you can see Im not lazy see:
B4X:
Sub ListaArquivos(Pasta As String, Mascara As String) As List
   Dim L As List
   L.Initialize
   L = File.ListFiles(Pasta) 'Like DirInternar or External
   Dim i As Int
   For i = L.Size - 1 To 0 Step -1
      'Pseudo code
      'If not in Mask then L.RemoveAt(i)    
   Next
   Return L
End Sub
Looking forward for your answers! Thanks!
 

DonManfred

Expert
Licensed User
Longtime User
B4X:
'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
 
Upvote 0

Vinians2006

Active Member
Licensed User
Longtime User
Thanks friend, I will try it now
doest not work for things like test*.*st for example, your function is good but it not what I am searching for.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Thanks friend, I will try it now
doest not work for things like test*.*st for example, your function is good but it not what I am searching for.

It´s not my function... I found it in the code-snippets... Yes, you are right... But there must be a sub.. i´m sure there is something in my brains memory. somewhere! but i dont remember
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Try this. I think this is what you are looking for:
B4X:
Sub ListaArquivos(Pasta As String, mascara As String) As List
   Dim L, L2 As List
   L.Initialize :L2.Initialize
   L = File.ListFiles(Pasta) 'Like DirInternar or External
   Dim i As Int
   For i = 0 To L.Size - 1
      Dim MyFile As String
      MyFile=L.Get(i)
     If MyFile.Contains(mascara) Then
        L2.Add(MyFile)
     End If
   Next
   Return L2
End Sub
 
Upvote 0

Vinians2006

Active Member
Licensed User
Longtime User
Thanks for trying to help friend, but "Contains" method does not "expands" wildargs like "*" or "?".... Still need help
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Try this

B4X:
    Dim mask As String = "amazon*"
    Dim flist As List = File.ListFiles(File.DirRootExternal)
    For i = 0 To flist.Size -1
        Dim filename As String = flist.Get(i)
        filename = mask.Replace("*",".+").Replace("?",".")
        Dim pattern As String = "^"&mask.Replace("*",".+").Replace("?",".")&"$"
        'Log("pattern="&pattern)
        'Log("IsMatch="&Regex.IsMatch(pattern,flist.Get(i)))
        If Regex.IsMatch(pattern,flist.Get(i)) = True Then
            Log(flist.Get(i))
        End If
    Next
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
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

B4X:
    Dim flist As List = WildCardFilesList(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
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…