Adding a logfile to your VBS scripts

Share your tips, tricks and favorite iMacros macros, scripts and applications for web automation in general here.
Post Reply
Hannes, Tech Support

Adding a logfile to your VBS scripts

Post by Hannes, Tech Support » Thu Oct 18, 2007 7:34 am

The following script contains the necessary lines of code to add logging to an VBS script. Please note that in "real world" applications, the logging should be linked to the iMacros methods' return values (i.e. they should reflect whether a command was performed successfully).

Here's how the script's simple log looks like when run only once:

Code: Select all

10/18/2007 9:11:55 Startup 
Init...Done
Running macro "#Current"...Done
And here's the script:

Code: Select all

Option Explicit
Dim LOG_FILE

' global settings
LOG_FILE="C:\Documents and Settings\All Users\Documents\iMacros\Downloads\script.with.logging.log" 

' File access constants
Const FOR_READING = 1
Const FOR_WRITING = 2
Const FOR_APPENDING = 8 

'open log file for appending
Dim objFileSystemLog,logFile
Set objFileSystemLog = CreateObject("Scripting.fileSystemObject")
Set logFile = objFileSystemLog.OpenTextFile(LOG_FILE, FOR_APPENDING, true)


'initialize iMacros instance
Dim iim1, iret
loggingLine(vbNewLine+vbNewLine + Cstr(now()) + " Startup ")

logging("Init...")
set iim1= CreateObject ("imacros")
iret = iim1.iimInit ("",TRUE)
loggingLine("Done")

logging("Running macro ""#Current""...")
iret = iim1.iimPlay("#Current")
loggingLine("Done")


'##############
'# end of main script
'##############

sub logging (myText)
	logFile.write(myText)
end sub 'logging

sub loggingLine (myText)
	logFile.write(myText+vbNewLine)
end sub
Post Reply