Windows: IIS Log Housekeeping
I have a server that fills up with IIS logs. Over a week or two, the logs end up eating the entire c: drive and the box crashes like a drunk teenager. I spent a few lunch hours trawling Google to find some kind of script to clean out the oldest several days worth of logs automatically. I used just those words: "automatically delete IIS logs", in a few different combos. Also tried "archive old IIS logs", "dump flippin' web logs", and "Aaarrrrrrggggh!". Didn't have a lot of luck. Found a few commercial apps, but I'm religiously adverse to paying for simple code, so I kept looking, long past the point where I could have written something myself to deal with the issue.
Eventually, my laziness paid off.
I found a simple DOS batch script that would copy the current logs to an archive area, and delete the oldest archive. It was on “Experts Exchange” and I'd link to it, but I can't find it anymore. Pretty simple, once I saw it:
rename C:\WINNT\system32\LogFiles\W3SVC1\6 7
rename C:\WINNT\system32\LogFiles\W3SVC1\5 6
rename C:\WINNT\system32\LogFiles\W3SVC1\4 5
rename C:\WINNT\system32\LogFiles\W3SVC1\3 4
rename C:\WINNT\system32\LogFiles\W3SVC1\2 3
rename C:\WINNT\system32\LogFiles\W3SVC1\1 2
move C:\WINNT\system32\LogFiles\W3SVC1\*.log C:\WINNT\system32\LogFiles\W3SVC1\1
This one assumes you have only one web server, and you want 7 days worth of history. The pattern to add or remove a few days should be readily apparent (or you shouldn't be doing this). Since you want this to run unattended, the "/Q" suppresses the "are you sure" that you'd normally bull through, anyway.
Name it "something.bat". Call the batch file with the Windows Scheduler app, set to run right after midnight, before there's too much in the new log.
BTW, the successful search string was “IIS Logs”, followed by a lot of sifting. Hopefully, this post will save the next guy from the same fate.