Pages

Wednesday, 19 October 2011

QTP: Example of how to write text to a file


' =============================================================
' function: WriteLog
' desc :    Writes a message to a log file. File is created
'           inside a Log folder of the current directory
' params :  strCode is a code to prefix the message with
'           strMessage is the message to add to the file
' returns : void
' =============================================================
Function WriteLog(strCodestrMessage)

Dim objFS
Dim objFile
Dim objFolder
Dim strFileName

' create a file system object
Set objFS = CreateObject("Scripting.FileSystemObject")

' is there a log folder in the directory that we are currently working
If Not objFS.FolderExists(objFS.GetAbsolutePathName("."& "\log"Then

    ' if there is no log folder, create one
    Set objFolder = objFS.CreateFolder(objFS.GetAbsolutePathName("."& "\log"

End If ' folder exists

' set a name for the log file using year, month and day values
strFileName = objFS.GetAbsolutePathName("."& "\log\" & year(date& month(date& day(date& ".log"

' create the log file
Set objFile = objFS.OpenTextFile(strFileName8True)

' in case of any issues writing the file
On Error Resume Next

' write the log entry, include a carriage return
objFile.Write Date & ", " & Time & ", " & strCode & ", " & strMessage & vbcrlf

' disable the on error statement
On Error GoTo 0

' close the log file
objFile.Close

' destrory the object
Set objFS = Nothing

End Function ' WriteLog

0 comments:

Post a Comment