Windows: Rotação de Log - Log Rotation

From Wiki

Dica por Johan Känngård - http://johankanngard.net/2008/12/11/log-file-roller-for-microsoft-windows/

Simples script para rotacionar arquivos de log

Modo de usar:

rollLog LOGDIRECTORY LOGFILENAME

Exemplo:

rollLog c:\lots meu.log

e cria arquivos do tipo meu.log.0, meu.log.1, até meu.log.9


Arquivo roolLog.cmd

@ECHO off
REM Rolls a log file from FILENAME.0 to FILENAME.9 files, where FILENAME.0 is the newest and FILENAME.9 is the oldest. If there exists a FILENAME.9 file, it is deleted when run. The original log file is named FILENAME.0.
REM Copyright (C)  2008 Johan Känngård, [email protected]
REM
REM    This program is free software: you can redistribute it and/or modify
REM    it under the terms of the GNU General Public License as published by
REM    the Free Software Foundation, either version 3 of the License, or
REM    (at your option) any later version.
REM
REM    This program is distributed in the hope that it will be useful,
REM    but WITHOUT ANY WARRANTY; without even the implied warranty of
REM    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
REM    GNU General Public License for more details.
REM
REM    You should have received a copy of the GNU General Public License
REM    along with this program.  If not, see <http://www.gnu.org/licenses/>.
REM
REM Usage:
REM rollLog LOGDIRECTORY LOGFILENAME
REM - LOGDIRECTORY is the directory where the log file is located in
REM - LOGFILENAME is the filename of the log file located in LOGDIRECTORY.
REM
REM Example:
REM rollLog c:\logs at.log

IF EXIST %1% GOTO logfilelocationfound
ECHO %1% not found
GOTO end

:logfilelocationfound
	 cd %1%
	SET LOGFILE=%2%

	 IF NOT EXIST %LOGFILE% GOTO end

	 IF NOT EXIST %LOGFILE%.9 GOTO no9
	DEL %LOGFILE%.9

:no9
	 IF NOT EXIST %LOGFILE%.8 GOTO no8
	RENAME %LOGFILE%.8 %LOGFILE%.9

:no8
	 IF NOT EXIST %LOGFILE%.7 GOTO no7
	RENAME %LOGFILE%.7 %LOGFILE%.8

:no7
	 IF NOT EXIST %LOGFILE%.6 GOTO no6
	RENAME %LOGFILE%.6 %LOGFILE%.7

:no6
	 IF NOT EXIST %LOGFILE%.5 GOTO no5
	RENAME %LOGFILE%.5 %LOGFILE%.6

:no5
	 IF NOT EXIST %LOGFILE%.4 GOTO no4
	RENAME %LOGFILE%.4 %LOGFILE%.5

:no4
	 IF NOT EXIST %LOGFILE%.3 GOTO no3
	RENAME %LOGFILE%.3 %LOGFILE%.4

:no3
	 IF NOT EXIST %LOGFILE%.2 GOTO no2
	RENAME %LOGFILE%.2 %LOGFILE%.3

:no2
	 IF NOT EXIST %LOGFILE%.1 GOTO no1
	RENAME %LOGFILE%.1 %LOGFILE%.2

:no1
	 IF NOT EXIST %LOGFILE%.0 GOTO no0
	RENAME %LOGFILE%.0 %LOGFILE%.1

:no0
	 IF NOT EXIST %LOGFILE% GOTO end
	RENAME %LOGFILE% %LOGFILE%.0

:end


Ver também