Monday, August 17, 2009

Script to recursively delete Oracle Log Files under Windows

Script to recursively delete Oracle Log Files under Windows

Set the number of days you want to keep data for with the following variable

dtmDate = Date - 20

Specify top level dir for your bdump , adump , cdump etc


Set Thefolder = objFSO.GetFolder("S:\ADMIN\DB_SID")

It will do a recursive search of all folders e.g. bdump , adump , cdump , udump
under the top level folder

Specify file types you wish to delete here

If UCase(objFSO.GetExtensionName (AFile)) = "TRC" Then   

            Killfile (Afile)
        ElseIf UCase(objFSO.GetExtensionName (AFile)) = "TMP" Then
            Killfile (Afile)
        ElseIf UCase(objFSO.GetExtensionName (AFile)) = "ERR" Then

            Killfile (Afile)
        ElseIf Ucase(objFSO.GetExtensionName (AFile)) = "DMP" Then
            Killfile (Afile)
        ElseIf Ucase(objFSO.GetExtensionName (AFile)) = "TRW" Then

            Killfile (Afile)
        End If


Here is the actual script , cut and paste the script with the relevant change into a text file
and rename it to a file name with a .vbs extension e.g. delete_oracle_log.vbs
You can schedule the script to run periodically using windows scheduler

'==========================================================================

'==========================================================================

'Current Date minus 20 days
Dim sMsg
dtmDate = Date - 20

strTargetDate = ConvDate(dtmDate)

FolderCount = 0
DeletedCount = 0

'strComputer = "."
'Set objWMIService = GetObject("winmgmts:\\"& strComputer & "\root\cimv2")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objWSH = CreateObject("WScript.Shell")

'folder dialog Box
Const WINDOW_HANDLE = 0
Const OPTIONS = 0

Set Thefolder = objFSO.GetFolder("J:\ADMIN\DB_SID")
StartTime = Now
WorkWithSubFolders Thefolder
EndTime = Now
sMsg = "Deleted: " & DeletedCount & " File(s) from " & FolderCount & " Directory(s)!!" & _
VbCrLf&VbCrLf & "Script Started: " & vbTab&StartTime & _
VbCrLf & "Script Ended: " & vbTab&EndTime
objWSH.LogEvent 0, sMsg

'Credit for this sub goes to S. Hussain Akbar
Sub WorkWithSubFolders (AFolder)
Dim MoreFolders, TempFolder
FolderCount = FolderCount + 1

CheckExt AFolder
Set Morefolders = AFolder.Subfolders
For Each TempFolder In MoreFolders
WorkWithSubFolders (TempFolder)
Next

End Sub

Sub CheckExt (AFolder)
Dim Afile, TheFiles

On Error Resume Next
Set TheFiles = AFolder.Files
For Each Afile In TheFiles
If UCase(objFSO.GetExtensionName (AFile)) = "TRC" Then
Killfile (Afile)
ElseIf UCase(objFSO.GetExtensionName (AFile)) = "TMP" Then
Killfile (Afile)
ElseIf UCase(objFSO.GetExtensionName (AFile)) = "ERR" Then
Killfile (Afile)
ElseIf Ucase(objFSO.GetExtensionName (AFile)) = "DMP" Then
Killfile (Afile)
ElseIf Ucase(objFSO.GetExtensionName (AFile)) = "TRW" Then
Killfile (Afile)
End If
Next
If (DeletedCount > 0) And (DeletedCount Mod 10 = 0) Then
sMsg = "Have worked so far with " & FolderCount & " folders." & VbCrLf&VbCrLf
sMsg = sMsg & "And have deleted " & DeletedCount & " files."
objWSH.LogEvent 1, sMsg
End If
End Sub

Sub Killfile (AFile)
On Error Resume Next
strDate = ConvDate(Afile.DatelastModified)
If strDate < strTargetDate Then
objFSO.DeleteFile (AFile)
DeletedCount = DeletedCount + 1
End If
End Sub

Function ConvDate (sDate) 'Converts MM/DD/YYYY HH:MM:SS to string YYYYMMDD
strModifyDay = day(sDate)
If len(strModifyDay) < 2 Then
strModifyDay = "0" & strModifyDay
End If
strModifyMonth = Month(sDate)
If len(strModifyMonth) < 2 Then
strModifyMonth = "0" & strModifyMonth
End If
strModifyYear = Year(sDate)
ConvDate = strModifyYear & strModifyMonth & strModifyDay

End Function

No comments: