B4A Library [CLASS] clsFilesLists

Description:
With this class you can:
- get an ordered list of files from a directory without directories names;
- get an ordered list of files from a directory filtered using classic pattern;
- delete all files in a directory that match a classic pattern.
(patterns like: "MyFile*.txt" or "*My*.*" or "Image.jp*" etc.).

Methods:
- ListFilesOnly(Dir As String, Sorted As Boolean, Ascending As Boolean) As List
- ListFiles(Dir As String, Pattern As String, Sorted As Boolean, Ascending As Boolean) As List
- DeleteFiles(Dir As String, Pattern As String)

Tags: ListFiles, File, Files, Directory, Directories


Version 1.1 - see posts #6 and #7
 

Attachments

Last edited:
Hi @LucaMs.
How I can call the sub ListFiles and show the list of xml files (in DirDefaultExternal) in a InputList.
Thanks.
 
Many thanks for this very useful tool! AND thanks for providing it as a .bas file, so I can learn from your code.
This work helps me a lot!
 
LucaMs

Tthanks for this Class, I have been using it happily for some time but recently struck a problem. My LG V20 produces files without extensions in the Download Folder, the lack of "." caused a crash in the "GetFilteredFileList" Sub. The code below shows my bypass of the problem, you will probably have a better solution.

Code: Copy
Private Sub GetFilteredFileList(Dir As String, Pattern As String, Sorted As Boolean, Ascending As Boolean) As List
    If Pattern = "*.*" Then
        Return ListFilesOnly(Dir, Sorted, Ascending)
    End If

    Dim lstRes As List
    lstRes.Initialize

    ' Get the files list in the Dir directory without directories names.
    Dim lstFilesOnly As List
    lstFilesOnly = ListFilesOnly(Dir, Sorted, Ascending)
  
    ' Analyzes the Pattern
    Dim Filter As typFilter
    Filter.Initialize
    Filter = ParsePattern(Pattern)
  
    ' Filters the files list
    Dim FileName As String
    Dim FileBaseName As String
    Dim Extension As String
    Dim Valid As Boolean
  
    For i = 0 To lstFilesOnly.Size - 1
        FileName = lstFilesOnly.Get(i)
        If FileName.Contains(".") Then                'TEST FOR "."
'The following line causes and error if the FileName has no extension.
            FileBaseName = FileName.SubString2(0, FileName.LastIndexOf("."))
            ' Filters on name
            Select Case Filter.NamePosition
                Case F_NONE
                    Valid = True
                Case F_STARTSWITH
                    Valid = FileBaseName.StartsWith(Filter.NameFilter)
                Case F_ENDSWITH
                    Valid = FileBaseName.EndsWith(Filter.NameFilter)
                Case F_CONTAINS
                    Valid = FileBaseName.Contains(Filter.NameFilter)
            End Select
          
            If Valid And Filter.ExtFilter <> "*" Then
                ' Filters on extension
                Extension = FileName.SubString(FileName.LastIndexOf(".") + 1)
                Select Case Filter.ExtPosition
                    Case F_NONE
                        Valid = True
                    Case F_STARTSWITH
                        Valid = Extension.StartsWith(Filter.ExtFilter)
                    Case F_ENDSWITH
                        Valid = Extension.EndsWith(Filter.ExtFilter)
                    Case F_CONTAINS
                        Valid = Extension.Contains(Filter.ExtFilter)
                End Select
            End If

            If Valid Then
                lstRes.Add(FileName)
            End If
        End If
    Next
  
    Return lstRes
  
End Sub

Just in case someone else has the same issue.

Regards Roger
 
Last edited:
the lack of "."
What do you mean? What "pattern" do you have used? An empty string? If so, you should use ListFilesOnly instead of ListFiles.
If you pass *.* (even using Listfiles) you get all files, also those without extension.

Anyway, I changed the second line here:
Code: Copy
Private Sub GetFilteredFileList(Dir As String, Pattern As String, Sorted As Boolean, Ascending As Boolean) As List
    If Pattern = "*.*" Then

to:
Code: Copy
Private Sub GetFilteredFileList(Dir As String, Pattern As String, Sorted As Boolean, Ascending As Boolean) As List
    If Pattern = "*.*" Or Pattern.Length = 0 Then

(added the "new" class to the first post)
 
Last edited:
Sorry for not explaining myself sufficiently.
The pattern I am using is *.txt but this is not the problem.

This line causes the error:
FileBaseName = FileName.SubString2(0, FileName.LastIndexOf("."))

"FileName.txt" or "FileName.jpg" etc. is no problem.
"FileName" with not extension has no DOT therefore "FileName.LastIndexOf(".")" causes an error.

I bypassed the problem using the following IF statement:
If FileName.Contains(".") Then 'TEST FOR "."
 
Back
Top