Tip

How to keep IIS logs from filling up your server hard drives

If you're running a version of Microsoft Internet Information Services (IIS) on any of your servers, then you've probably seen what happens when IIS logs are not cleaned up periodically. Old IIS logs can quickly eat up all of the free space on the hard drive, especially on servers with highly utilized websites, such as Microsoft Outlook Web Access or line-of-business applications.

Instead of having to check and clear IIS logs manually, wouldn't it be nice to have a scheduled task that deletes the old logs automatically? The solution is easily implemented with a simple Visual Basic script and a scheduled task on each of the servers you want to keep cleaned up.

    Requires Free Membership to View

To clear the old IIS logs, or any type of log file that accumulates over time, you'll need to first save the script presented below with the extension of .vbs, such as delete_old_logs.vbs. I like to put all of the maintenance scripts I place on a server in a specific directory that I create, which in my case is C:\Program Files\Scripts. But you can save the script file anywhere on the server.

After the script has been saved on the server, you'll need to edit the script to specify the exact path that is to be cleaned and how many days of log files should be left. You'll find this information in the line that starts with Call CleanDirectory. In the example below, the script is set to delete the default IIS logs that are older than seven days. Be sure your folder path is inside the quotation marks, and remember that you can insert multiple Call CleanDirectory lines if you'd like to delete logs from several sources on the same server simultaneously. After making your changes to the script, be sure to save the script file.

The last step is to schedule a task to run the script file. The account that you run the scheduled task under needs to have permissions on the folders where the log files are located, so it's usually best to create a special account (local or domain) and put that account in the local server administrations group. This will allow the script to perform its intended function in any location on the server.

When you create the scheduled task, you'll need to browse to the location where you saved the file. Make sure the path is enclosed in quotation marks if it contains any spaces. For best results, schedule the task to run daily.

With just a few simple steps, you can quickly implement a proactive solution on your servers to keep IIS logs from completely filling up your hard drives. Since Internet Information Services is not the only application that has a habit of creating lots of log files, I'm sure you'll find plenty of uses for this cleanup script.

'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
' delete_old_files.vbs
' Based on original script here:
'
http://www.microsoft.com/technet/scriptcenter/csc/scripts/files/files/
cscfi006.mspx
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

' Clean the listed directories of old backup files
Call CleanDirectory("C:\WINDOWS\system32\LogFiles\W3SVC1", 7)

' Delete old backup files
Function CleanDirectory(strPathtoFiles, intDaystoKeep)
   Dim fso, f, fc, f1, strFiles, intFiles
   strFiles = ""
   intFiles = 0

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

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

      Set f1 = Nothing
      Set fc = Nothing
      Set f = Nothing

   End if

   set fso = Nothing

End Function

' Stop the script in case it doesn't automatically WScript.Quit

ABOUT THE AUTHOR
Will Schmied
is a senior systems administrator for a world renowned children's research hospital. He holds numerous Microsoft MCITP, MCTS and older certifications and is experienced with Exchange and Blackberry. Will has been actively involved with the certification and training side of IT for many years, writing or contributing to several dozen books. He is a founder of the certification portal MCSE World. Having passed the reigns to a good friend from down under, Will maintains a smaller presence today with his blog, Tales of a Systems Administrator.

This was first published in March 2009

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.