Skip to content

Instantly share code, notes, and snippets.

@gardart
Last active August 29, 2015 14:06
Show Gist options
  • Save gardart/2964be65156d654ebaa9 to your computer and use it in GitHub Desktop.
Save gardart/2964be65156d654ebaa9 to your computer and use it in GitHub Desktop.
Windows - Stop IIS services, archive logs and restart IIS
@echo off
:: Stop services, archive logs and start them again
:: Create date variables
for /f "tokens=2-4 delims=. " %%a in ('date /T') do set year=%%c
for /f "tokens=2-4 delims=. " %%a in ('date /T') do set month=%%b
for /f "tokens=2-4 delims=. " %%a in ('date /T') do set day=%%a
:: Set Log File location
set LOGFILESPATH=C:\inetpub\logs\LogFiles
:: Set archive locations
set ARCHIVEPATHROOT=E:\Backup\Operation_Trace_Logs
set ARCHIVEPATHCURRENT= %ARCHIVEPATHROOT%\%YEAR%\%MONTH%\%DAY%
:: Set retention time
set DAYS=7
:: Make directory structure [YYYY\MM]
md %ARCHIVEPATHCURRENT%
:: Stop WAS Service
net stop WAS /YES
TIMEOUT /T 20 /NOBREAK
:: ZIP files with 7ZIP to archive location (Needs 7zip)
forfiles -p %LOGFILESPATH% -s -m *.log -d %DAYS% -c "Cmd /C "c:\scripts\7za\7za.exe" a %ARCHIVEPATHCURRENT%\@FILE.zip @FILE"
:: Delete files after compressing
forfiles -p %LOGFILESPATH% -s -m *.log -d %DAYS% -c "Cmd /C del @FILE"
:: Start Services
iisreset
TIMEOUT /T 20 /NOBREAK
net start %SomeOtherService% /YES
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment