Finding a folder by name, quickest way?

Currently using this code:---------------

Forall varView In dbMail.Views

If RetainFolder(varView) Then

	Call ProcessViewOrFolder(varView, MyList)

End If

End Forall


Boy is it slow. RetainFolder does an InStr check on the folder name (if it is a folder) to see if it has a certain string. Then scrolls through the docs in that folder and dumps their NoteIDs into MyList.

Is there any way to speed this up?

Subject: Finding a folder by name, quickest way?

Using a NotesNoteCollection you should be able to get just the folders that match your name criteria (test of $TITLE item), with a very fast search. Then you have to open the note as a NotesDocument to find out the actual name, and use that to open the folder using GetView.

Probably the reason your current code is so slow, is that accessing a NotesView object causes the view index to be updated.

Subject: RE: Finding a folder by name, quickest way?

Does anyone have any example code on this?

Subject: RE: Finding a folder by name, quickest way?

Caveat: I don’t consider this function to have been adequately tested – I only just wrote it and did some tire-kicking. If it needs corrections please let me know.

Function GetFoldersLike(db As NotesDatabase, Byval strMatch As String, Byval options As Integer) As Variant

%rem

Args are a database, a wildcard match string in the format used by @Matches macro function,

and an option flag which may contain 1 to specify case-insensitive search. Return value

is variant array of NotesView objects – folders whose titles matched the search string.

If no folder matched, the return value is EMPTY.

Because of an internal implementation issue, this routine may fail with an error if the search

matches a folder that does not have any unique name or alias (among views and folders in

that database). If that happens, your alternative is to iterate through db.Views.

%end rem

Dim notecoll As NotesNoteCollection
Set notecoll = db.CreateNoteCollection(False)
notecoll.SelectFolders = True
If options And 1 Then
	notecoll.SelectionFormula = { @Matches(@lowercase($Title); } & _

macroquote(Lcase(strMatch)) & {) }
Else
notecoll.SelectionFormula = { @Matches($Title; } & macroquote(strMatch) & {) }
End If
notecoll.BuildCollection
If notecoll.Count > 0 Then
Redim retval(0 To notecoll.count - 1) As NotesView
Dim docDes As NotesDocument ’ the folder design note as a document
Dim strNoteID As String
Dim intIndex As Integer, intAliasID As Integer
Dim folderTitles As Variant
Dim viewTemp As NotesView
strNoteID = notecoll.GetFirstNoteId
Do ’ for each noteID in the result
Set docDes = db.GetDocumentByID(strNoteID)
folderTitles = Split(docDes.GetItemValue(“$TITLE”)(0), {|})

		' try each name or alias to locate the folder using GetView.
		For intAliasID = Ubound(folderTitles) To 0 Step -1
			Set viewTemp = db.GetView(folderTitles(intAliasID))

			If Not (viewTemp Is Nothing) Then
				If viewTemp.UniversalID = docDes.UniversalID Then Exit For

			End If
			' if we reach here the alias was not unique and we try other names.
		Next
		If intAliasID < 0 Then
			Error 20340, {Folder name "} & Join(folderTitles, " | ") & _

{" is not sufficiently unique to locate the folder.}
End If
Set retval(intIndex) = viewTemp
intIndex = intIndex + 1
Delete docDes
strNoteID = notecoll.GetNextNoteId(strNoteID)
Loop Until strNoteID = “”
GetFoldersLike = retval
End If
End Function

Function macroquote(Byval x As String) As String

’ convert string to macro code quoted string; change all " to " and add quotes at start and end.
Dim fromArr(0 To 0) As String
Dim toArr(0 To 0) As String
Dim targetArr(0 To 0) As String
If Instr(x, {“}) Then
fromArr(0) = {”}
toArr(0) = {"}
targetArr(0) = x
Dim result As Variant
result = Replace(targetArr, fromArr, toArr)
macroquote = {“} & result(0) & {”}
Else
macroquote = {“} & x & {”}
End If
End Function

Subject: RE: Finding a folder by name, quickest way?

How do I do the search to get views in a notesnotecollection?