Logging RollOver (keep "x" logs) |
This is an example of how you can maintain a history of the past 'x' logs:
@rem --- Prepare for logging (max 1+15=16 logs) --- @echo off set LogDir=C:\tmp\Log md "%LogDir%" >nul 2>&1 set LogFile=%LogDir%\SomeLog.TXT set LogFileMask=%LogDir%\SomeLog?.TXT @REM --- Actually do the rollover --- call :RollOverLogs "%LogFileMask%" 15 @rem --- Now do "stuff" and log --- echo [%date% %time%] Now doing stuff > "%LogFile%" 2>&1 goto :EOF @rem ======================[ Version 07.100] ================================== :RollOverLogs @rem === Parm 1: What backup logs look like, example "c:\tmp\SomeFile?.Log" (? replaced by number) @rem === Parm 2: Number of logs @rem ========================================================================== SetLocal ENABLEDELAYEDEXPANSION set LogMask=%~1 set LogNumbBackups=%~2 for /L %%b in (%LogNumbBackups%, -1, 1) do ( @rem *** Work out log numbers and names ********************** set This=%%b set /a Prev=!This! - 1 if "!Prev!" == "0" set Prev= call :LogName "FileThis", "%LogMask%", "!This!" call :LogName "FilePrev", "%LogMask%", "!Prev!" for %%f in ("!FileThis!") do set FileThisSN=%%~nf%%~xf @rem *** Move "previous" log to this level ******************* if exist "!FileThis!" del "!FileThis!" >nul 2>&1 if exist "!FilePrev!" ren "!FilePrev!" "!FileThisSN!" >nul 2>&1 ) EndLocal goto :EOF @rem ========================================================================== :LogName @rem Helper routine only to be used by ":RollOverLogs" @rem ========================================================================== set LogMask2=%~2 set NewVal=%~3 set %~1=!LogMask2:?=%NewVal%! goto :EOF