How to Filter ReadList to retrieve only images file in a folder

stefanoa

Active Member
Licensed User
Longtime User
How to Filter ReadList to retrieve only images files in a folder

what's the best way to retrieve the first image in a folder? (jpg, gif, png, ecc.)
I'm trying to use File.ReadList but there is not an option FILTER, to set the files extension as filter.
In FileDialog for example there's a properties:
ex. fd.FileFilter = ".jpg, .gif, .png, .jpeg"

B4X:
Dim fileList As List

fileList = File.ReadList(path1 ,"*.jpg")    '<< set a filter to list only image??     
fileList.Sort(True)
If fileList.Size > 0 Then
..............

any suggestion?
 
Last edited:

Informatix

Expert
Licensed User
Longtime User
what's the best method to retrieve the first image in a folder? (jpg, gif, png, ecc.)
I'm trying to use File.ReadList but there is not an option FILTER, to set the files extension as filter.
In FileDialog for example there's a properties:
ex. fd.FileFilter = ".jpg, .gif, .png, .jpeg"

B4X:
Dim fileList As List

fileList = File.ReadList(path1 ,"*.jpg")    '<< set a filter to list only image??     
fileList.Sort(True)
If fileList.Size > 0 Then
..............

any suggestion?

Here's an excerpt of the function ReadFolder (FileExplorer class). Allowed extensions are stored in the list lstFiltre:
B4X:
lst = File.ListFiles(Chemin)
      lstFichiers.Clear
      lstD.Initialize 'Dir list
      lstF.Initialize 'File list
      If Chemin <> "/" Then lstFichiers.AddSingleLine2("/ ..", "..")
      For i = 0 To lst.Size - 1
         If File.IsDirectory(Chemin, lst.Get(i)) Then
            lstD.Add(lst.Get(i))
         Else
            If lstFiltre.Size = 0 Then
               lstF.Add(lst.Get(i))
            Else
               Dim NomFichier As String
               NomFichier = lst.Get(i)
               NomFichier = NomFichier.ToLowerCase
               For f = 0 To lstFiltre.Size - 1
                  If NomFichier.EndsWith(lstFiltre.Get(f)) Then
                     lstF.Add(lst.Get(i))
                     Exit
                  End If
               Next
            End If
         End If
      Next
 
Upvote 0

stefanoa

Active Member
Licensed User
Longtime User
Excellent class.. but for now i've used this approach (a litte slow.. to optimize):

B4X:
Sub searchFirstImageInFolder(path1 As String) As String

Dim lstFilter As List
lstFilter.Initialize
lstFilter.AddAll(Array As String(".jpg", ".gif", ".png", ".jpeg" ))

Dim fileList As List
fileList.Initialize
fileList = File.listfiles(path1)      '<<--- load files from folder path1

Dim tmpFileName As String
tmpFileName=""
   
For i = 0 To fileList.Size - 1
      tmpFileName =fileList.Get(i)
      For f = 0 To lstFilter.Size - 1
          If tmpFileName.EndsWith(lstFilter.Get(f)) Then
            i=fileList.Size - 1 '<<--- force exit from 1' for
            Exit
         Else 
            tmpFileName=""
         End If
      Next
Next

Return tmpFileName      '--- return 1' image in folder
  
End Sub

thanks
 
Upvote 0

stefanoa

Active Member
Licensed User
Longtime User
ok!

the correct code should be:
B4X:
....
If tmpFileName.ToLowerCase.EndsWith(lstFilter.Get(f)) Then
....
thanks
 
Upvote 0
Top