erik7426 Posted July 31, 2008 Share Posted July 31, 2008 Is it possible to sort an array by the file modified date?If not, here is what I am trying to do:I have a few files. Usually there are two but sometimes there are three. The filenames are NOT static. I need to compare the date modified stamp on each file and delete all but the oldest one. Just to be clear we're talking hours and minutes differences between the files.So, basically out of these files...FILE-A 07/31/2008 2:15 AMFILE-B 07/31/2008 2:00 AMFILE-C 07/31/2008 3:00 AMFILE-B would be the one I would want to keep. Link to comment Share on other sites More sharing options...
Paulie Posted July 31, 2008 Share Posted July 31, 2008 _DateDiff() Link to comment Share on other sites More sharing options...
Gigglestick Posted July 31, 2008 Share Posted July 31, 2008 Perhaps use a two-dimensional array with _ArraySort? My UDFs: ExitCodes Link to comment Share on other sites More sharing options...
erik7426 Posted August 1, 2008 Author Share Posted August 1, 2008 Perhaps use a two-dimensional array with _ArraySort?I'm new to arrays. What would the code look like to build an array that contained the files in a folder and the date/time stamp for each file?Thanks for the help. Link to comment Share on other sites More sharing options...
Paulie Posted August 1, 2008 Share Posted August 1, 2008 Is the date/time stamp in the filename? or are you pulling it from the "created" or "modified" file properties? Link to comment Share on other sites More sharing options...
erik7426 Posted August 1, 2008 Author Share Posted August 1, 2008 Is the date/time stamp in the filename? or are you pulling it from the "created" or "modified" file properties?It is not part of the file name. I am pulling it using FileGetTime(). Link to comment Share on other sites More sharing options...
weaponx Posted August 1, 2008 Share Posted August 1, 2008 #include <file.au3> $current_timestamp = @YEAR & @MON & @MDAY & @HOUR & @MIN & @SEC $oldest_timestamp = $current_timestamp $oldest_filename = "" $oldest_index = -1 ;Retrieve list of files $aFiles = _FileListToArray($sPath[, $sFilter = "*"[, $iFlag = 0]]) ;Find oldest file For $X = 1 to $aFiles[0] $file_timestamp = FileGetTime($aFiles[$X], 1) If $file_timestamp < $oldest_timestamp Then $oldest_timestamp = $file_timestamp $oldest_filename = $aFiles[$X] $oldest_index = $X EndIf Next ;Delete everything but the oldest file For $X = 1 to $aFiles[0] If $X <> $oldest_index Then FileDelete($aFiles[$X]) Next Link to comment Share on other sites More sharing options...
PsaltyDS Posted August 1, 2008 Share Posted August 1, 2008 It is not part of the file name. I am pulling it using FileGetTime(). Try something like this: #cs FILE-A 07/31/2008 2:15 AM FILE-B 07/31/2008 2:00 AM FILE-C 07/31/2008 3:00 AM #ce Global $avFiles[4] = [3, "FILE-A", "FILE-B", "FILE-C"] Global $iOldestTime = 99999999999999; YYYYMMDDhhmmss Global $iOldestIndex = 0; Array index of oldest file ; Find the oldest file For $n = 1 To $avFiles[0] $iFileTime = Number(FileGetTime($avFiles[$n], 0, 1)) If $iFileTime < $iOldestTime Then $iOldestTime = $iFileTime $sOldestFile = $n EndIf Next ; Delete all but the oldest file If $iOldestIndex Then For $n = 1 To $avFiles[0] If $n <> $iOldestIndex Then FileDelete($avFiles[$n]) Next Else MsgBox(16, "Error", "Failed to find an oldest file.") EndIf Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law Link to comment Share on other sites More sharing options...
weaponx Posted August 1, 2008 Share Posted August 1, 2008 That penguin is always following me. Link to comment Share on other sites More sharing options...
erik7426 Posted August 1, 2008 Author Share Posted August 1, 2008 Try something like this: #cs FILE-A 07/31/2008 2:15 AM FILE-B 07/31/2008 2:00 AM FILE-C 07/31/2008 3:00 AM #ce Global $avFiles[4] = [3, "FILE-A", "FILE-B", "FILE-C"] Global $iOldestTime = 99999999999999; YYYYMMDDhhmmss Global $iOldestIndex = 0; Array index of oldest file ; Find the oldest file For $n = 1 To $avFiles[0] $iFileTime = Number(FileGetTime($avFiles[$n], 0, 1)) If $iFileTime < $iOldestTime Then $iOldestTime = $iFileTime $sOldestFile = $n EndIf Next ; Delete all but the oldest file If $iOldestIndex Then For $n = 1 To $avFiles[0] If $n <> $iOldestIndex Then FileDelete($avFiles[$n]) Next Else MsgBox(16, "Error", "Failed to find an oldest file.") EndIf Thanks for the help. I had to modify it slightly to get it to work. Here is what I ended up with... $avFiles = _FileListToArray("C:\temp123\S10098", "*PNBCD001*") Global $iOldestTime = 99999999999999; YYYYMMDDhhmmss Global $iOldestIndex = 0; Array index of oldest file ; Find the oldest file For $n = 1 To $avFiles[0] $iFileTime = Number(FileGetTime($avFiles[$n], 0, 1)) If $iFileTime < $iOldestTime Then $iOldestTime = $iFileTime ;$sOldestFile = $n $iOldestIndex = $n EndIf Next ; Delete all but the oldest file If $iOldestIndex > 0 Then For $n = 1 To $avFiles[0] If $n <> $iOldestIndex Then FileDelete($avFiles[$n]) Next Else MsgBox(16, "Error", "Failed to find an oldest file.") EndIf Link to comment Share on other sites More sharing options...
PsaltyDS Posted August 1, 2008 Share Posted August 1, 2008 I had to modify it slightly to get it to work.Doh! Here is what I ended up with...Glad it helped. Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now