Tricentis Tosca 16.0 Released on Feb-2023 ----- UFT has been upgraded from UFT 15.0.1 to UFT One 15.0.2, Beginning at November 2020.

Monday 1 February 2021

Methods and Properties of Textfiles

We can read the content of a text file using File System Object methods and properties following are the various methods and properties used to extract data from text file.

Before reading content from a file, we have to create instance of file system object.

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTxtFile = objFSO.OpenTextFile(FilePath,iomode)

IO mode can have following values:

8 - Appending  –   Opens the file for appending, new content is appended at the end of file.
1 - Reading      –   Opens the file for reading.
2 - Writing       –   Opens the file for writing, does overwrite the existing content in file.


Methods

1. ReadAll It stores all the content of a file in a variable as string.
 sContent = objTxtFile.ReadAll

2. ReadLine : It allows a script to read individual lines in a text file.
sContent = objTxtFile.Readline.

Below code snippet shows how to read through each line.
 i=0
Do Until objTxtFile.AtEndOfStream
   sContent = objTxtFile.ReadLine
   sDataArray(i) = objTxtFile.ReadLine
   MsgBox sContent
   i = i+1
Loop

3. Read(chr):  The Read method allows you to read only a specified number of characters.
Stores number of characters as defined in chr.
sContent = objTxtFile.Read(5)

4. Skip(chr) : This method skips the number of characters defined in chr from the text file
skip(5)

5. SkipLine: This method skips line of data from the text file.
skipline()

6. AtEndOfLine :  Returns true if the file pointer is positioned immediately before the end-of-line marker in a TextStream file. 
It is useful to find characters in a line or reading characters till end of line.

Do While file.AtEndOfLine <> True 

7. Line: It gives the current line number of the textStream.
objTxtFile.line()

8. Column :  Read-only property that returns the column number of the current character position in a TextStream file.
objTxtFile.Column()      

9. OpenasTextStream : This method is used with getfile method to read from file.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTxtFile = objFSO.getFile(FilePath)
var objts = objTxtFile.OpenAsTextStream(iomode)

10 Close:  Closes an open TextStream file
Syntax: objTso.Close
Arguments:
objTso: (Required) The name of a TextStream Object previously instantiated.


Examples    


Read :  Reads a specified number of characters from a TextStream file and returns the resulting string.

Syntax: strChars = objTso.Read(numCharacters)

Example:    

Set fso=CreateObject("Scripting.FileSystemObject")

'Open the file "qtptest.txt" in writing mode.
Set qfile=fso.OpenTextFile("C:\qtptest.txt",2,True)

'write contents to the file into two newlines
qfile.Writeline  "Welcome to the World of QTP"
qfile.Writeline  "the file name is qtptest.txt"

'Open the file "qtptest.txt" in reading mode.
Set qfile=fso.OpenTextFile("C:\qtptest.txt",1,True)

'Read  characters from the file
Msgbox  qfile.Read(10)  
              
'Close the files
qfile.Close
'Release the allocated objects
Set qfile=nothing
Set fso=nothing

 'Output --> "Welcome to"  will be read


ReadAll :  Reads the entire TextStream file and returns the resulting string.

Syntax: strChars = objTso.ReadAll

Example:

Set fso=CreateObject("Scripting.FileSystemObject")

'Open the file "qtptest.txt" in writing mode.
Set qfile=fso.OpenTextFile("C:\qtptest.txt",2,True)

'write contents to the file into two newlines
qfile.Writeline  "Welcome to the World of QTP"
qfile.Writeline  "the file name is qtptest.txt"

'Open the file "qtptest.txt" in reading mode.
Set qfile=fso.OpenTextFile("C:\qtptest.txt",1,True)

'Read  the entire contents of  priously written file
Msgbox  qfile.ReadAll  

'Close the files
qfile.Close
'Release the allocated objects
Set qfile=nothing
Set fso=nothing

'Output --> Displays the entire file.


ReadLine: Reads an entire line (up to, but not including, the newline character) from a TextStream file and returns the resulting string.

Syntax: strChars = objTso.ReadLine

Example:

Set fso=CreateObject("Scripting.FileSystemObject")

'Open the file "qtptest.txt" in writing mode.
Set qfile=fso.OpenTextFile("C:\qtptest.txt",2,True)

'write contents to the file into two newlines
qfile.Writeline  "Welcome to the World of QTP"
qfile.Writeline  "the file name is qtptest.txt"

'Open the file "qtptest.txt" in reading mode.
Set qfile=fso.OpenTextFile("C:\qtptest.txt",1,True)

'Read  the entire contents of  priously written file
Do while qfile.AtEndOfStream <> true
     Msgbox  qfile.ReadLine  
Loop

'Close the files
qfile.Close
'Release the allocated objects
Set qfile=nothing
Set fso=nothing

'Output --> The file will be read line line by line.


Write:  Writes a specified string to a TextStream file.

Syntax: objTso.Write(string)

