Views:

Create a Windows scheduled task that will delete the log files regularly:

  1. Save the script below as a vbs file on a local directory (i.e., delete_old_IISLogs_30days.vbs):
     
    On Windows Server 2008 and 2012, change the locations indicated on Call DeleteOldLogFiles to C:\inetpub\logs\LogFiles\ (default location).

    Option Explicit

    ' Delete all IIS Log Files older than 30 days
    Call DeleteOldLogFiles("C:\WINDOWS\system32\LogFiles\W3SVC3", 30)
    Call DeleteOldLogFiles("C:\WINDOWS\system32\LogFiles\W3SVC4", 30)
    Call DeleteOldLogFiles("C:\WINDOWS\system32\LogFiles\W3SVC5", 30)

    Function DeleteOldLogFiles(strPath2Logs, intDays2Keep)
    Dim fso, f, fc, f1, strFiles

    strFiles = ""

    Set fso = CreateObject("Scripting.FileSystemObject")
    If (fso.FolderExists(strPath2Logs)) Then
    Set f = fso.GetFolder(strPath2Logs)
    Set fc = f.Files

    '-- Determine if file is older than defined days
    For Each f1 in fc
    If DateDiff("d", f1.DateLastModified, Now) > intDays2Keep Then
    strFiles = strFiles & f1.Name & vbCrLf
    f1.Delete(True)
    End If
    Next

    Set f1 = Nothing
    Set fc = Nothing
    Set f = Nothing
    End If
    Set fso = Nothing

    End Function

     
    A couple of changes was made especially in the "Delete all IIS Log Files older than 30 days" section. You can also change the logfile path and number of days (the current value is 30) based on your preferences.
  2. On the OSCE server, go to Control Panel > Scheduled Task.
  3. Double-click Add Scheduled Task, follow the wizard and provide the following information:
    1. Browse the script that Windows will execute: delete_old_IISLogs_30days.vbs
    2. Task Name: Delete IIS Logs older than 30 days
    3. Set Schedule: Daily
    4. Set Start Time: 00:00
    5. Set Start Date: <Same Day>
    6. Enter Domain Admin Credentials
  4. Click Finish.

The scheduled task will run daily starting at 00:00 on the day of creation.