Automatically delete old IIS log files

This is a really useful little VBS script that I’ve been meaning to post for a while now (along with a couple of other little applications I’ve written for log file analysis). I don’t think I wrote this script but at the same time can’t recall where it came from.

It basically traverses the FSO finding files with the designated extension and assuming the match the standard IIS date format, checks whether they’re older than x days, if they are deletes them. Running it is simple, place somewhere obvious on the server and just double click it. Alternatively if you want to read the output, run it from CMD. For safety’s sake, the first time you run it I would leave it just printing out the files that will be deleted.

Personally I don’t schedule this script as although automation is great, I’ll probably have it delete the logs before I’ve had a chance to download them so what I tend to do is download the logs and then after that (or the next time I’m on RDC) I run it, I find that way I ensure I get all the log files i.e. if I go on holiday.

I’ve got two other applications that I’ll post shortly, one outputs the location of the log files for each domain name within IIS and the other combines the log files into one for analysis –it also takes the exported file/folder locations and names the combined log files with the domain’s name –saves a ton of time!

Download the VBS script as a ZIP file

OptionExplicitDim intDaysOld, strObjTopFolderPath, strLogFIleSuffix, ObjFS, ObjTopFolder Dim ObjDomainFolder, ObjW3SvcFolder, ObjSubFolder, ObjLogFile, ObjFileintDaysOld        =5'Numberof days to retain on the serverstrObjTopFolderPath    =""'Thelocation of your log filesstrLogFIleSuffix    =".log"'The suffixof your log filesSet ObjFS =CreateObject("Scripting.FileSystemObject")Set ObjTopFolder = ObjFS.GetFolder(strObjTopFolderPath)ForEach ObjDomainFolder in ObjTopFolder.SubFoldersWScript.Echo("Folder:" & ObjDomainFolder.name)ForEach ObjW3SvcFolder in ObjDomainFolder.SubFolders        WScript.Echo("  Folder:" & ObjW3SvcFolder.name)Set ObjSubFolder = ObjFS.GetFolder(ObjW3SvcFolder)For each ObjLogFile in ObjSubFolder.filesSet ObjFile = ObjFS.GetFile(ObjLogFile)If datediff("d",ObjFile.DateLastModified,Date()) > intDaysOld and lcase(right(ObjLogFile,4))=strLogFIleSuffix then'*****************************************************'DON'T UNCOMMENT THIS UNTIL YOU KNOW IT WORKS PROPERLY!!!                    WScript.Echo("    Willdelete " & ObjSubFolder.name & "\" & ObjFile.name)'WScript.Echo("    Deleted " & ObjSubFolder.name & "\" & ObjFile.name)'ObjFile.Delete'*****************************************************EndIfSet ObjFile = nothingNextSet ObjSubFolder = nothingNextNextSet ObjTopFolder = nothingSet ObjFS = nothing

Subscribe to TSD

Don’t miss out on the latest posts. Sign up now to get access to the library of members-only posts.
jamie@example.com
Subscribe