Example:    

Set fso=CreateObject("Scripting.FileSystemObject")

'Open the file "qtptest.txt" in writing mode.
Set qfile=fso.OpenTextFile("C:\qtptest.txt",2,True)

'write contents to the file into a single line
qfile.Write  "Welcome to the World of QTP"
qfile.Write "the file name is qtptest.txt"

'Open the file "qtptest.txt" in reading mode.
Set qfile=fso.OpenTextFile("C:\qtptest.txt",1,True)

'Read  the entire contents of  priously written file
Do while qfile.AtEndOfStream <> true
   Msgbox  qfile.ReadLine
Loop

'Close the files
qfile.Close
'Release the allocated objects
Set qfile=nothing
Set fso=nothing

 'Output --> The file will contain the above written content  in  single line.


WriteLine: Writes a specified string and newline character to a TextStream file.

Syntax: objTso.WriteLine([string])

Example:

Set fso=CreateObject("Scripting.FileSystemObject")

'Open the file "qtptest.txt" in writing mode.
Set qfile=fso.OpenTextFile("C:\qtptest.txt",2,True)

'write contents to the file into two newlines
qfile.Writeline  "Welcome to the World of QTP"
qfile.Writeline  "the file name is qtptest.txt"

'Open the file "qtptest.txt" in reading mode.
Set qfile=fso.OpenTextFile("C:\qtptest.txt",1,True)

'Read  the entire contents of  priously written file
Do while qfile.AtEndOfStream <> true
   Msgbox  qfile.WriteLine
Loop

'Close the files
qfile.Close
'Release the allocated objects
Set qfile=nothing
Set fso=nothing

'Output --> The file will contain the above written content  line by line.


WriteBlankLines: Writes a specified number of newline characters to a TextStream file.

Syntax: objTso.WriteBlankLines(numLines)

Example:

Set fso=CreateObject("Scripting.FileSystemObject")

'Open the file "qtptest.txt" in writing mode.
Set qfile=fso.OpenTextFile("C:\qtptest.txt",2,True)

'write contents to the file into two newlines
qfile.Writeline  "Welcome to the World of QTP"

'will insert 4 new  blank lines
qfile.WriteBlankLines(4)
qfile.Writeline  "the file name is qtptest.txt"

'Open the file "qtptest.txt" in reading mode.
Set qfile=fso.OpenTextFile("C:\qtptest.txt",1,True)

'Read  the entire contents of  priously written file
Do while qfile.AtEndOfStream <> true
   Msgbox  qfile.ReadLine
Loop

'Close the files
qfile.Close
'Release the allocated objects
Set qfile=nothing
Set fso=nothing

'Output --> The file will be read file line by line.

Properties:

AtEndOfLine:   Indicates whether the file pointer is positioned immediately before the end-of line marker in a TextStream file.

Syntax: objTso.AtEndOfLine

Arguments:

objTso: (Required) The name of a TextStream Object previously instantiated.

Example:    

Set fso=CreateObject("Scripting.FileSystemObject")

'Open the file "qtptest.txt" in writing mode.
Set qfile=fso.OpenTextFile("C:\qtptest.txt",2,True)

'write contents to the file into newline
qfile.Writeline  "Welcome to the World of QTP"

'Open the file "qtptest.txt" in reading mode.
Set qfile=fso.OpenTextFile("C:\qtptest.txt",1,True)
Do while qfile.AtEndOfLine <> true
   Msgbox  qfile.Read(1)  
Loop
'Close the files
qfile.Close
'Release the allocated objects
Set qfile=nothing
Set fso=nothing

'Output --> The file will be read word by word.

AtEndOfStream: Indicates whether the file pointer is positioned at the end of a TextStream file.

Syntax: objTso.AtEndOfStream

Arguments:

objTso:      (Required) The name of a TextStream Object previously instantiated.

Example:    

Set fso=CreateObject("Scripting.FileSystemObject")

'Open the file "qtptest.txt" in writing mode.
Set qfile=fso.OpenTextFile("C:\qtptest.txt",2,True)

'write contents to the file into two newlines
qfile.Writeline  "Welcome to the World of QTP"
qfile.Writeline  "the file name is qtptest.txt"

'Open the file "qtptest.txt" in reading mode.
Set qfile=fso.OpenTextFile("C:\qtptest.txt",1,True)

'Read  the entire contents of  previously written file
Do while qfile.AtEndOfStream <> true
   Msgbox  qfile.ReadLine  
Loop
'Close the files
qfile.Close
'Release the allocated objects
Set qfile=nothing
Set fso=nothing

'Output --> The file will be read line line by line. 


Difference between AtEndOfStream and AtEndOfLine....?

AtEndOfStream- Returns true if the file pointer is at the end of a TextStream file, false it it is not.

AtEndOfLine-   Returns true if the file pinter is at the end of a line in a file, false if it is not.


No comments:

Post a Comment

Note: only a member of this blog may post a comment.