Solved

vbscript to delete ex*.log files older than x days

Posted on 2008-11-01
4
2,979 Views
Last Modified: 2012-06-27
I need a script that recursively deletes IIS log files
The log files are in a log folder within each website
I found this script
http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_23173209.html
but that is only to delete files older than x numver of days

I am a bit in a hurry for this, so 500 points
0
Comment
Question by:Richard Frank
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
DrunkenELF earned 375 total points
ID: 22857691
Hi,
I use this script to compress and delete log files for all items in IIS - FTP, SMTP service and all web sites.
 
Cheers!
D-ELf

Const RemoveOlderThan = 30todayDate=cDate(day(now) &" "& MonthName(Month(Now),true) &" "&  Year(Now))Set fso = CreateObject("Scripting.FileSystemObject")set WshShell = WScript.CreateObject("WScript.Shell")Set objIIS = GetObject("IIS://LOCALHOST/W3SVC")For Each objWeb in objIIS   If LCase(objWeb.Class) = "iiswebserver" Then        if fso.FolderExists(objWeb.LogFileDirectory) Then	For Each fLogDir in fso.GetFolder(objWeb.LogFileDirectory).SubFolders            WScript.Echo objWeb.LogFileDirectory & "\" & fLogDir.Name            For Each fLog in fLogDir.Files	        If UCase(Left(fLog.Name,2))="EX" And UCase(Right(fLog.Name,4))=".CAB" Then		   fileDate=cDate(Mid(fLog.name,7,2) &" "& monthname(cint(Mid(fLog.name,5,2)),true) &" "& Mid(fLog.name,3,2))		   If fileDate< DateAdd("d", toDayDate, -RemoveOlderThan) Then		      'WScript.Echo("Deleting "& objWeb.LogFileDirectory & "\" & fLogDir.Name & "\" & flog.name)		      fLog.Delete		   End If		ElseIf UCase(Left(fLog.Name,2))="EX" And UCase(Right(fLog.Name,4))=".LOG" Then		   fileDate=cDate(Mid(fLog.name,7,2) &" "& monthname(cint(Mid(fLog.name,5,2)),true) &" "& Mid(fLog.name,3,2))		   If fileDate< DateAdd("d", toDayDate, -RemoveOlderThan) Then		      'WScript.Echo("Deleting "& objWeb.LogFileDirectory & "\" & fLogDir.Name & "\" &  flog.name)                      fLog.Delete		   Else		       if fileDate < todayDate Then       		          'WScript.Echo("Compressing "& objWeb.LogFileDirectory & "\" & fLogDir.Name & "\" &  flog.name)		          CABCommand = "MAKECAB /V1 "& fLog.Path &" "& Left(fLog.Path,Len(fLog.Path)-3) &"cab"			  CABReturnCode = WshShell.Run(CABCommand, 7, True)			  If (Err.Number <> 0) Then			     RaiseError Err.Description & " (" & Err.Number & ")"                          ElseIf (CABReturnCode <> 0) Then                             RaiseError "Return code from MAKECAB.EXE was " & CABReturnCode			  Else          		    'WScript.Echo("Deleting "& objWeb.LogFileDirectory & "\" & fLogDir.Name & "\" &  flog.name)			    fLog.Delete			  End If		    End If		End If           End if	Next    Next    End If   End IfNextSet objIIS = nothingSet wshShell = nothingSet fso = nothing

Open in new window

0
 
LVL 19

Assisted Solution

by:weellio
weellio earned 125 total points
ID: 22859153
this is merely D-elfs script in a usable format,.. give him the points if it works, if not, then i'll write something up for you.
Const RemoveOlderThan = 30
todayDate=cDate(day(now) &" "& MonthName(Month(Now),true) &" "&  Year(Now))
Set fso = CreateObject("Scripting.FileSystemObject")
set WshShell = WScript.CreateObject("WScript.Shell")
Set objIIS = GetObject("IIS://LOCALHOST/W3SVC")
For Each objWeb in objIIS
	If LCase(objWeb.Class) = "iiswebserver" Then
		if fso.FolderExists(objWeb.LogFileDirectory) Then
			For Each fLogDir in fso.GetFolder(objWeb.LogFileDirectory).SubFolders
				WScript.Echo objWeb.LogFileDirectory & "\" & fLogDir.Name
				For Each fLog in fLogDir.Files
					If UCase(Left(fLog.Name,2))="EX" And UCase(Right(fLog.Name,4))=".CAB" Then
						fileDate=cDate(Mid(fLog.name,7,2) &" "& monthname(cint(Mid(fLog.name,5,2)),true) &" "& Mid(fLog.name,3,2))
						If fileDate< DateAdd("d", toDayDate, -RemoveOlderThan) Then
							'WScript.Echo("Deleting "& objWeb.LogFileDirectory & "\" & fLogDir.Name & "\" & flog.name)
							fLog.Delete
						End If
					ElseIf UCase(Left(fLog.Name,2))="EX" And UCase(Right(fLog.Name,4))=".LOG" Then
						fileDate=cDate(Mid(fLog.name,7,2) &" "& monthname(cint(Mid(fLog.name,5,2)),true) &" "& Mid(fLog.name,3,2))
						If fileDate < DateAdd("d", toDayDate, -RemoveOlderThan) Then
							'WScript.Echo("Deleting "& objWeb.LogFileDirectory & "\" & fLogDir.Name & "\" &  flog.name)
							fLog.Delete
						Else
							if fileDate < todayDate Then
								'WScript.Echo("Compressing "& objWeb.LogFileDirectory & "\" & fLogDir.Name & "\" &  flog.name)
								CABCommand = "MAKECAB /V1 "& fLog.Path &" "& Left(fLog.Path,Len(fLog.Path)-3) &"cab"
								CABReturnCode = WshShell.Run(CABCommand, 7, True)
								If (Err.Number <> 0) Then
									RaiseError Err.Description & " (" & Err.Number & ")"
								ElseIf (CABReturnCode <> 0) Then
									RaiseError "Return code from MAKECAB.EXE was " & CABReturnCode
								Else
									'WScript.Echo("Deleting "& objWeb.LogFileDirectory & "\" & fLogDir.Name & "\" &  flog.name)
									fLog.Delete
								End If
							End If
						End If
					End if
				Next
			Next
		End If
	End If
Next
   Set objIIS = nothing
   Set wshShell = nothing
   Set fso = nothing

Open in new window

script.vbs.txt
0
 

Author Comment

by:Richard Frank
ID: 22861231
thnx you both.. that is wat I needed. top notch
0
 

Author Closing Comment

by:Richard Frank
ID: 31512334
simple and all what is necessary.
though d-elf.. you had to much beers ;) to omit the enters
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to debug this code 7 61
Excel copy picture into Outlook email 7 55
VB Script: restart service function 30 36
Creating An Intelligent Dropdown 8 25
Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question