I decided to check the size of some of my logs on specific servers. Well, as it turns out there were 4GB of logs! I needed a script that would delete the files after so many days. I was really lazy and looked up the recursion portion (MS Scripting Guys)! You can point this script anywhere and it will delete any files older then the days you specify.
————Copy Everything Below This Line————
'Delete Old Files (IIS)
'Created by Chey "The Dean of Mean" Harden =P
On Error Resume Next
Dim arrFolders()
intSize = 0
Const MaxDays = 45 'This is the created Date
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
strFolderName = "C:\WINNT\system32\LogFiles"
Set colSubfolders = objWMIService.ExecQuery _
("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _
& "Where AssocClass = Win32_Subdirectory " _
& "ResultRole = PartComponent")
ReDim Preserve arrFolders(intSize)
arrFolders(intSize) = strFolderName
intSize = intSize + 1
For Each objFolder in colSubfolders
GetSubFolders strFolderName
Next
Sub GetSubFolders(strFolderName)
Set colSubfolders2 = objWMIService.ExecQuery _
("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _
& "Where AssocClass = Win32_Subdirectory " _
& "ResultRole = PartComponent")
For Each objFolder2 in colSubfolders2
strFolderName = objFolder2.Name
ReDim Preserve arrFolders(intSize)
arrFolders(intSize) = strFolderName
intSize = intSize + 1
GetSubFolders strFolderName
Next
End Sub
For Each strFolder in arrFolders
'WScript.Echo strFolder
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder1 = objFSO.GetFolder(strFolder)
Set colFiles = objFolder1.Files
For Each objFile in colFiles
'WScript.Echo objFile
If DateDiff("d",objFile.DateCreated,now) >= MaxDays Then
objFSO.DeleteFile(objFile.Path)
End If
Next
Next
————Copy Everything Above This Line————
1. Const MaxDays = 45 Edit this line to remove the files older then this. BTW this is the created date
2. Edit this line strFolderName = "C:\Windows\system32\LogFiles" to represent where you want the recursion to start.
3. Schedule the Task to run. DONE!
***Make sure NO word wrap is happening in your script
This information is provided "AS IS" with no warranties expressed or implied.