Best way to read file dates and delete them?

  1. 2 weeks ago

    Tim S

    Mar 21 Pre-Release Testers, Xojo Pro Phoenix Arizona USA

    Hello all,

    Can anyone suggest either sample code or methods to iterate through files in a folder, determine their date and delete as necessary?

    Thanks
    Tim

  2. Markus R

    Mar 22 Pre-Release Testers, Xojo Pro Europe / Germany / Lower Saxon...

    FolderItem have .CreationDateTime and .ModificationDateTime
    FolderItem.Remove

  3. Kem T

    Mar 22 Pre-Release Testers, Xojo Pro, XDC Speakers, MVP Connecticut

    If on Mac or Linux, you might find it faster to use the find command through a Shell.

  4. Douglas H

    Mar 22 Pre-Release Testers, Xojo Pro

    If you have a MBS license, the FileListMBS class is significantly faster than Xojo. Or at least was; I think Xojo may now use different APIs on macOS which may at least partially mitigate that. You can benchmark on your own machine with a FileList Benchmark demo program in their Util/FileList/ example subfolder.

    Personally, I always prefer to first iterate over a list and build an array of items to delete, and only when complege iterate over that array and perform delete actions instead of deleting entries while iterating the original list. YMMV.

  5. Beatrix W

    Mar 23 Pre-Release Testers, Third Party Store Europe (Germany)

    FileListMBS still is significantly faster. It also doesn't show the beachball if you iterate over a lot of files.

    Here is a simple routine to delete files that are older than 30 days:

    dim theFileList as new FileListMBS(theAppSupportFolder)
    dim FileCount as Integer = theFileList.Count - 1
    
    for currentFile as Integer = 0 to FileCount
      
      dim theFile as FolderItem = theFileList.TrueItem(currentFile)
      if theFile = Nil then Continue
      
      dim theTime as Integer = 60 * 60 * 24 * 30
      'delete logs older than 30 days
      dim FileCreationDate as DateTime = theFile.CreationDateTime
      if theFile.Name.Left(11) = LogName and currentDate.SecondsFrom1970 - FileCreationDate.SecondsFrom1970 > theTime then
        theFile.Remove
      end if
      
    next

    I had a very stupid bug where a helper was was constantly starting and quitting. The app used 100% of processor power because I was using Xojo functionality and not MBS one in the above code.

or Sign Up to reply!