Tuesday, July 19, 2016

Creating text file with lotusscript

Following is a block of lotusscript code that helps one understand how a text file can be created and updated using Freefile and NotesStream concepts

As a catch, the code employs a validation mechanism that check if a given file exists or not and create it only if its not found.

Hope this helps

Function updateLog(logText As String)
      Dim session As New NotesSession
      Dim stream As NotesStream
     
      On Error GoTo errHandler
  Set session = New NotesSession
 
  REM Create stream and display properties
  Set stream = session.CreateStream
 
  'check if log file exists
  If Not stream.Open("C:\\ak\\log.txt") Then
      'if log file doesnot exist then create one and add a time stamp to it
      Dim fileNum As Integer
      fileNum% = FreeFile()
      Open "/ww414/notes/ebcdicfile.txt" For Output As fileNum%
     
      Print #fileNum%, "Created: " ; CStr(Now)
     
      Close fileNum%
      'this should have created the log file. see if it existis now
      If Not stream.Open("C:\\ak\\log.txt") Then
            'if log file has not been created yet then let the user know of the error that blocks the operation
            print "Log file Is inaccessible"
            Exit Function
      End if
  End If

  'update log
  Call stream.WriteText(Chr(13) & Chr(10) & CStr(Now) & "  ::  >  " & logText)
  'close stream/file open in memory
  Call stream.Close()
Exit Function
errHandler:
'display unhandled errors
print Error & " on line " & CStr(Erl)
Exit function

End function

'now try producing some logs with the following statements
updateLog "This is a sample log"
updateLog "This is an other sample log"
updateLog "This is yet an other sample log"
updateLog "I am done logging"
updateLog "forget it bye bye"

'you can view the results on the text file or through browser as follows