Sign in to follow this  
Followers 0
liteswap

Log rotation UDF

1 post in this topic

#1 ·  Posted (edited)

Here's a log rotation UDF I developed to help me rotate my backup logs.

There are obvious ways it could be improved, not least to make it more generic (eg add a filename/file template parameter). But if I did all that, what would you guys need to do?

#cs ======================
Name:           _RotateLogs()
Description:    Archives logs every week to an archive folder.
Requirements:   #include <Date.au3>
                Log files with the date (YYYYMMDD formet) as the first eight characters of the filename
Parameters:     $sPath = path to log files
Returns:        nothing
Remarks:        Works for log files whose YYYYMMDD date falls between 20000101 and 20500101
#ce ======================

#include <date.au3>

Func _RotateLogs($sPath)
    Local $nHandle = FileFindFirstFile($sPath & "*.log")
    Local $sFileName, $sFileDate, $sToday = @YEAR & @MON & @MDAY
    If $nHandle = -1 Then
        MsgBox(0, "Error", "Cannot find any log files")
        Exit
    EndIf

    While 1
        $sFileName = FileFindNextFile($nHandle)
        If @error Then ExitLoop; no more files
        $sFileDate = StringLeft($sFileName, 8); get the date component of the filename
        If Number($sFileDate) > 0 Then; are the filename's digits numeric?
;~          Now double-check that the initial eight filename numerics are dates
            If Number($sFileDate) > 20000101 And Number($sFileDate) < 20500101 Then
                If _DateDiff("D", _DateStringWithSlashes($sFileDate), _DateStringWithSlashes($sToday)) > 7 Then; over a week old?
                    FileMove($sPath & $sFileName, $sPath & "archive\" & $sFileName, 8)
                EndIf
            EndIf
        EndIf
    WEnd
    FileClose($nHandle)
EndFunc;==>_RotateLogs

Func _DateStringWithSlashes($sString)
    If StringLen($sString) = 8 Then
        Return StringLeft($sString, 4) & "/" & StringMid($sString, 5, 2) & "/" & StringRight($sString, 2)
    Else
        MsgBox(0, "Error", "Bad date string length")
    EndIf
EndFunc;==>_DateStringWithSlashes

Enjoy. Comments?

[PS: Edited to correct spelling, tweak code]

Edited by liteswap

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