Best way to read file dates and delete them?

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

FolderItem have .CreationDateTime and .ModificationDateTime
FolderItem.Remove

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

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.

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:

[code]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[/code]

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.