I did some digging for a quick script to delete old IIS logs for a web host that uses IIS. Yes, there are some web hosts that use IIS… but not too many that STILL use a Windows 2000 server to do it. But, I never find anything I can run as is… so here’s a quick merge of a couple examples I found.
It queries the IIS metabase for the site name, number and most importantly, the log path variable. Expands the path variables, and deletes .log files older than intDaysOld.
The client I’m working for has hundreds of domains hosted on an old Win2k server. Its tiny hard drive is choking on almost 10 years worth of data. I’ve watched the output of this script run for an hour now… it’s finding logs from November of 2000. I estimate it should clear 10GB of IIS log files by morning.
Option Explicit
Dim FSO, WSH, strServerName, intDaysOld, strLogPrefix, strLogSuffix
Set WSH = CreateObject("Wscript.Shell")
Set FSO = CreateObject("Scripting.FileSystemObject")
intDaysOld = 60
strLogPrefix = "ex"
strLogSuffix = ".log"
strServerName = "LocalHost"
ProcServices "W3SVC", "IIsWebServer"
ProcServices "MSFTPSVC", "IIsFtpServer"
Set FSO = Nothing
Set WSH = Nothing
Sub ProcessServiceEntry(Service, Entry, parent)
Dim LogDir, Name
WScript.echo Service & " - " & Entry.Name & " - " & Entry.ServerComment
If (Parent = true) Then
Logdir = Entry.LogfileDirectory
Else
Logdir = Entry.LogfileDirectory & "\" & Service & Entry.Name
End If
LogDir = WSH.ExpandEnvironmentStrings(LogDir)
If (FSO.FolderExists(LogDir) = true) Then
DeleteLogFiles LogDir
Else
WScript.Echo("INVALID Path: " & Parent & Service & Entry & LogDir)
End If
End Sub
Sub DeleteLogFiles(FolderPath)
Dim objFolder, objFile, fsize, fcnt
Set objFolder = FSO.GetFolder(FolderPath)
fsize = 0
fcnt = 0
For each objFile in objFolder.files
If datediff("d", objFile.DateLastModified, Date()) > intDaysOld and lcase(right(objFile.name, 4)) = strLogSuffix and Left(objFile.name, Len(strLogPrefix)) = strLogPrefix Then
fsize = fsize + objFile.size
fcnt = fcnt + 1
WScript.Echo("Deleting " & objFile.name & " - " & objFile.size)
objFile.Delete(true)
Else
WScript.Echo("Will NOT delete " & objFile.name)
End If
Set objFile = nothing
Next
WScript.echo "Deleted " & fcnt & " files - " & fsize & " bytes"
End Sub
Sub ProcServices(Service, ServiceClass)
Dim Key, IISOBJ, Entry
WScript.echo Service & " - " & ServiceClass
Key = "IIS://" & strServerName & "/" & Service
On Error Resume Next
Set IISOBJ = GetObject(Key)
If (Err <> 0) Then
WScript.Echo "Error unable to open IIS Metabase key : " & Key & ", Err = " & Err & ", Desc = " & Err.Description
Exit Sub
End If
ProcessServiceEntry Service, IISOBJ, true
For each Entry in IISOBJ
If (Entry.Class = ServiceClass) Then
ProcessServiceEntry Service, Entry, false
End If
Next
End Sub
Based on examples here: * IIS FAQ * The Site Doctor: Automatically Delete Old IIS Log Files