OpenLog - Feature Request: Purge Old Documents AgentCreated on |
May 4, 2004 |
Created by |
John Smart |
Status |
Submitted |
I wrote an agent to purge old documents. Slightly cheesey since it uses the "Events\By Date" view. I should at least have added an alias to the view, but I wanted to change as few elements as possible to preserve my ability to upgrade to later releases.
Scheduled to run on the first of the month. Uses a DbProfile profile document. If one doesn't exist, it creates one. I didn't bother making the functionality to actually edit the profile document yet.
Sub Initialize
Dim db As NotesDatabase
Dim doc As NotesDocument ' from view
Dim docNext As NotesDocument ' view.GetNextDocument(doc)
Dim docProfile As NotesDocument ' db.GetProfileDocument("DbProfile")
Dim dtPurge As Variant ' @Adjust(today, 0; -iPurgeMonths; 0; 0; 0; 0)
Dim iPurgeMonths As Integer ' number of months to leave in db
Dim iRemoved As Long ' number of documents removed by this agent
Dim itemPurgeMonths As NotesItem ' the PurgeMonths field on docProfile
Dim ndt As NotesDateTime ' today - iPurgeMonths
Dim sngKey As Single ' YYYY.MM number correlating to dtPurge, used to match against similar values within view
Dim view As NotesView ' Events\By Date view
Dim session As New NotesSession
On Error Goto ErrorSub
Set db = session.CurrentDatabase
Set docProfile = db.GetProfileDocument("DbProfile")
Set itemPurgeMonths = docProfile.GetFirstItem("PurgeMonths")
If itemPurgeMonths Is Nothing Then
iPurgeMonths = 3
With docProfile
.PurgeMonths = 3
.Form = "DbProfile"
.Save True, True
End With
Else
iPurgeMonths = itemPurgeMonths.Values(0)
End If
Set ndt = New NotesDateTime("Today")
Call ndt.AdjustMonth(0 - iPurgeMonths)
dtPurge = ndt.LocalTime
sngKey = Year(dtPurge) + (Month(dtPurge) - iPurgeMonths) * 0.01
Set view = db.GetView("Events\by Date")
Set doc = view.GetFirstDocument
Do Until doc Is Nothing
If doc.ColumnValues(0) > sngKey Then Exit Do
Set docNext = view.GetNextDocument(doc)
Call doc.Remove(False)
Loop
Set ndt = New NotesDateTime("Today")
Call ndt.AdjustMonth(0 - iPurgeMonths)
dtPurge = ndt.LocalTime
sngKey = Year(dtPurge) + (Month(dtPurge) - iPurgeMonths) * 0.01
Set view = db.GetView("Events\by Date")
Set doc = view.GetFirstDocument
Do Until doc Is Nothing
If doc.ColumnValues(0) > sngKey Then Exit Do
Set docNext = view.GetNextDocument(doc)
Call doc.Remove(False)
iRemoved = iRemoved + 1
Set doc = docNext
Loop
ExitSub:
If iRemoved Then Call LogEvent("Removed " & iRemoved & " old documents.", SEVERITY_LOW, Nothing)
Exit Sub
ErrorSub:
Call LogError()
On Error Resume Next
Resume ExitSub
End Sub
Taken Actions by OwnersNo actions have been taken yet.Documents