Jump to content

FileDeleteByAge with retention option


Roland
 Share

Recommended Posts

As an system administrator I have a lot of log, bak and tmp files. In most situations I want to clean up the oldest files, but retaining te last log files.

So I made a script wich looks at the age and number of files to be retained.

Example:

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


Delete al files in TempDir with mask ~DF*.TMP minimum 24 hours old, but keeping 7 versions
_FileDeleteByAge(@TempDir, "~DF*.TMP", 24, 7)

UDF:

;======================================================================
; _FileDeleteByAge
; Author     Roland Raijmakers
; Date       28-2-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.
Func _FileDeleteByAge($Path, $FileMask = "*.*", $Age = 14, $FileRetention = 0)
    Dim $t[10]
    Local $Date
    Local $Files
    Local $Files2[1][4]
    Local $FileCount
    Local $Now
    $Now = _NowCalc()
    $Files = _FileListToArray($Path, $FileMask, 1)
    If $Files[0] > 0 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
                    $Files2[$i][3] = "DELETE"
                    FileDelete($Path & "\" & $Files2[$i][0])
            Endif
        Next
        ; _ArrayDisplay($Files2)          ; Display Error for Testing Purposes
    Endif

EndFunc   ;==>_FileDeleteByAge
Edited by Roland
Link to comment
Share on other sites

Welcolme to the forums Posted Image

It looks good, but i get an error if there is no files

: ==> Subscript used with non-Array variable.:

If $Files[0] > 0 Then

If $Files^ ERROR

->10:07:21 AutoIT3.exe ended.rc:1

So i modified script with a return @error and the number of files deleted.

#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
; 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.

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 ( )
Edited by wakillon

AutoIt 3.3.14.2 X86 - SciTE 3.6.0 - WIN 8.1 X64 - Other Example Scripts

Link to comment
Share on other sites

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 ( )
Edited by Roland
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...