Hello rejoinder,
Thanks for your help thus far, I made a a few revisions and so far the script is working well. The only issue I am having is when a file is found in the directory that does not meet the criteria of being deleted then it is listed multiple times in the email like this:
"Sorry, there are no files to delete that are older than 10 days in the AsyncQueue directory
Sorry, there are no files to delete that are older than 10 days in the AsyncQueue directory"
1. I would like to list this message just once instead of multiple times.
2. If possible I also would like to get a total of all files that were deleted.
Thanks in advance for your assistance.
Regards
'<<<<<<<<<<<<DEFINE VARIABLES HERE-BEGIN>>>>>>>>>>>>>>>>>>
DIM cutoff, curyear, scriptStatus, mailto, mailfrom, maillog
'DEFINE CUTOFF DATE (LOG FILES OF DATES OLDER THAN THIS DATE SHOULD BE DELETED)
'SPECIFY CUTOFF DATE IN CENTER PARAMETER
cutoff = dateadd("d", -10, date)'wscript.echo "Cut off=" & cutoff
'SPECIFY ERROR MESSAGE IF THERE ARE NO FILES TO PROCESS
sorry = "Sorry, there are no files to delete that are older than 10 days in the "
'GET CURRENT YEAR FOR REGULAR EXPRESSION TO PERFORM LOG DELETION
curyear = year(now) 'wscript.echo "Current Year=" & curyear
'PARAMETER TO ENABLE LOGS TO BE DELETED
'SET TO 'ON' TO ENABLE 'OFF' TO DISABLE
scriptStatus = "ON"
'EMAIL TO ADDRESS (Add multiple by seperating with commas)
mailto = "me@me.com"
'EMAIL FROM ADDRESS (Add multiple by seperating with commas)
mailfrom = "me@me.com"
'<<<<<<<<<<<<DEFINE VARIABLES HERE-END>>>>>>>>>>>>>>>>>>
'++++++++++++++++++++++++EMAIL CONFIGURATION BEGIN++++++++++++++++++++++++++++++++++
set maillog = createobject("CDO.Message")
'This section provides the configuration information for the remote SMTP server.
maillog.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'Send the message using the network (SMTP over the network).
maillog.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") ="mx01exchange.me.com"
maillog.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
maillog.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False 'Use SSL for the connection (True or False)
maillog.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 30
maillog.Configuration.Fields.Update
'End remote SMTP server configuration section==
maillog.from = mailfrom
maillog.to = mailto
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
if scriptStatus = "ON" then
'DELETE ALL LOG AND TEMP FILES CONFIGURATION
'ADD TEMP FILE DIRECTORIES TO THE ARRAY TO DISPLAY THEM IN THE EMAIL
arrayHeaders = array("AccountSystem", "AsyncQueue", "Common", "Login", "Publishing", "Shared")
'ADD A CASE STATEMENT FOR EACH DIRECTORY OF FILES THAT SHOULD BE DELETED
for each strHeader in arrayHeaders
strbody = strbody & vbCRLF & strHeader & vbCRLF
select case strHeader
Case "AccountSystem" 'DELETE ALL ACCOUNTSYSTEM LOG FILES
cleanup "D:\MyApp\LOG\Accountsystem", "^"&curyear& ".*?\.log$", cutoff
Case "AsyncQueue" 'DELETE ALL ASYNCQUEUE LOG FILES
cleanup "D:\MyApp\LOG\AsyncQueue", "^"&curyear& ".*?\.xml$", cutoff
Case "Common" 'DELETE ALL COMMON LOG FILES
cleanup "D:\MyApp\LOG\Common", "^"&curyear& ".*?\.log$", cutoff
Case "Login" 'DELETE ALL LOGIN LOG FILES
cleanup "D:\MyApp\LOG\Login", "^"&curyear& ".*?\.log$", cutoff
Case "Publishing" 'DELETE ALL PUBLISHING LOG FILES
cleanup "D:\MyApp\LOG\Publishing", "^"&curyear& ".*?\.xml$", cutoff
cleanup "D:\MyApp\LOG\Publishing", "^"&curyear& ".*?\.conf$", cutoff
Case "Shared" 'DELETE ALL SHARED LOG FILES
cleanup "D:\MyApp\LOG\Shared", "^"&curyear& ".*?\.log$", cutoff
End Select
Next
'--------------------------------------------------------------------------------------------------------------
'******************FUNCTIONS USED TO DELETE FILES************************************
Sub cleanup(folderName, pattern, cutoff)
Set fso = createobject("scripting.filesystemobject")
Set folder = fso.getfolder(folderName)
Set files = folder.files
Set re = new regexp
re.Pattern = pattern
countFiles = 0
countSize = 0
For each file in files
'CHECK THE REGULAR EXPRESSION
If re.Test(file.name) Then
'IF THE DATELASTMODIFIED IS LESS THAN THE CUTOFF DATE THEN DELETE THE FILE
If file.datelastmodified < cutoff Then
'wscript.echo "Deleting " & file.name &" last modified: " & file.datelastmodified
countFiles = countFiles + 1
countSize = countSize + file.Size
strbody = strbody &vbCrLf & countFiles & ":" & "Deleted-> " & file.name &" last modified: " & file.datelastmodified & vbCrLf
maillog.textbody = strbody
On Error Resume Next
file.delete
'IF THERE ARE NO MORE FILES TO PROCESS THEN SEND AN EMAIL MESSAGE
else
if countFiles = 0 then
'wscript.echo "Nothing to delete"
strbody = strbody & vbCRLF & sorry & strHeader & " directory" & vbCRLF
maillog.textbody = strbody
end if
End If
End If
Next
'wscript.echo "Deleted " & countFiles & " files from " & folderName & " [" & niceSize(countSize) & "]"
maillog.subject = "ATTENTION: Logs and files older than " & cutoff & " have been deleted!"
Set re = Nothing
Set files = Nothing
Set folder = Nothing
Set fso = Nothing
End Sub
Function niceSize(size)
units = "B"
If size > 1024 Then
size = Round(size / 1024, 1)
units = "kB"
End If
If size > 1024 Then
size = Round(size / 1024, 2)
units = "MB"
End If
If size > 1024 Then
size = Round(size / 1024, 3)
units = "GB"
End If
niceSize = "" & size & units
End Function
'*************************************************************************************
elseif scriptStatus = "OFF" then
maillog.subject = "ATTENTION: RE: The yo yo logs and files deletion script did not run properly"
cleanupOFF = "The script is turned off"
'wscript.echo cleanupOFF
strbody = cleanupOFF
maillog.textbody = strbody
end if
maillog.send
Set maillog = nothing
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136:





