VBScript to delete IIS Log files

VBScript to delete IIS Log files

Create a file using the text below. Save it somewhere with a .VBS extension. I used IISLogCleaner.vbs

sLogFolder = "c:\inetpub\logs\LogFiles"
iMaxAge = 14 'in days
Set objFSO = CreateObject("Scripting.FileSystemObject")
set colFolder = objFSO.GetFolder(sLogFolder)
For Each colSubfolder in colFolder.SubFolders
Set objFolder = objFSO.GetFolder(colSubfolder.Path)
Set colFiles = objFolder.Files
For Each objFile in colFiles
iFileAge = now-objFile.DateCreated
if iFileAge > (iMaxAge+1) then
objFSO.deletefile objFile, True
end if
Next
Next

The script above will scan all subfolders, so it will process logs for ALL sites in and under the folder specified. If you want to limit the process to just a single site, change the path appropriately.

To run the script manually, execute the following script in an administrator command line:

cscript.exe c:\scripts\retentionscript.vbs

Using a script to delete log files is a long-term, reliable solution to the problem of log files consuming disk space. If you automate the process, as shown below, it doesn’t require constant maintenance.

RUN THE SCRIPT AS A SCHEDULED TASK

You can automate the task of deleting log files by script by creating a Windows task schedule to run the script periodically. You can schedule the script to run at any time using the Windows Task Scheduler. How you configure the scheduled task should be coordinated with the configuration of the log file rollover options.

Open Server Manager, click the Tools menu, and then click Task Scheduler.
In the Actions pane of the Task Scheduler dialog box, click Create Task.

managing-log-file-storage-scheduling-step2

On the General tab of the Create Task dialog box, enter a name for the task, such as “Delete Log Files”. Set the security properties, selecting a user account with sufficient privileges to run the script.

managing-log-file-storage-scheduling-step3

Click the Triggers tab, and then click New. In the New Trigger dialog box, set Begin the task to On a schedule. Select the periodicity, for example, Daily. Enter the Start date, select more advanced settings, and ensure that Enabled is selected if you are ready to initiate the schedule. Click OK.

managing-log-file-storage-scheduling-step4

 

Click the Actions tab, and then click New. In the New Action dialog box, select a value for Action, in this case, Start a program. In Program/script, enter cscript, and in Add arguments (optional), enter the path and name of the script file, for example, “C:\iis\Log_File_Deletion.vbs”. Click OK.

managing-log-file-storage-scheduling-step5

Click OK.

Verify that the task has been added to the Active Tasks pane.

Right-click on the new task, and select Run.

managing-log-file-storage-scheduling-step8

 

Navigate to the folder that the script ran on, and verify that the appropriate log files were deleted.

Navigate back to the Task Scheduler, right-click on the task, and click End so the status returns to Ready and the task is ready for scheduled runs.

Leave a Reply