Saturday, June 30, 2018

IIS Log cleaner old method to clean all the IIS logs

IIS Log cleaner old method to clean all the IIS logs


IIS Log cleaner






Determine the number of days for which you want to keep logs
logDelDays = 30

Main driver
purgeIISLogs("W3SVC")
purgeIISLogs("MSFTPSVC")

Purge IIS logs
Sub purgeIISlogs(strService)
    delCount   = 0
    Set fso    = CreateObject("Scripting.FileSystemObject")
    Set objIIS = GetObject("IIS://" & getComputer() & "/" & strService)
    For Each objweb in objIIS
        If lCase(objweb.Class) = "iiswebserver" _
        Or lCase(objweb.Class) = "iisftpserver" Then
            fLogDirPath = objweb.LogFileDirectory & "" & strService & objweb.name
            If fso.FolderExists(fLogDirPath) Then
                For Each fLog in fso.GetFolder(fLogDirPath).Files
                    If UCase(Left(fLog.Name,2)) = "EX" And UCase(Right(fLog.Name,4)) = ".LOG" Then
                        fileDate = cDate(Mid(fLog.name,7,2) & " " & monthname(cint(Mid(fLog.name,5,2)),true) & " " & Mid(fLog.name,3,2))
                        If fileDate < DateAdd("d", now(), -logDelDays) Then
                            delCount = delCount + 1
                            fLog.Delete
                        End If
                    End If
                Next
                Wscript.Echo "Purged " & delCount & " logs for " & objweb.ServerComment
            End If
        End If
    Next
    Set objIIS = nothing
    Set fso    = nothing
End Sub

Get computer name
Function getComputer()
    Set objNet = WScript.CreateObject("WScript.Network")
    getComputer= objNet.ComputerName
    Set objNet = Nothing
End Function


visit link download