Android Question How to read file list per name/size/date ?

amorosik

Expert
Licensed User
To list pdf file in current directory i use this code

B4X:
For Each f As String In File.ListFiles(log_dir_pdf )
    If f.ToLowerCase.EndsWith("pdf") Then 
        flexListaPdf.AddRow(Array As Object( f, "0","0"),True)
        End If
    bext

How to obtain a file list order by name, size, date?
 

Chris2

Active Member
Licensed User
Longtime User
I don't know what your flexListaPdf is, but with a normal list:

B4X:
Type FileInfo (name As String, lastModified As Long, size as long)   'size in bytes

Public Sub CreateFileInfo (name As String, lastModified As Long, size As Long) As FileInfo
    Dim t1 As FileInfo
    t1.Initialize
    t1.name = name
    t1.lastModified = lastModified
    t1.size = size
    Return t1
End Sub


Private Sub SortedList as List

    Dim lst As List
    lst.Initialize

    For Each f As String In File.ListFiles(log_dir_pdf )

        If f.ToLowerCase.EndsWith("pdf") Then
    
            Dim fInfo As FileInfo = CreateFileInfo(f, File.LastModified(log_dir_pdf, f), File.Size(log_dir_pdf, f))
            lst.Add(fInfo)
            
        End If
    
    Next
    
    lst.SortType("name", True)    'or "lastModified", or "size"
    
    Return lst
    
End Sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
How to obtain a file list order by name, size, date?
Sort it by yourself.

File.Listfiles only provides filenames.
To get the Size you need to get it first with
B4X:
dim filesize as Long=File.Size(Dir As String, FileName As String) As Long
To get the Date you need to get it first with
B4X:
    Dim d As Long = File.LastModified(path, filename)

Create your own list which you can sort.
 
Upvote 0
Top