Submitted By: Jake Baker

Deletes IIS log files that are more than 90 days old.

Visual Basic
Edit|Remove
MaxDays = 90
strComputer = InputBox("This script will delete IIS .log files over 90 days old "_
	& "from the machine you specify below.")
wmiQuery = "Select * from Win32_OperatingSystem"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery(wmiQuery)
	For Each objItem In colItems
		sWindowsPath = objItem.WindowsDirectory
	Next
Set objW3SVC = GetObject( "IIS://" & strComputer & "/W3SVC")
	For Each objSite In objW3SVC
	    If objSite.Class = "IIsWebServer" Then
	    	strLogDir = UCase(objSite.LogFileDirectory)
				strLogDir = Replace(strLogDir,"%WINDIR%",sWindowsPath,1,1,1)
				strLogDir = Replace(strLogDir,"%SYSTEMROOT%",sWindowsPath,1,1,1)
				strLogDir = Replace(strLogDir,":","$",1,1,1)
		    objLogFolder = "\\" & strComputer & "\" & strLogDir
    		Set oFSO = CreateObject("Scripting.FileSystemObject")
			Set oFolder = oFSO.GetFolder(objLogFolder)
			Set colSubFolders = oFolder.Subfolders
			For Each oSubFolder In colSubFolders
				If InStr(UCase(oSubFolder),"W3SVC") Then
					For Each oFile In oSubFolder.files
						If InStr(LCase(oFile.Name),".log") Then
 							If (Date - oFile.DateCreated > CInt(MaxDays)) Then
								oFSO.DeleteFile(oSubFolder & "\" & oFile.Name)
							End If
						End If
					Next
				End If
			Next
	    End If
	Next
MsgBox("All done.")