to wakillon
Thanx for the adjustment. I didn't test it for No files.
#include <file.au3>
#include <array.au3>
#include <date.au3>
$_FileDeleteByAge = _FileDeleteByAge ( @TempDir, "~DF*.tmp", 24, 0 )
ConsoleWrite ( "-->-- @error : " & @error & @Crlf )
ConsoleWrite ( "-->-- Files Delete By Age : " & $_FileDeleteByAge & @Crlf )
;======================================================================
; _FileDeleteByAge
; Author Roland Raijmakers
; Date 28-2-2011
; Change: Error Handler by Wakillon 1-3-2011
; Parameters $Path : Path to files
; $FileMask : File mask for files to be deleted
; $Age : Minimum Age (in hours) before files are deleted
; $FileRetention : Number of Files to be kept
; Function Deletes files in $FileMask to be deleted.
; Intented to cleanup old log, bak and tmp files
; There are two triggers before a file is deleted, $Age (in hours) and $FileRetention
; If both are met then a file is deleted.
; Return Values:
; @Error -1 No files found
; 0 or greater number Non-Deleted files
; Return No of deleted Files
Func _FileDeleteByAge ( $Path, $FileMask="*.*", $Age=14, $FileRetention=0 )
Local $t[10], $Files2[1][4]
Local $Date, $FileCount, $_FileTotDelete=0, $_NotDeleted=0
$Now = _NowCalc()
$Files = _FileListToArray($Path, $FileMask, 1)
If Not @error Then
$FileCount = ubound($Files)-1
Redim $Files2[$FileCount+1][4]
$Files2[0][0]=$FileCount
For $i = 1 To $FileCount
$t = FileGetTime($Path & "\" & $Files[$i], 0, 0)
$Date = StringFormat("%02i/%02i/%02i %02i:%02i:%02i", $t[0], $t[1], $t[2], $t[3], $t[4], $t[5])
$Files2[$i][0] = $Files[$i]
$Files2[$i][1] = $Date
$Files2[$i][2] = _DateDiff('H', $Date, $Now)
Next
_Arraysort($Files2,1,1,0,1) ;Sort Array Ascending on column 1 with row 0
For $i = 1 To $FileCount
If $i > $FileRetention And $Files2[$i][2] > $Age Then
$_FileTotDelete += 1
$Files2[$i][3] = "DELETE"
$_FileDelete = FileDelete($Path & "\" & $Files2[$i][0])
If Not $_FileDelete Then $_NotDeleted += 1
Endif
Next
SetError ( $_NotDeleted )
Return $_FileTotDelete - $_NotDeleted
Endif
SetError ( -1 )
EndFunc ;==> _FileDeleteByAge ( )