List of files by creation time
BlitzMax Forums/BlitzMax Programming/List of files by creation time
| ||
I'm looking for a way to load a list of files in a folder, and then sort them by creation time. Last modified time would be ok too. My intention is to delete a certain number of old files in the folder every now and again. |
| ||
FileTime returns an integer : "The time the file at path was last modified" Does anybody know how to interpret this number? Here's an example: C:\BlitzMax = 1294508154 What does that mean? |
| ||
Its the time/datestamp in unix format (the number of seconds since 1st January 1970). Try this. |
| ||
Some rounding is used 32-bit signed integer = 2147483647 seconds 35791394 minutes 596523 hours 24855 days 68 years 2011-1970 = 41 years 68 - 41 = 27 years So in 27 years, it won't work anymore in BlitzMax. Is that right? Thanks for the link, it could be useful. Last edited 2011 |
| ||
Probably, but I'll be too old to care by then. |
| ||
The UNIX timestamp overflows at 03:14:07 UTC 2038-01-19. All 32-bit Unix/Linux systems are affected by this as well, but it's a non-issue for 64-bit operating systems and applications. |
| ||
Why do people create these systems!!! Probably, but I'll be too old to care by then. I guess that explains it. |
| ||
FileSize does not work on folders. Is there an alternative? Other than summing the sizes of all files myself. |
| ||
Here is my solution. Somewhat tested.Private Type TFileRecord Final Field name:String Field time:Int Field size:Long Function Create:TFileRecord(name:String, time:Int, size:Long) Local record:TFileRecord = New TFileRecord record.name = name record.time = time record.size = size Return record End Function Function CompareByTime:Int(o1:Object, o2:Object) Local f1:TFileRecord = TFileRecord(o1) Local f2:TFileRecord = TFileRecord(o2) Assert f1 And f2 'a greater time means the file is YOUNGER If f1.time < f2.time Then Return 1 If f1.time > f2.time Then Return -1 Return 0 End Function End Type Public Function trim_logs(size:Long) Local files:String[] = LoadDir("Logs") Local records:TList = New TList Local size_sum:Long = 0 'scan all files and store their name, size and time For Local file:String = EachIn files Local path:String = "Logs\" + file If Not (FileType(path) = FILETYPE_FILE) Then Continue Local size = FileSize(path) size_sum :+ size Local time = FileTime(path) records.AddLast(TFileRecord.Create(file, time, size)) Next 'sort according to age records.Sort(True, TFileRecord.CompareByTime) 'the latest record should never be deleted, regardless of size records.RemoveFirst() 'delete records starting from oldest, until trim size is reached While size_sum > size And Not records.IsEmpty() Local record:TFileRecord = TFileRecord(records.RemoveLast()) DeleteFile "Logs\" + record.name size_sum :- record.size WEnd End Function Last edited 2011 |