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

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
LVL 1
Richard FrankSystems AdministratorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DrunkenELFCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
William ElliottSr Tech GuruCommented:
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
Richard FrankSystems AdministratorAuthor Commented:
thnx you both.. that is wat I needed. top notch
0
Richard FrankSystems AdministratorAuthor Commented:
simple and all what is necessary.
though d-elf.. you had to much beers ;) to omit the enters
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.