Tim

Footprints in the snow of a warped mind

Automatically delete old IIS log files

Where to find me

Flickr Icon  Twitter Icon  Linked In Icon  FaceBook Icon  Windows Live Alerts Butterfly  RSS 2.0 

Tag Cloud

AJAX (4) ASP (6) ASP.Net (50) Error Reporting (4) Web Service (1) WSDL (1) Atlas (2) Business (76) Business Start-up Advice (25) Client (14) Expanding Your Business (17) C# (16) Canoeing (4) Canoe Racing (5) Cheshire Ring Race (5) Racing (2) Training (4) CIMA (1) Cisco (1) 7970G (1) CSS (3) dasBlog (4) DDD (1) Design (9) Icons (1) Development (12) General (39) Christmas (6) Fun and Games (11) Internet (22) Random (46) RX-8 (8) Home Cinema (2) Hosting (2) IIS (10) iPhone (1) JavaScript (4) jQuery (1) Marketing (5) Email (1) Multipack (1) Networking (2) Nintendo (1) OS Commerce (1) Photography (1) PHP (1) PowerShell (2) Press Release (1) Productivity (2) Security (2) SEO (5) Server Maintenance (4) Server Management (9) Social Media (1) Social Networking (2) Experiment (1) Software (9) Office (5) Visual Studio (12) Windows (4) Vista (1) SQL (1) SQL Server (13) Stored Procedure (1) Testing (1) The Site Doctor (104) Turnover Challenge (1) Twitter (2) Umbraco (17) 2009 (1) Web Development (54) WebDD (33) Wii (1)

Blog Archive

Search

<July 2010>
SunMonTueWedThuFriSat
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

Recent Comments

Blog Archive

Various Links

Blogs I Read

[Feed] Google Blog
Official Google Webmaster Central Blog
[Feed] Matt Cutts
Gadgets, Google, and SEO
[Feed] Ol' Deano's Blog
My mate Dean's blog on my space, equally as random as mine but not off on as much of a tangent!
[Feed] Sam's Blog
Sam is one of my younger brothers studying Product Design and Manufacture at Loughborough, this is his blog :) Enjoy!

Recent Tracks

last.fm - The Social Music Revolution

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

newtelligence dasBlog 2.2.8279.16125

Send mail to the author(s) Email Me (Tim Gaunt)

© 2010 Tim Gaunt.

