Wednesday, April 4, 2012

Lotus Script to extract files

I am in the middle of replacing a large report routing system developed in Lotus Notes with a couple SharePoint sites and the Content Organizer feature.  Creating the new sites and routing the documents using the Content Organizer rules has proved easy.  I have to give big props to Microsoft on that feature.  The next big step came in extracting all the old documents and moving them to the SharePoint document libraries.  I took the code from (http://www.cubetoon.com/2007/accessing-the-local-file-sytem-from-lotusscript/) and customized it to do what I needed.  My script is below in case anyone is running into a similar requirement.



Sub Initialize
   
    Dim s As NotesSession
    Dim db As NotesDatabase
    Dim view As NotesView
    Dim Doc As NotesDocument
    Dim rtitem As Variant
    Dim CreatedDate As Variant
    Dim i As Integer
    Dim FileName As String
    Dim FileExtension As String
    Dim testString As String
    Dim SavedFileName As String
   
   
   
    Dim ExtractFolder As String
    ExtractFolder = "C:\ReportRouterFiles\"
   
    Set s = New NotesSession
    Set db = s.CurrentDatabase
    Set view = db.GetView ("By Report ID")
    Set doc = view.GetFirstDocument
   
    Set rtitem = doc.GetFirstItem ("Body")
   
    While Not ( doc Is Nothing )
        Set rtitem = doc.GetFirstItem ("Body")
       
        If (rtitem.type = RICHTEXT) Then
            Forall o In rtitem.EmbeddedObjects
                CreatedDate = Format(doc.Created, "yyyymmdd")
               
                FileName = o.Name
               
                testString = Mid(FileName, Len(FileName) - 3, 1)
               
                If (testString = ".") Then
                    FileExtension = Mid(FileName, Len(FileName) - 3, 4)
                    FileName = Mid(FileName, 1, Len(FileName) - 4)
                   
                Else ' I'm going to assume the file extension is 4
                    FileExtension = Mid(FileName, Len(FileName) - 3, 5)
                    FileName = Mid(FileName, 1, Len(FileName) - 5)
                   
                End If
               
                SavedFileName = FileName & "_" & CreatedDate & FileExtension
               
               
                If Dir$(ExtractFolder & SavedFileName, 0 ) = "" Then
                    'there is no file for that day, go ahead and extract
                Else
                    'the file exists, so append a unique number to it before extracting
                    i = 1
                    While i < 10
                        SavedFileName = FileName & "_" & CreatedDate & "_" & i & FileExtension
                       
                        If Dir$(ExtractFolder & SavedFileName, 0 ) = "" Then
                            i = 11
                        End If
                       
                        i = i + 1
                    Wend
                End If
               
            'could put check here in case we have a _9 file and warn me, but should not have more than 1 so I'm not worried about it
                o.extractFile(ExtractFolder & SavedFileName)
            End Forall
        End If
       
        Set doc = view.GetNextDocument( doc )
    Wend
   
End Sub