Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to open a file in one script and write into it in another script
#1
Solved: 11 Years, 4 Months, 4 Weeks ago
Hi All,

Can anybody tell me how to open a file in one script and write into it in a different script?

I have to open a file once in login script and use it in all the test cases script to write the result of the test case into it.

I have written a code like this in login script

Code:
Dim fso
Set fso = createobject("Scripting.FileSystemObject")
Set tf = fso.CreateTextFile("path", True)
Now in my TEST_CASE 1 script I have to write

Code:
tf.writeline("Pass")

But this doesn't work.
Reply
#2
Solved: 11 Years, 4 Months, 4 Weeks ago
Just curious, why can't you just reopen the file in the next script? This approach would also have the advantage of being able to open the file in "append" mode so that you aren't accidentally writing over your previous results:

Script 1 Wrote:'Script 1 (Login script?)
Dim fso, tf
Set fso = CreateObject("Scripting.FileSystemObject")
Set tf = fso.CreateTextFile("C:/QTP.txt", True)
tf.WriteLine("Pass")
tf.Close
Set tf = Nothing
Set fso = Nothing

Script 2 Wrote:'Script 2
Dim ForAppending, fso, tf
ForAppending = 8
Set fso = CreateObject("Scripting.FileSystemObject")
Set tf = fso.OpenTextFile("C:/QTP.txt", ForAppending)
tf.WriteLine("Fail")
tf.Close
Set tf = Nothing
Set fso = Nothing

If you are talking about a test that calls reusable actions and you want to be able to hand the text file between the actions, you could use an environment variable. This works as long as the scripts are calling each other (i.e. clicking run once) since the environment variable only lives for the current test run:

Quote:'Store the text file object in a varaible -->
Environment("ResultsFileObject").Value = tf

'Reuse the text file object somewhere else -->
Environment("ResultsFileObject").WriteLine("Pass")
Reply
#3
Solved: 11 Years, 4 Months, 4 Weeks ago
Thanks for the reply. Yes I wanted to create a file in one script and use it all the other scripts for appending.
Reply
#4
Solved: 11 Years, 4 Months, 4 Weeks ago
So have you tried one of the strategies from my first post?
Reply
#5
Solved: 11 Years, 4 Months, 4 Weeks ago
Hi, I've developed some code here. I' working on RFT now... If any syntax's mixed with java, Plz ignore and convert into VBScript.
I'm writing this with out using QTP. just I analyzed and developed... check it once and revert me if you get any issues with this.

Here, you can first create a text file at your desired location and then pass the values required to the function. just call this function and conclude that the passing text has been appended to file.

Code:
public function fncAppendFile(path_of_the_file, updating_text_into_file)
dim fso, fs
dim txtBefore, txtAfter
int intlenBefore, intlenAfter, intlenAppTxtString

intlenAppTxtString = len(updating_text_into_file)

set fso = createobject("Scripting.filesystemobject")

set fs = fso.opentextfile("path_of_the_file", 8)

txtBefore = fs.readall
intlenBefore = len(txtBefore)

fs.writeline(updating_text_into_file)
fs.save

txtAfter = fs.readall
intlenAfter = len(txtAfter)

if (intlenAfter=intlenBefore+intlenAppTxtString) Then

    fncAppendFile = True
    fs.close

else

    fncAppendFile = False
    fs.close

End if

set fso = nothing
set fs = nothing

End function

Dim path_of_the_file, updating_text_into_file

path_of_the_file = " your desired path of the text file"
updating_text_into_file = " The text you want to append to text file is here"

result = fncAppendFile(path_of_the_file, updating_text_into_file)

if(result = True) Then
reporter.reportevent "micpass", "The string is appended to file successfully"
else
reporter.reportevent "micfail", "The string is not appended to file"
end if
Thanks & Regards,
Baba Fakruddin.D
Reply
#6
Solved: 11 Years, 4 Months, 4 Weeks ago
Hi All,

Thanks for the reply.

@cdesserich, The environment variable still not works and displays an error "Object not found in the OR" in the script where I am using tf.writeline.

But my issue is resolved for now though I have not tried different ways. This is what I do now.

Currently I am using my library file to open the Result.txt file and I am declaring the "tf" variable as Public and attaching the library file with my script and this works fine now.

@Baba, later I am going to try out the script which you have given too.

Regards,
Reema.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to send/pass UFT Run Error Dialog as output value to main driver script lravi4u 0 635 11-05-2023, 03:55 PM
Last Post: lravi4u
  How do you Change Run mode mid script? Caleytown 6 7,360 03-25-2021, 08:27 AM
Last Post: RB26578
  picking different points in UFT using VB Script azjk786 0 1,069 12-14-2020, 09:57 AM
Last Post: azjk786
  script for mouseover rumitkon 2 2,032 02-20-2019, 12:52 AM
Last Post: rumitkon
  Call Stack in QTP Script smitapawar610 0 1,739 12-03-2018, 10:42 AM
Last Post: smitapawar610

Forum Jump:


Users browsing this thread: 1 Guest(s)