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:

del C:\WINNT\system32\LogFiles\W3SVC1\7 /Q
rmdir C:\WINNT\system32\LogFiles\W3SVC1\7
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
mkdir C:\WINNT\system32\LogFiles\W3SVC1\1
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.

Comments

This is that perl junk I wrote back at eTour. Remember we had this same problem in 2000? So I wrote some perl stuff that not only took the files off multiple servers, but also zipped them? You need those scripts?

This simple batch file meets my current needs, but I do recall you putting something nice together on that back in the day. Post it up on Idmwiziard and I'll link to it as an alternate.

BTW, thanks for helping me test out the Subscriptions and Comment modules!

I still owe you this. I'll have the scripts with me at the GBRC. Bring a thumbdrive.

I know this was light-years ago but here is a modern version

http://www.808.dk/?code-iis-log-housekeeping#ManualPlusArchive

What - mine weren't good enough for you? ;-) No, those look pretty slick. Always good to have options.