by: rejoinderPosted on 2008-10-03 at 00:31:51ID: 22631776
Try this. The output should look like what you requested plus if a file cannot be deleted you will see this in the email.
m", "^"&curyear& ".*?\.log$", cutoff "^"&curyear& ".*?\.xml$", cutoff
m", "^"&curyear& ".*?\.log$", cutoff "^"&curyear& ".*?\.xml$", cutoff
On the line;
arrayHeaders = array("AccountSystem", "AsyncQueue")
Enter as many folder titles you want.
In this section;
select case strHeader
Case "AccountSystem"
'DELETE ALL ACCOUNTSYSTEM LOG FILES
cleanup "D:\MyApp\LOG\Accountsyste
Case "AsyncQueue"
'DELETE ALL ASYNCQUEUE LOG FILES
cleanup "D:\MyApp\LOG\AsyncQueue",
End Select
Add a new Case statement for each additional item in your array.
For example;
arrayHeaders = array("AccountSystem", "AsyncQueue","Temp")
select case strHeader
Case "AccountSystem"
'DELETE ALL ACCOUNTSYSTEM LOG FILES
cleanup "D:\MyApp\LOG\Accountsyste
Case "AsyncQueue"
'DELETE ALL ASYNCQUEUE LOG FILES
cleanup "D:\MyApp\LOG\AsyncQueue",
Case "Temp"
'DELETE ALL TEMP LOG FILES
cleanup "D:\MyApp\LOG\Temp", "^"&curyear& ".*?\.tmp$", cutoff
End Select
Select allOpen in new window