126 lines
4.2 KiB
Batchfile
126 lines
4.2 KiB
Batchfile
|
@echo off
|
||
|
rem The author of the scipt can be found here http://www.datori.org/?p=7
|
||
|
rem Poor man's logrotate for Windows command shell
|
||
|
|
||
|
rem Usage: cmd /v:on /c logrotate.cmd [-f <configfile>]
|
||
|
rem where <configfile> is a fully qualified name of a configuration file.
|
||
|
rem If <configfile> is not specified an attempt is made to locate
|
||
|
rem logrotatew.conf in the current directory. If it is not found the script
|
||
|
rem fails.
|
||
|
|
||
|
rem A configuration file specifies log file name patterns and how many
|
||
|
rem generations to keep. For example, a line in the configuration file:
|
||
|
rem "c:\temp\*.log 5" will cause rotation of every file with the extension
|
||
|
rem ".log" in the specified directory, keeping 5 generations of rotated files.
|
||
|
rem The rotated files can be optionally compressed. See comments in the sample
|
||
|
rem logrotatew.conf for more details.
|
||
|
|
||
|
rem The script rotates the los specified by the configuration file each time
|
||
|
rem it is invoked. Schedule it in Windows Scheduler as often as you need, e.g.
|
||
|
rem at 23:55 /every:Su cmd /v:on /c logrotatew.cmd -f c:\logrotatew.conf
|
||
|
|
||
|
rem NOTE: the script must be invoked with "cmd /v:on /c logrotate.cmd [-f <configfile>]"
|
||
|
rem otherwise it will not work properly!
|
||
|
|
||
|
rem Copyright (c) Nick Ivanov/datori 2007
|
||
|
rem All rights reserved.
|
||
|
rem
|
||
|
rem Redistribution and use in source and binary forms, with or without
|
||
|
rem modification, are permitted provided that the following conditions
|
||
|
rem are met:
|
||
|
rem 1. Redistributions of source code must retain the above copyright
|
||
|
rem notice, this list of conditions and the following disclaimer.
|
||
|
rem 2. Redistributions in binary form must reproduce the above copyright
|
||
|
rem notice, this list of conditions and the following disclaimer in the
|
||
|
rem documentation and/or other materials provided with the distribution.
|
||
|
rem 3. The name of the author may not be used to endorse or promote products
|
||
|
rem derived from this software without specific prior written permission.
|
||
|
rem
|
||
|
rem THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||
|
rem IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||
|
rem OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||
|
rem IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||
|
rem INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||
|
rem NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||
|
rem DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||
|
rem THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||
|
rem (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||
|
rem THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||
|
|
||
|
|
||
|
rem === Begin main ===
|
||
|
|
||
|
setlocal
|
||
|
|
||
|
rem Default configuration file
|
||
|
set _CONFIGFILE=logrotatew.conf
|
||
|
|
||
|
rem See if another config file is specified
|
||
|
|
||
|
if "%1" == "-f" set _CONFIGFILE=%2
|
||
|
|
||
|
if not "%1" == "" if not "%1" == "-f" (echo [err] Invalid switch: "%1" & exit /b 1)
|
||
|
|
||
|
rem See if config file exists
|
||
|
if not exist !_CONFIGFILE! echo [err] Configuration file !_CONFIGFILE! not found & exit /b 1
|
||
|
|
||
|
rem Parse the file and invoke rotation subprogram for each file name that matches the pattern
|
||
|
for /f "tokens=1,2,3,*" %%f in (!_CONFIGFILE!) do call :f_cfg %%f %%g %%h
|
||
|
|
||
|
endlocal
|
||
|
goto :eof
|
||
|
|
||
|
rem ==== End main ====
|
||
|
|
||
|
|
||
|
rem === Begin f_cfg ===
|
||
|
:f_cfg
|
||
|
|
||
|
rem Parse configuration file
|
||
|
|
||
|
rem Skip comments and blank lines
|
||
|
if "%1" == "" goto :eof
|
||
|
if /I "%1" == "rem" goto :eof
|
||
|
|
||
|
if /I "%1" == "compresscmd" set _COMPR_=%2 & goto :eof
|
||
|
if /I "%1" == "compressext" set _EXT_=%2 & goto :eof
|
||
|
if /I "%1" == "compressoptions" set _COPTS_=%2 & goto :eof
|
||
|
|
||
|
rem This must be a file specification
|
||
|
for %%n in (%1) do call :f_rotate %%n %2
|
||
|
|
||
|
goto :eof
|
||
|
|
||
|
rem ==== End f_cfg ====
|
||
|
|
||
|
rem === Begin f_rotate ===
|
||
|
:f_rotate
|
||
|
|
||
|
rem Delete the last file in sequence
|
||
|
if exist %1.%2 del /q %1.%2
|
||
|
if exist %1.%2.!_EXT_! del /q %1.%2.!_EXT_!
|
||
|
|
||
|
rem Rotate non-current files
|
||
|
for /l %%i in (%2,-1,2) do (
|
||
|
set i=%%i
|
||
|
set /a j=i-1
|
||
|
for /f %%f in ("%1") do (
|
||
|
if exist %1.!j!.!_EXT_! ren %1.!j!.!_EXT_! %%~nxf.%%i.!_EXT_!
|
||
|
if exist %1.!j! (
|
||
|
ren %1.!j! %%~nxf.%%i
|
||
|
if not "!_COMPR_!" == "" !_COMPR_! !_COPTS_! %1.%%i
|
||
|
)
|
||
|
)
|
||
|
)
|
||
|
|
||
|
rem Rotate current file
|
||
|
for /f %%f in ("%1") do (
|
||
|
ren %1 %%~nxf.1
|
||
|
if not "!_COMPR_!" == "" !_COMPR_! !_COPTS_! %1.1
|
||
|
)
|
||
|
|
||
|
goto :eof
|
||
|
|
||
|
rem ==== End f_rotate ====
|
||
|
|