Wednesday, 1 August 2012

L24 - Working with Notepad

Problem statement : Working with notepad, open edit, write and close, here is an example of reading an ID saved in notepad and replace it with new ID that is entered in input box.

' Input box gets the ID saved previous time mechanism
' create the Final report file
Const ForAppending=8  ' We have to use second argument in OpenTextFile as value 8 . for appending mode.
Dim objFSO,objSetTextFile,strFileToWriteID
strFileToWriteID= "C:\QTP Test Class\Manu\Notepad\ID.txt"
' create object
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objSetTextFile = objFSO.OpenTextFile(strFileToWriteID,ForAppending)' Open it in Append Mode
objSetTextFile.WriteLine "Manuj"
objSetTextFile.close


' if does not exist then write the default TSOID first and close the object
If (objFSO.FileExists(strFileToWriteID)) Then
Else
Set objSetTextFile = objFSO.OpenTextFile(strFileToWriteID,ForAppending)' Open it in Append Mode
objSetTextFile.WriteLine "Manuj"
objSetTextFile.close
End if

' Read the TSO ID from the notepad file
Set objFSOInput = CreateObject("Scripting.FileSystemObject")
Set objReadFile = objFSOInput.GetFile (strFileToWriteID)
Set objReadFile = objFSOInput.OpenTextFile(strFileToWriteID, 1)   
strTSOID=ucase(trim(objReadFile.ReadLine))
objReadFile.close
' close the read object
strID=InputBox ("Enter ID used for the files ","Enter ID",strTSOID)

' Write the new ID in the file

If (objFSO.FileExists(strFileToWriteID)) Then
set f=objFSO.GetFile(strFileToWriteID)
f.Delete
End if

Set objSetTextFile = objFSO.OpenTextFile(strFileToWriteID,ForAppending,True)' Open it in Append Mode
objSetTextFile.WriteLine strID
objSetTextFile.close