Skip to content

Instantly share code, notes, and snippets.

@urin
Last active January 22, 2024 08:56
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save urin/3b68e8388cc34a688d13 to your computer and use it in GitHub Desktop.
Save urin/3b68e8388cc34a688d13 to your computer and use it in GitHub Desktop.
@echo off
::
:: logrotate.bat - Log rotation for windows
::
:: Usage: logrotate.bat filename maxfilecount [maxfilesize]
::
:: filename : The file name to rotate.
:: maxfilecount: Number of backup files.
:: maxfilesize : Rotated if the size of file exceeds specified size.
:: (Default is 0. Unit notation is available (K, M and G))
::
:: Example:
:: logrotate.bat path\to\file 1
:: The file is renamed to path\to\file.1.
:: The file path\to\file.1 is deleted if exists.
:: logrotate.bat path\to\file 100 128
:: The file is rotated from path\to\file.1 to file\to\file.100
:: if the size exeeds 128 bytes.
:: logrotate.bat path\to\file 10 10M
:: The file is rotated from path\to\file.1 to file\to\file.10
:: if the size exeeds 10 Mega-bytes.
::
:: Copyright © 2015 Urin. Licensed under MIT.
::
setlocal enabledelayedexpansion
if "%~1" == "" (exit /b 1) else (set file=%~1)
if "%~2" == "" (exit /b 1) else (set /a maxfilecount=%~2)
if "%~3" == "" (set /a maxfilesize=0) else (set /a maxfilesize=%~3)
if not "%maxfilesize%" == "%maxfilesize:K=%" (
set /a maxfilesize=%maxfilesize:K= * 1024%
)
if not "%maxfilesize%" == "%maxfilesize:M=%" (
set /a maxfilesize=%maxfilesize:M= * 1048576%
)
if not "%maxfilesize%" == "%maxfilesize:G=%" (
set /a maxfilesize=%maxfilesize:G= * 1073741824%
)
if exist "%file%" (
for %%F in ("%file%") do (
set filename=%%~nxF
set size=%%~zF
)
if !size! geq %maxfilesize% (
for /l %%i in (%maxfilecount%,-1,2) do (
set /a old=%%i - 1
if exist "%file%.!old!" (move /y "%file%.!old!" "%file%.%%i" > nul)
)
rename "%file%" "!filename!.1" > nul
)
)
endlocal
@cgopalai-tibco
Copy link

Hi Urin,

I am Chitra from working as release engineer for TIBCO TDV Product. I found the code you published on your GitHub Gist site at https://gist.github.com/urin/3b68e8388cc34a688d13 which I would like to use for our product. It currently does not appear to have a license associated with it. I would like to use this code but only if it has an open source license. Would you be able to let me know what license this code is under?

reference: https://zebracatzebra.com/oss/getting-the-gist-of-github-gist-licensing/
Thanks
Chitra

@urin
Copy link
Author

urin commented Mar 9, 2021

Added license information. Please star this code, thanks!

Revisions · logrotate.bat

@tachylatus
Copy link

The /a switch on maxfilesize bugs out when using unit notation.
I have forked and made a fix here: https://gist.github.com/tachylatus/9c4894016ceaf09886d3db726c297b21/revisions

@urin
Copy link
Author

urin commented Sep 18, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment