:: ArchiveWebLogs.cmd :: :: - by Nick @ FoolishIT.com [Foolish IT LLC] :: - Official Download: https://download.foolishit.com/BatchFiles/ArchiveWebLogs.cmd.txt :: :: Created: 10-30-07 :: Last Modified: 10-30-07 :: :: Archives log files older than xx days across multiple folders by using :: rar.exe (this can of course be changed to whatever program you use, just :: remember to change the parameters!) :: :: Expected format: :: Drive:\domainpath\???????\logspath\????????\Ex??????.log :: Where Ex??????.log represents year/month/day in YYMMDD format. :: :: Years are counted as 365 days, Months as 30 days, so results are approximate. :: Script will produce odd results by the year 2100. All because I am lazy. :: :: Be sure to set static varaibles below according to your needs. :: :: This batch accepts no parameters but could easily be modified to do so. :: @echo off&title %~nx0&setlocal enableextensions enabledelayedexpansion :: SET STATIC VARIABLES HERE :: Drive Letter script will run on set drive=C :: Sub Path domains are found in set dpath=domains :: Name of log file path in domain set logsubdir=logs :: Archive any log older than these days set daystokeep=90 :: WinRAR temp and batch temp working directory set arctemp=D:\temp :: WinRAR temp archive file list set wrarlist=archive_these.txt if not exist "%drive%:\%dpath%" ( echo Domain path not found!&pause&goto :eof ) rar.exe if "%errorlevel%"=="9009" ( echo.&echo RAR not found!&pause&goto :eof ) call :cleanup :: CURRENT DATE CONVERTED TO YYMMDD for /f "tokens=1,2,3,4 delims=/ " %%a in ('date /t') do ( set cyear=%%d set cmonth=%%b set cday=%%c set today=!cyear:~-2!%%b%%c ) :: SUBTRACT %daystokeep% FROM CURRENT DATE :: Stripping leading zeros next two loops... :mzero if not "%cmonth:~0,1%"=="0" goto :mzdone if "%cmonth%"=="0" goto :mzdone set cmonth=%cmonth:~1% goto :mzero :mzdone :dzero if not "%cday:~0,1%"=="0" goto :dzdone if "%cday%"=="0" goto :dzdone set cday=%cday:~1% goto :dzero :dzdone set /a yeardays=%cyear% * 365 set /a monthdays=%cmonth% * 30 set /a totaldays=%yeardays% + %monthdays% + %cday% set /a olderthan=%totaldays% - %daystokeep% :: MEAT %drive%: cd\"%dpath%" for /f "tokens=*" %%_ in ('dir /a:d /b') do ( cd "%%_\%logsubdir%" if not errorlevel 1 ( for /f "tokens=*" %%a in ('dir /a:d /b') do ( set lastdir=%%a cd "%%a" call :parfname if exist "%arctemp%\%wrarlist%" call :archive cd .. ) cd "%drive%:\%dpath%" ) ) goto :cleanup :: PARSE FILENAME FOR DATE SUBROUTINE :parfname for /f "tokens=*" %%_ in ('dir /b *.log') do ( set tfile=%%_ set tyear=20!tfile:~2,-8! set tmonth=!tfile:~4,-6! set tday=!tfile:~6,-4! call :math ) goto :eof :: COMPARE AGE OF FILE SUBROUTINE :math :: Stripping leading zeros again, with year this time. :tyzero if not "%tyear:~0,1%"=="0" goto :tyzdone if "%tyear%"=="0" goto :tyzdone set tyear=%tyear:~1% goto :tyzero :tyzdone :tmzero if not "%tmonth:~0,1%"=="0" goto :tmzdone if "%tmonth%"=="0" goto :tmzdone set tmonth=%tmonth:~1% goto :tmzero :tmzdone :tdzero if not "%tday:~0,1%"=="0" goto :tdzdone if "%tday%"=="0" goto :tdzdone set tday=%tday:~1% goto :tdzero :tdzdone set /a tyeardays=%tyear% * 365 set /a tmonthdays=%tmonth% * 30 set /a targetdays=%tyeardays% + %tmonthdays% + %tday% set targetpath=%cd% if %targetdays% LEQ %olderthan% ( echo %targetpath%\%tfile%>> "%arctemp%\%wrarlist%" ) goto :eof :prearc goto :eof :: ARCHIVE ROUTINE FOR EACH LOGDIR GOES HERE AND CLEANS UP WHEN DONE :archive if exist "%arctemp%\%wrarlist%" ( rar a -m5 -n@"%arctemp%\%wrarlist%" "%targetpath%\%lastdir%.rar" for /f "tokens=*" %%_ in ('type "%arctemp%\%wrarlist%"') do ( if exist "%%_" del /q "%%_" ) ) call :cleanup goto :eof :: CLEANUP which runs before and after all functions of script :cleanup if defined arctemp ( if exist "%arctemp%\%wrarlist%" del /q "%arctemp%\%wrarlist%" ) goto :eof