Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Editing a notepad file in QTP
#1
Solved: 11 Years, 4 Months ago
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?
Reply
#2
Solved: 11 Years, 4 Months ago
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
Reply
#3
Solved: 11 Years, 4 Months ago
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
Reply
#4
Solved: 11 Years, 4 Months ago
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
Reply
#5
Solved: 11 Years, 4 Months ago
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
Reply
#6
Solved: 11 Years, 4 Months ago
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Fifth word from every thousand line in notepad files. sallu.iet@gmail.com 0 946 01-17-2020, 09:58 AM
Last Post: sallu.iet@gmail.com
  QTP 11 Evaluation installation file not having executable file Akhila 2 4,274 10-19-2012, 12:04 AM
Last Post: gdp9600
  Script Editing Problem neerdeth 4 3,542 07-20-2012, 10:12 AM
Last Post: Shridevi.Salagare
  Copy Data from Notepad jegansurya 3 6,785 07-04-2012, 05:31 PM
Last Post: Shridevi.Salagare
  Cannot find the "Edit" object's parent "Notepad" gnanajyothi 1 3,149 04-09-2012, 11:12 AM
Last Post: sshukla12

Forum Jump:


Users browsing this thread: 1 Guest(s)