search First Image In Folder

stefanoa

Active Member
Licensed User
Longtime User
hi,
i'm searching for a faster method to retrieve the first image in a folder.
Any suggestion?

now i have this code but is very slow:
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.ToLowerCase.EndsWith(lstFilter.Get(f)) Then
            i=fileList.Size - 1 '<<--- FOUND! then force exit from 1' for
            Exit
         Else 
            tmpFileName=""
         End If
      Next
   Next
     
   Return tmpFileName      '--- return 1' image in folder
  
End Sub
 

kickaha

Well-Known Member
Licensed User
Longtime User
Instead of stepping through a filter list on each filename, have a look at Regex.IsMatch. There are lots of examples on the web that show how to check for file extensions, like this
B4X:
\.(?i:)(?:jpg|gif|png|jpeg)$
 
Upvote 0

stefanoa

Active Member
Licensed User
Longtime User
@kickaha: thanks for suggestion.

This is my current solution:

B4X:
Sub searchFirstImageInFolder(path1 As String) As String

    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)
      If IsImage(tmpFileName) = True  Then
         Exit
      Else 
         tmpFileName=""
      End If
   Next
     
   Return tmpFileName      '--- return 1' image in folder
  
End Sub

B4X:
Sub IsImage(fileName As String) As Boolean
   If  Regex.IsMatch("^.*\.(jpg|JPG|gif|GIF|png|PNG|jpeg|JPEG)$", fileName)=True Then
      Return True
   End If
   Return False
End Sub
 
Upvote 0

stefanoa

Active Member
Licensed User
Longtime User
@Informatix: not very fast...
the problem is that it have to read all the files in the folder, until it finds an image

have you a little example of listfile from reflection library?.. i don't see a tutorial for this library..
thanks
 
Last edited:
Upvote 0

Gargoyle

Member
Licensed User
Longtime User
Without resorting to Reflection I'd suggest a slight change to your loop in 'searchFirstImageInFolder'

B4X:
    For i = 0 To fileList.Size - 1
        If IsImage(fileList.Get(i)) = True Then
            tmpFileName = fileList.Get(i)
            Exit
        End If
    Next

You're still only checking the file once, but not creating/reassigning a string every time, which is (slightly) time consuming.
 
Last edited:
Upvote 0

Informatix

Expert
Licensed User
Longtime User
I looked at the source code of the listFiles variants and I don't think they will give you a faster execution.
I'm going to release today or tomorrow a library that should help you to collect these files in the background and put them in a cache, so you won't have to read them again and again.
 
Upvote 0

stefanoa

Active Member
Licensed User
Longtime User
@Informatix
some news about your message? (library that should help you to collect these files in the background and put them in a cache)..
thanks
 
Upvote 0
Top