Cutting IIS logs down to size


A client with SBS 2003 had some issues with space being taken up by IIS logfiles. They wanted to have visibility of the log files but needed them to be automatically cleared after 7 days to prevent a local partition filling over time. The solution to this requirement was relatively simple. A bat file using the forfiles executable from the Windows 2003 server resource kit and a scheduled task to run the job on a daily basis.  So you will either need the forfiles.exe or simply install the Resource kit from www.microsoft.com. If you have forfiles.exe place it in the same directory that you are creating the batch file in.

As always the author accepts no responsibility for any damage that this or any other script might cause !!

The actual bat file in the end contained

Forfiles -p C:WINDOWSsystem32LogFilesW3SVC1 -s -m *.log -d -7 -c “Cmd /C DEL @File”

Forfiles -p C:WINDOWSsystem32LogFilesSMTPSVC1 -s -m *.log -d -7 -c “Cmd /C DEL @File”

The parameters are

-p the path to the log files

-m the file extension of the log files

-d the age in number of days of the files to be selected

-c the command to issue

While testing you may wish to use Echo rather than DEL !!!

As you can see I decided to clear out the SMTP logfiles also. Scheduling is done using the windows scheduler – Control Panel / Scheduled tasks. Browse to the folder where you placed the batch file and forfiles.exe unless you installed the resource kit in which case forfiles is in the path already when add schedule tasks asks you what to run. Complete the wizard by setting the time and frequency you want the batch file to run.