Delete old Blackberry Enterprise Server (BES) logs folder using vbscript

Scenarios :
While working daily on ticket, You have lots of stuff to work on, in Between you got a mail from Monitoring Team saying that, Team the disk space got full for log drive on one of the BES servers. On top of it your Lead asks – cant you guys put one script to do the automation here.

So finally here is that automation, which is gonna to delete old date logs automatically (Its all up to you for how many days you wanted to retain)

Solution:
=======================================
Examples:

This command will move logs older than 7 days from the folder “C:\Scripts\logs” where folder name value only contains numbers like “21112010″.

cscript //NoLogo c:\scripts\deleteBESLogFolders.vbs “C:\Scripts\logs” 7 move>> c:\scripts\BESLOg_Folder_Delete.log

This command will delete logs older than 7 days from the folder “C:\Scripts\logs” where folder name value only contains numbers like “21112010″.

cscript //NoLogo c:\scripts\deleteBESLogFolders.vbs “C:\Scripts\logs” 7 delete>> c:\scripts\BESLOg_Folder_Delete.log

——————————————————————————————————————————————————-

‘Copy and paste all the text below and create deleteBESlogFolders.vbs file.

‘Script: deleteBESlogFolders.vbs
‘Created By: Manish Girdhar
‘Date: 26 Nov 2010
‘Purpose: To delete/move folders from given path and all subfolders below this folder
‘ Usage: cscript DeleteBESlogFolders.vbs {DriveLetter:\FolderName} {#ofDays} {Delete or Move}
‘ or: cscript DeleteBESlogFolders.vbs {\\servername\FolderName} {#ofDays} {Delete or Move}
‘ Usage: cscript DeleteBESLogFolders.vbs c:\BES\log 3 d
‘ (deletes folders older than 3 days from the \BES\log file on drive C:)
‘Usage: cscript DeleteBESLogFolders.vbs c:\BES\log 3 m
‘ (moves folders older than 3 days from the \BES\log file on drive C: to C:\BES_Log_Backup)

Const BACKUP_FOLDER = “C:\BES_Log_Backup” ‘Change path folder to move log files
creatfold
Set ObjUsrInput = WScript.Arguments
FolderName =ObjUsrInput(0)
Days=ObjUsrInput(1)
Action=LCase(ObjUsrInput(2))
’1
if action = “delete” or action = “move” or action = “d” or action = “m” then
set fso = createobject(“scripting.filesystemobject”)
set folders = fso.getfolder(FolderName)
datetoday = now()
wscript.echo “”
wscript.echo “”
wscript.echo “”
wscript.echo “”
newdate = dateadd(“d”, Days*-1, datetoday)
wscript.echo “Current Date (Today):” & now()
wscript.echo “==================================================================”
wscript.echo “STARTING: Folders where date created is older than date :” & newdate & ” Will be deleted / Moved.”
wscript.echo “<>”
wscript.echo “”
route folders
wscript.echo “”
wscript.echo “COMPLETED: All Folders created older than date :” & newdate & ” are deleted / Moved.”
wscript.echo “<>”

‘=== Route Function Defined
sub route( byref folders)
set subfolders = folders.subfolders
‘==== For loop started
for each folder in subfolders
wscript.echo “”
‘Check if Date is in limit
if folder.datecreated < newdate then
””check if folder name is numeric
if IsNumeric(folder.Name) = true then
‘==============================M
”if action is MOve
if action = “move” or action = “m” then
wscript.echo “__________________________________________________________________________”
wscript.echo “Move folder :” & folder.path
wscript.echo “”
folder.move BACKUP_FOLDER & “\” & folder.Name
‘track if error
if err 0 then
DisplayErrorInfo
else
wscript.echo “”
wscript.echo “Folder Moved :” & folder.path
wscript.echo “__________________________________________________________________________”
end if ‘ tracking error
end if
‘================================M
‘End if Action is Move
‘===================== D
‘if Action is delete
if action = “delete” or action = “d” then
dim foldpath
wscript.echo “__________________________________________________________________________”
foldpath = folder.path
wscript.echo “Deleting folder :” & foldpath
wscript.echo folder.path
folder.delete
if err 0 then
DisplayErrorInfo
else
wscript.echo “”
wscript.echo “Folder Deleted :” & foldpath
wscript.echo “__________________________________________________________________________”
end if
End if
‘End of action Delete
‘======================D
End if
end if
on error resume next
next
‘====== for loop ended
set subfolders = nothing
set files = nothing
end sub
”’Function Route Ended
Else ’1

wscript.echo “Correct Action type not defined”

End if ’1

Sub DisplayErrorInfo
WScript.Echo “Error: : ” & Err
WScript.Echo “Error (hex) : &H” & Hex(Err)
WScript.Echo “Source : ” & Err.Source
WScript.Echo “Description : ” & Err.Description
Err.Clear
End Sub

sub creatfold
dim objFSO
set objFSO = createobject(“Scripting.FileSystemObject”)
if objFSO.FolderExists(BACKUP_FOLDER) then
else
objFSO.CreateFolder(BACKUP_FOLDER)
end if
End sub

Note: The Content has been taken from one of my favorite Blog title