TESTEVERYTHING

Saturday 25 June 2011

Adding Data to the File Working with files(FileSystemObject) / QTP

Adding Data to the File

Once the text file is created, add data to the file using the following three steps:
Open the text file.
Write the data.
Close the file.
To open an existing file, use either the OpenTextFile method of the FileSystemObject object or the OpenAsTextStream method of the File object.
To write data to the open text file, use the Write, WriteLine, or WriteBlankLines methods of the TextStream object, according to the tasks outlined in the following table
Task
Method
Write data to an open text file without a trailing newline character.      
Write
Write data to an open text file with a trailing newline character.
WriteLine
Write one or more blank lines to an open text file.
WriteBlankLines
To close an open file, use the Close method of the TextStream object.
Note The newline character contains a character or characters (depending on the operating system) to advance the cursor to the beginning of the next line (carriage return/line feed). Be aware that the end of some strings may already have such nonprinting characters.
The following example demonstrates how to open a file, use all three write methods to add data to the file, and then close the file:
[VBScript]
Sub CreateFile()
   Dim fso, tf
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set tf = fso.CreateTextFile("c:\testfile.txt", True)
   ' Write a line with a newline character.
   tf.WriteLine("Testing 1, 2, 3.") 
   ' Write three newline characters to the file.        
   tf.WriteBlankLines(3) 
   ' Write a line.
   tf.Write ("This is a test.") 
   tf.Close
End Sub

No comments:

Post a Comment

Which one is right ?

Translate







Tweet