Jump to content

Recommended Posts

Posted

Hello,

I am trying to delete the *.trn files in the c:\temp1 folder that are older than 10 hours. Can you please take a look at the code and offer corrections? I am running this on a Windows 7 with AutoIt v 3.3.6.1.

When I run a simple FileDelete("c:\temp1\*.trn") it works fine but would like to only delete them if older than 10 hours.

#include <file.au3>
#include <date.au3>

$sourceFolder = "C:\temp1"
$fileList = _FileListToArray($sourceFolder, "*.trn", 1)

For $X = 1 to $fileList[0]
    $tDate = FileGetTime($sourceFolder & "\" & $fileList[$X], 1, 0)
    $fDate = StringFormat("%s/%s/%s %s:%s:%s", $tDate[0],$tDate[1],$tDate[2],$tDate[3],$tDate[4],$tDate[5])
    If _DateDiff('h', $fDate, _NowCalc()) > 10 Then FileDelete($sourceFolder & "\" & $fileList[$X])
Next
Posted

Only thing missing is some error checkup to prevent a terminal error if there where no files found by _FileListToArray.

Other than that its fine.

Trivial(personal) additional changes I would do:

- added int() to turn the double-type value from _DateDiff() into a real int().

- more consistent variable type use in the variable names.

#include <file.au3>
#include <date.au3>

test()
Func test()
    Local $sSourceFolder = @TempDir
    Local $aFileList = _FileListToArray($sSourceFolder, "*.*", 1)
;~  If @error Then Return SetError(@error) ;; (ErrorCheckup1) check doc for error value's.
    If Not @error Then ;; Or (ErrorCheckup2)
        Local $aDate, $sDate, $nDiff
        For $i = 1 To $aFileList[0]
            $aDate = FileGetTime($sSourceFolder & "\" & $aFileList[$i], 1, 0)
            $sDate = StringFormat("%s/%s/%s %s:%s:%s", $aDate[0], $aDate[1], $aDate[2], $aDate[3], $aDate[4], $aDate[5])
            $nDiff = Int(_DateDiff('h', $sDate, _NowCalc())) ;; make double to int. (not needing double-type in this case)
            If $nDiff > 10 Then ConsoleWrite($nDiff & ', ' & $aFileList[$i] & @CRLF)
;~          FileDelete($sSourceFolder & "\" & $aFileList[$i])
        Next
    EndIf
EndFunc

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Posted (edited)

StringFormat() is a bit of a dog...

This:

$sDate = FileGetTime($sSourceFolder & "\" & $aFileList[$i], 1, 1)
$sDate = StringRegExpReplace($sDate, "(....)(..)(..)(..)(..)(..)", "\1/\2/\3 \4:\5:\6")

is about 400% faster than these two lines:

$aDate = FileGetTime($sSourceFolder & "\" & $aFileList[$i], 1, 0)
$sDate = StringFormat("%s/%s/%s %s:%s:%s", $aDate[0], $aDate[1], $aDate[2], $aDate[3], $aDate[4], $aDate[5])

typo

Edited by Spiff59
Posted (edited)

Hehe. Never seen StringFormat() as a potential slow function. Never tested it of course either.

One thing though. Merging commands is in general not a good in examples I think. (Apart from the lesser readability, in some cases it can be slower* than a unmerged command version.)

*) not talking really significant speed differences in general.

Edited by MvGulik

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

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
×
×
  • Create New...