For the
DoFoldering event, the following script example shows how to create folders, move documents to
corresponding folders, and how to set a value for a folder field.
1: ' If folders exist, in the BeforeAutoFoldering event
2: ' they were deleted And All documents
3: ' copied back to the root folder.
4: ' Now they are being put into appropriate folders again,
5: ' each folder containing only documents
6: ' with the same ProcessID.
7: Private Sub Batch_DoFoldering(pXRootFolder As CASCADELib.CscXFolder)
8: Dim DocIndex As Long
9: Dim pXDoc As CscXDocument
10: Dim pFolder As CscXFolder
11: Dim ProcessID As String
12:
13: ' Loop over all documents in the root folder
14: For DocIndex = pXRootFolder.DocInfos.Count - 1 To 0 Step - 1
15:
16: ' Get the current document and its ProcessID
17: Set pXDoc = pXRootFolder.DocInfos(DocIndex).XDocument
18: ProcessID = pXDoc.Fields.ItemByName("ProcessID").Text
19:
20: ' Find a folder with this Process ID
21: Set pFolder = GetFolderByProcessID(pXRootFolder,ProcessID)
22:
23: ' If no such folder was found, create it and init
24: ' its ProcessID field
25: If pFolder Is Nothing Then
26: Set pFolder = New CscXFolder
27: pXRootFolder.Folders.Add(pFolder)
28: pFolder.Fields.ItemByName("ProcessID").Text = ProcessID
29: End If
30:
31: ' Move the document into the folder
32: pFolder.DocInfos.MoveInto(pXRootFolder.DocInfos(DocIndex), 0)
33: Next DocIndex
34: End Sub
35:
36: ' This is a helper function that searches the root folder
37: ' for a subfolder with
38: ' a certain value in its ProcessID field
39: Private Function GetFolderByProcessID(pRootFolder As CscXFolder, _
40: ProcessID As String) As CscXFolder
41: Dim FolderIndex As Long
42: Dim pXFolder As CscXFolder
43:
44: For FolderIndex = 0 To pRootFolder.Folders.Count - 1
45: Set pXFolder = pRootFolder.Folders(FolderIndex)
46: If pXFolder.Fields.ItemByName("ProcessID").Text = ProcessID Then
47: Set GetFolderByProcessID = pXFolder
48: Exit Function
49: End If
50: Next FolderIndex
51: Set GetFolderByProcessID = Nothing
52: End Function
This is what the script does:
-
Line 7: The
DoFoldering defines the event handler that is called. The
pXRootFolder object contains all documents. We assume that there are no folders present yet. Our goal
is to create one folder for each ProcessID we find in the ProcessID fields of the documents and move the respective documents into
that folder.
-
Line 14: Loop over all documents in the root folder.
-
Line 17: Get the current document.
-
Line 18: Get the ProcessID from the ProcessID field of the current document.
-
Line 21: Try to find a folder that contains the documents with the current ProcessID. At the beginning, no such folder
exists and the helper function returns nothing. The helper function is described later.
-
Line 26-27: If no such folder is found, we create one and add it to the root folder.
-
Line 28: For the newly created folder, save the ProcessID in the ProcessID folder field
-
Line 32: pFolder now is either the newly created folder or one that already existed. In both cases, it is the one that
matches the current ProcessID.
-
Line 39-52: This helper function checks all folders in the root folder to see if one of them matches the processID of
the current document. If so, it returns that folder, else it returns
Nothing.
After the
DoFoldering event, the batch now contains as many folders as different ProcessID values are present in the
documents.