Tim

Footprints in the snow of a warped mind

Tag Cloud

AJAX (4) ASP (6) ASP.Net (38) Error Reporting (2) Atlas (2) Business (61) Business Start-up Advice (24) Client (8) Expanding Your Business (15) C# (8) Canoeing (4) Canoe Racing (5) Cheshire Ring Race (5) Racing (2) Training (4) CIMA (1) Cisco (1) 7970G (1) CSS (3) dasBlog (2) Design (9) Icons (1) Development (7) General (37) Christmas (6) Fun and Games (10) Internet (18) Random (42) RX-8 (8) Home Cinema (2) Hosting (1) IIS (8) iPhone (1) JavaScript (2) Marketing (3) Multipack (1) Networking (2) Nintendo (1) OS Commerce (1) Photography (1) PHP (1) PowerShell (1) Press Release (1) Security (1) SEO (5) Server Maintenance (3) Server Management (8) Software (9) Office (4) Visual Studio (6) Windows (4) Vista (1) SQL Server (12) Testing (1) The Site Doctor (86) Turnover Challenge (1) Umbraco (10) Web Development (39) WebDD (32) Wii (1)

Atom 1.0 RSS 2.0 CDF 

Search

<September 2008>
SunMonTueWedThuFriSat
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

Recent Comments

Blog Archive

Various Links

Blogs I Read

 Google Blog
Official Google Webmaster Central Blog
 Matt Cutts
Gadgets, Google, and SEO
 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!
 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.0.7226.0

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

© 2008 Tim Gaunt.

Sign In

Get Windows Live Alerts

 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 [8]  Trackback Link
CategoriesTags: IIS | Windows
Wednesday, May 28, 2008 1:08:27 PM (GMT Standard Time, UTC+00: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 4:31:45 PM (GMT Standard Time, UTC+00: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 3:57:52 PM (GMT Standard Time, UTC+00: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 6:33:25 AM (GMT Standard Time, UTC+00: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 11:12:19 AM (GMT Standard Time, UTC+00: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 5:04:45 PM (GMT Standard Time, UTC+00: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 7:20:33 AM (GMT Standard Time, UTC+00: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 10:55:28 AM (GMT Standard Time, UTC+00:00)
Hey! Great stuff. +1 tnks :) Visit my blog too.
Name
E-mail
Home page

Comment (HTML not allowed)  

Live Comment Preview