Sign In

    # Saturday, February 10, 2007

    Automatically delete old IIS log files

    Saturday, February 10, 2007 4:23:10 PM (GMT Standard Time, UTC+00:00)

    This is a really useful little VBS script that I’ve been meaning to post for a while now (along with a couple of other little applications I’ve written for log file analysis). I don’t think I wrote this script but at the same time can’t recall where it came from.

    It basically traverses the FSO finding files with the designated extension and assuming the match the standard IIS date format, checks whether they’re older than x days, if they are deletes them. Running it is simple, place somewhere obvious on the server and just double click it. Alternatively if you want to read the output, run it from CMD. For safety’s sake, the first time you run it I would leave it just printing out the files that will be deleted.

    Personally I don’t schedule this script as although automation is great, I’ll probably have it delete the logs before I’ve had a chance to download them so what I tend to do is download the logs and then after that (or the next time I’m on RDC) I run it, I find that way I ensure I get all the log files i.e. if I go on holiday.

    I’ve got two other applications that I’ll post shortly, one outputs the location of the log files for each domain name within IIS and the other combines the log files into one for analysis –it also takes the exported file/folder locations and names the combined log files with the domain’s name –saves a ton of time!

    Download the VBS script as a ZIP file

    Option Explicit

    Dim intDaysOld, strObjTopFolderPath, strLogFIleSuffix, ObjFS, ObjTopFolder 
    Dim ObjDomainFolder, ObjW3SvcFolder, ObjSubFolder, ObjLogFile, ObjFile

    intDaysOld        = 5        'Number of days to retain on the server
    strObjTopFolderPath    = ""        'The location of your log files
    strLogFIleSuffix    = ".log"    'The suffix of your log files

    Set ObjFS = CreateObject("Scripting.FileSystemObject")
    Set ObjTopFolder = ObjFS.GetFolder(strObjTopFolderPath)

    For Each ObjDomainFolder in ObjTopFolder.SubFolders
    WScript.Echo("Folder: " & ObjDomainFolder.name)
        For Each ObjW3SvcFolder in ObjDomainFolder.SubFolders
            WScript.Echo("  Folder: " & ObjW3SvcFolder.name)
            Set ObjSubFolder = ObjFS.GetFolder(ObjW3SvcFolder)
                For each ObjLogFile in ObjSubFolder.files
                    Set ObjFile = ObjFS.GetFile(ObjLogFile)
                    If datediff("d",ObjFile.DateLastModified,Date()) > intDaysOld and lcase(right(ObjLogFile,4))=strLogFIleSuffix then
                        '*****************************************************
                        'DON'T UNCOMMENT THIS UNTIL YOU KNOW IT WORKS PROPERLY!!!
                        WScript.Echo("    Will delete " & ObjSubFolder.name & "\" & ObjFile.name)
                        'WScript.Echo("    Deleted " & ObjSubFolder.name & "\" & ObjFile.name)
                        'ObjFile.Delete
                        '*****************************************************
                    End If
                    Set ObjFile = nothing
                Next
            Set ObjSubFolder = nothing
        Next
    Next

    Set ObjTopFolder = nothing
    Set ObjFS = nothing
    Automatically delete old IIS log files
    Useful Links:  #  digg it!  del.icio.us  Technorati  email it!  Post CommentsComments [10]  Trackback LinkTrackback
    CategoriesTags: IIS | Windows
    Wednesday, May 28, 2008 2:08:27 PM (GMT Daylight Time, UTC+01:00)
    Great Script but 3 things:

    1. Please be careful people the downloaded script in the zip file is not commented properly. it's commented to delete the files please alter the script to do the test run as it shows in the above text field. Hopefully the author will correct this so the script out of the box will be in "chicken mode"

    2. This will delete any file with the extension .log. Please exercise caution if you have a shared environment as some customers might have their own log files for their applications. maybe the author will do some altering of the script to match "ex{[0-9]+}.log"

    3. This script will only go 2 folders deep. Like in the comments it states it assumes a folder structure that the log files folder is one deeper then the starting point that you type in. It would be nice to have it dive as deep as possible. with the reg ex from comment 2 in place this would be a safe operation to preform as long as you have containerized all web sites to folder and not just put them anywhere.
    Steve Main
    Wednesday, May 28, 2008 5:31:45 PM (GMT Daylight Time, UTC+01:00)
    Thanks Steve, I've updated the zip and have also made a few changes, well pretty much rewritten the script for you, the only thing I've not done is change to a regex as I don't need that but I've added a prefix check to it.

    Next version I'll add a way of limiting the depth it recurses.

    HTH

    Tim
    Wednesday, July 02, 2008 4:57:52 PM (GMT Daylight Time, UTC+01:00)
    We added a little bit more code to this you might like to include in your code :)

    We wanted to restrict the deletion to a specific folder name when doing the recursion.
    Although it still recursivly goes through all the folders it will only delete logs found in the /logs/ folders.

    'Restrict to /logs/ folder
    If objsubFolder.name = "logs" then
    For each objLogFile in objSubFolder.files
    Set objFile = objFS.GetFile(objLogFile)

    If datediff("d",objFile.DateLastModified,Date()) > intDaysOld and lcase(right(objLogFile,4))=strLogFIleSuffix and Left(objFile.name, Len(strPrefix)) = strPrefix then
    '*****************************************************
    'DON'T UNCOMMENT THIS UNTIL YOU KNOW IT WORKS PROPERLY!!!
    WScript.Echo(padDigits + " Will delete " & objFile.name)
    'WScript.Echo(padDigits + " Deleted " & objFile.name)
    'objFile.Delete
    '*****************************************************
    End If
    Set objFile = nothing
    Next
    End If
    Thursday, July 03, 2008 7:33:25 AM (GMT Daylight Time, UTC+01:00)
    Thanks Stuart, I guess in that case you store your log files under your website directory structure? We tend to keep the two seperate but a useful addition for those who don't :)

    Tim
    Thursday, July 03, 2008 12:12:19 PM (GMT Daylight Time, UTC+01:00)
    Our structure is:

    d:\websites\logs
    d:\websites\www
    d:\websites\database

    We didn't want the script to remove any ex******.log's that it found in the users \www folder :)
    Wednesday, September 03, 2008 6:04:45 PM (GMT Daylight Time, UTC+01:00)
    I know nothing about scripting, and have a question. The only files I want purged reside in C:\windows\system32\logfiles\w3svc1 folder, and I want to retain 30 days worth. So do I just change the following to this

    intDaysOld = 30
    strobjTopFolderPath = "c:\Windows\system32\LogFiles\W3SVC1\"

    Thanks
    Sean
    Thursday, September 04, 2008 8:20:33 AM (GMT Daylight Time, UTC+01:00)
    Hi Sean,

    In that instance no as this is designed to recurse the sub folders, you'll need to use this script:

    Option Explicit

    Dim intDaysOld, strObjTopFolderPath, strLogFIleSuffix, ObjFS, ObjTopFolder
    Dim ObjW3SvcFolder, ObjSubFolder, ObjLogFile, ObjFile

    intDaysOld = 30
    strobjTopFolderPath = "c:\Windows\system32\LogFiles\W3SVC1\"
    strLogFIleSuffix = ".log" 'The suffix of your log files

    Set ObjFS = CreateObject("Scripting.FileSystemObject")
    Set ObjTopFolder = ObjFS.GetFolder(strObjTopFolderPath)

    For Each ObjW3SvcFolder in ObjTopFolder.SubFolders
    WScript.Echo(" Folder: " & ObjW3SvcFolder.name)
    Set ObjSubFolder = ObjFS.GetFolder(ObjW3SvcFolder)
    For each ObjLogFile in ObjSubFolder.files
    Set ObjFile = ObjFS.GetFile(ObjLogFile)
    If datediff("d",ObjFile.DateLastModified,Date()) > intDaysOld and lcase(right(ObjLogFile,4))=strLogFIleSuffix then
    '*****************************************************
    'DON'T UNCOMMENT THIS UNTIL YOU KNOW IT WORKS PROPERLY!!!
    WScript.Echo(" Will delete " & ObjSubFolder.name & "\" & ObjFile.name)
    'WScript.Echo(" Deleted " & ObjSubFolder.name & "\" & ObjFile.name)
    'ObjFile.Delete
    '*****************************************************
    End If
    Set ObjFile = nothing
    Next
    Set ObjSubFolder = nothing
    Next

    Set ObjTopFolder = nothing
    Set ObjFS = nothing
    Friday, September 05, 2008 11:55:28 AM (GMT Daylight Time, UTC+01:00)
    Hey! Great stuff. +1 tnks :) Visit my blog too.
    Thursday, September 18, 2008 4:20:18 PM (GMT Daylight Time, UTC+01:00)
    Thanks. Works perfectly.
    Sean
    Saturday, August 29, 2009 3:23:54 PM (GMT Daylight Time, UTC+01:00)
    Dear All,

    Here is an adapted version to clean all Logs found in 1-Level subfolders below the Root Folder.

    It'll also log in the EventLog instead of msgbox (or console) for better scheduled task compliance.


    Option Explicit
    On Error Resume Next
    Dim intDaysOld, strObjTopFolderPath, strLogFIleSuffix, ObjFS, ObjTopFolder, objShell, tmpObjFileName
    Dim ObjLogFolder, ObjW3SvcFolder, ObjSubFolder, ObjLogFile, ObjFile
    intDaysOld = 1 'Number of days to retain on the server
    strObjTopFolderPath = "C:\WINDOWS\system32\LogFiles" 'The location of your log files
    strLogFileSuffix = ".log" 'The suffix of your log files

    Set ObjFS = CreateObject("Scripting.FileSystemObject")
    Set ObjTopFolder = ObjFS.GetFolder(strObjTopFolderPath)
    Set objShell = Wscript.CreateObject("Wscript.Shell")


    PurgeLogs()

    Sub PurgeLogs()
    WriteLog 0,"Purge Logs Script Starting with Root Folder: " & ObjTopFolder.Path
    For Each ObjLogFolder in ObjTopFolder.SubFolders
    WriteLog 0,"Purge Logs Script Parsing SubFolder: " & ObjLogFolder.name
    Set ObjSubFolder = ObjFS.GetFolder(ObjLogFolder)
    For each ObjLogFile in ObjSubFolder.files
    On Error Resume Next
    Err.Clear
    Set ObjFile = ObjFS.GetFile(ObjLogFile)
    If datediff("d",ObjFile.DateLastModified,Date()) > intDaysOld and lcase(right(ObjLogFile,4))=strLogFileSuffix then
    tmpObjFileName = ObjFile.Name
    '*****************************************************
    'DON'T UNCOMMENT THIS UNTIL YOU KNOW IT WORKS PROPERLY!!!
    'ObjFile.Delete
    '*****************************************************
    If Not Err then
    WriteLog 8," Purge Logs Script has Deleted SucessFully " & ObjSubFolder.name & "\" & tmpObjFileName
    Else
    WriteLog 16,"Purge Logs Script Error While Deleting " & ObjSubFolder.name & "\" & tmpObjFileName
    End If
    End If
    Set ObjFile = nothing
    Next
    Set ObjSubFolder = nothing
    Next
    Set ObjTopFolder = nothing
    Set ObjFS = nothing
    End Sub

    Sub WriteLog(evType,msg)
    on error resume next
    Err.Clear
    objShell.LogEvent evType, msg
    End Sub
    Name
    E-mail
    (will show your gravatar icon)
    Home page

    Comment (HTML not allowed)  

    Live Comment Preview