Sign in to follow this  
Followers 0
erik7426

Sorting an array by file date

11 posts in this topic

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 AM

FILE-B 07/31/2008 2:00 AM

FILE-C 07/31/2008 3:00 AM

FILE-B would be the one I would want to keep.

Share this post


Link to post
Share on other sites



_DateDiff()

Share this post


Link to post
Share on other sites

Perhaps use a two-dimensional array with _ArraySort?


My UDFs: ExitCodes

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Is the date/time stamp in the filename? or are you pulling it from the "created" or "modified" file properties?

Share this post


Link to post
Share on other sites

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().

Share this post


Link to post
Share on other sites

#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

Share this post


Link to post
Share on other sites

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

:P


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

Share this post


Link to post
Share on other sites

That penguin is always following me.

Share this post


Link to post
Share on other sites

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

:P

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

Share this post


Link to post
Share on other sites

I had to modify it slightly to get it to work.

Doh! :P

Here is what I ended up with...

Glad it helped.

:P


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

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0