Getting newest files in a folder

I have been using code in Xojo 2018 r3 to pull the newest two photos from the Pictures folder which has been running very fast. The idea is to grab a business card that was just scanned within the last 60 seconds.

I now upgraded to Xojo 2022 r1 and the same code spins and spins forever.

I ran this on a computer with just a few files in the Pictures folder and it works fine, but the production computer has about 1200 files in the Pictures folder.

Can you help figure out this method works great using Xojo 2018 but so slow in Xojo 2022?

The code I am using is as follows:

dim theFolder as FolderItem = SpecialFolder.Pictures

if theFolder<>nil then
  if theFolder.Directory then
    for i as Integer=1 to theFolder.Count
      if theFolder.Item(i).Directory then  //if a folder, skip it
        //Next i
      ElseIf ( Right(theFolder.Item(i).Name,4) = ".jpg" Or Right(theFolder.Item(i).Name,5) = ".jpeg" ) And _
         ( theFolder.Item(i).ModificationDate.TotalSeconds > d.TotalSeconds - 60 ) then
        
        //add the jpeg files from the last 60 seconds
        listbox1.addrow (theFolder.Item(i).Name, theFolder.Item(i).ModificationDate.SQLDateTime)
        
      end if
    next
  end if
end if

There is a new property on FolderItem which is Interable, FolderItem.Children. I don’t know if it gives better performance, but it’s worth a try.

if theFolder <> Nil then
   if theFolder.Directory then
      for each f as FolderItem in theFolder.Children
         ...
      next
   end
end

Trying caching things in local variables and using those instead of constantly accessing FolderItem properties.

eg:

count = theFolder.Count

theChild = theFolder.Item(i)

theName = theChild.Name

theModificationDate = theChild.ModificationDate

What OS Platform ?

MacOS 10.14.6

The difference was night and day. Thank you Tim.

Awesome! Glad it helped.

FileListMBS is even faster.

Good to know.

I’d write this:

if theFolder.Item(i).Directory=True  then Continue                   // Skip it

Makes the logic easier to follow.