Hi @Henry Hong ,
You can try the following script:
Tips:
- This script deletes IIS log files older than a specified number of days and runs it as a daily scheduled task on high-traffic web servers to avoid running out of disk space.
- Edit the value of intDelAge to set the desired retention time on the server.
- The location of the IIS log files is found automatically (for this to also work on IIS 7.x on Windows Vista, Windows Server 2008 or Windows 7, please enable "IIS 6 Metabase Compatibility" aka "IIS Metabase and IIS 6 configuration compatibility).
- Save the code as a .vbs file and run it daily on the server as a scheduled job.
Option Explicit
WScript.Timeout = 82800
Dim intDelAge
intDelAge = 30
Dim objIIS
Dim objWeb
Dim objIISOuter
Dim objWebOuter
Set objIISOuter = GetObject("IIS://LOCALHOST")
For Each objWebOuter in objIISOuter
If LCase(objWebOuter.Class) = "iiswebservice" Then
Set objIIS = GetObject("IIS://LOCALHOST/W3SVC")
For Each objWeb in objIIS
If LCase(objWeb.Class) = "iiswebserver" Then
Call DeleteLogFiles( _
objWeb.LogFileDirectory & "\W3SVC" & objWeb.Name, _
intDelAge)
End If
Next
ElseIf LCase(objWebOuter.Class) = "iissmtpservice" Then
Set objIIS = GetObject("IIS://LOCALHOST/SMTPSVC")
For Each objWeb in objIIS
If LCase(objWeb.Class) = "iissmtpserver" Then
Call DeleteLogFiles( _
objWeb.LogFileDirectory & "\SMTPSVC" & objWeb.Name, _
intDelAge)
End If
Next
ElseIf LCase(objWebOuter.Class) = "iisftpservice" Then
Set objIIS = GetObject("IIS://LOCALHOST/MSFTPSVC")
For Each objWeb in objIIS
If LCase(objWeb.Class) = "iisftpserver" Then
Call DeleteLogFiles( _
objWeb.LogFileDirectory & "\MSFTPSVC" & objWeb.Name, _
intDelAge)
End If
Next
End If
Next
Set objIIS = nothing
Set objIISOuter = nothing
Function DeleteLogFiles(strLogPath, intDelAge)
Dim objFs
Dim objFolder
Dim objSubFolder
Dim objFile
Dim objWShell
Set objWShell = CreateObject("WScript.Shell")
Set objFs = CreateObject("Scripting.FileSystemObject")
If Right(strLogPath, 1) <> "\" Then
strLogPath = strLogPath & "\"
End If
If objFs.FolderExists(strLogPath) Then
Set objFolder = objFs.GetFolder(strLogPath)
For Each objSubFolder in objFolder.subFolders
DeleteLogFiles strLogPath & objSubFolder.Name, intDelAge
Next
For Each objFile in objFolder.Files
If (InStr(objFile.Name, "ex") > 0) _
And (Right(objFile.Name, 4) = ".log") Then
If DateDiff("d",objFile.DateLastModified,Date) > intDelAge Then
objFs.DeleteFile(strLogPath & objFile.Name)
End If
End If
Next
Set objFs = Nothing
Set objFolder = Nothing
Set objWShell = nothing
End If
End Function
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the email notification for this thread.
Best regards,
Yurong Dai