Micro Focus QTP (UFT) Forums
Editing a notepad file in QTP - Printable Version

+- Micro Focus QTP (UFT) Forums (https://www.learnqtp.com/forums)
+-- Forum: Micro Focus UFT (earlier known as QTP) (https://www.learnqtp.com/forums/Forum-Micro-Focus-UFT-earlier-known-as-QTP)
+--- Forum: UFT / QTP Beginners (https://www.learnqtp.com/forums/Forum-UFT-QTP-Beginners)
+--- Thread: Editing a notepad file in QTP (/Thread-Editing-a-notepad-file-in-QTP)



Editing a notepad file in QTP - arjun.singh - 03-23-2009

Hi,

Can anyone please help me.I am trying to open a saved notepad file,search a particular string,edit it,save the file and closes it.
How to do that in QTP?


RE: Editing a notepad file in QTP - Tarik Sheth - 03-23-2009

Hi,
You can try this.

Code:
dim fso,a


Const ForReading = 1, ForWriting = 2
' Create a text file
Set fso = CreateObject("Scripting.FileSystemObject")    
Set a = fso.CreateTextFile("C:\tarik.txt",True)    
a.WriteLine("This is a test")
a.Close

'Open and edit it
Set a= fso.OpenTextFile("C:\tarik.txt",ForWriting,True)
a.WriteLine("This is test1")
a.close



RE: Editing a notepad file in QTP - Jackomcnabb - 03-24-2009

Here is a Script I built to Read a file in and search for version Numbers this should give you an idea of how to search for a string in a file.

Code:
ExpectedVersion = Parameter("ExpectedVersion")
TDMRPT_File = Parameter("File_Path")
Record_Found = 0 ' initialize the "Record_Found" flag...

Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.OpenTextFile(TDMRPT_File)

Do while MyFile.AtEndofStream <> True
currentLine = MyFile.ReadLine
find_report_record = instr(1,currentLine,"VERSION",1)
Record_Found= Record_Found +find_report_record
If   find_report_record > 0 Then
    MyVersion = Trim(mid(currentLine,2,50))
        If  MyVersion <> " " Then
            splitstring=split(MyVersion,":")
            VersionNumber=replace(splitstring(1)," ","")
            Reporter.ReportEvent micPass, "Version Number Exist", "The Version Number was displayed in "&TDMRPT_File&" File "
            Valid_Version_Number ExpectedVersion, VersionNumber
        Else
        Reporter.ReportEvent micFail, "Version Number Exist", "The Version Number was NOT displayed in "&TDMRPT_File&" File "
        End If
End If
Loop ' AtEndOfStream

If  Record_Found = 0  Then
Reporter.ReportEvent micFail, "Version Number Exist", "The Version Number was NOT displayed in "&TDMRPT_File&" File "
End If



RE: Editing a notepad file in QTP - Ankesh - 10-20-2011

Hi All,

I know its an old post bt i am having same issue.

Can u tell me how to save the notepad after editing. I am using file system object.

Regards,
Ankesh
i have another issues. I have a notepad which contains only one single line. My objective is to replace the existing line with the new line and save the file.

Any help would be appreciated.

Regards,
Ankesh


RE: Editing a notepad file in QTP - rajpes - 10-20-2011

1.It is automatically saved when you perform those write operations

2.One way is, read all contents of notepad(ReadAll) into a string var.
Then replace(var,"oldstring","new string").
Get the file name in some other variable.delete the file.create a new notepad with same name and fileobj.Write var


RE: Editing a notepad file in QTP - Ankesh - 10-20-2011

Thanks rajpes for ur time. I have already thought of your suggestion first. I dont want to delete and create again. I want to do it in one shot.

I found one solution for it..and it is working for me.

My objective was to open a notepad, get its content, perform some matematical operations and generate a new string and finally write the new string to the file..



Code:
Const ForReading = 1, ForWriting = 2
Const TristateUseDefault = -2

strFilePath="C:\xyz.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set fileObject = fso.GetFile(strFilePath)
Set ts = fileObject.OpenAsTextStream(ForReading, TristateUseDefault)
objText = ts.ReadLine
print objText

strTest=Split(objText,vbTab)

'Split and get the value
strVar1=strTest(0)
strVar2=strTest(1)
strVar3=strTest(2)
strVar4=strTest(3)
strVar5=strTest(4)
strVar6=strTest(5)
strVar7=strTest(6)
strVar8=strTest(7)
strVar9=strTest(8)
strVar10=strTest(9)

'Get the values on which the operation has to be performed
strVar5=strVar5+1
strVar6=strVar6+1
strVar7=strVar7+0.01
strVar8=strVar8+0.01
strVar9=strVar9+0.01
strVar10=strVar10+0.01

'Create a new string to be written to file
strNewString=strVar1&vbTab&strVar2&vbTab&strVar3&vbTab&strVar4&vbTab&strVar5&vbTab&strVar6&vbTab&strVar7&vbTab&strVar8&vbTab&strVar9&vbTab&strVar10

print strNewString
ts.Close

'Open the file and write the content to the file. This will replace the old content
Set ts = fileObject.OpenAsTextStream(ForWriting, TristateUseDefault)
ts.Write strNewString
ts.Close
Set fso=Nothing


Regards,
Ankesh