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, 28 October 2019

Text Files in UFT

FSO stands for File System Object. This is used to support text file creation and manipulation through the TextStream object and is contained in the Scripting type library (Scrrun.dll).The FSO Object Model has a rich set of properties, methods and events to process folders and files.

Create a File :

First we want create a FSO object using CreateObject and then create a text file using CreateTextFile.



Dim fso, file, file_location
file_location = “C:file_location”
Set fso = CreateObject(“Scripting.FileSystemObject”)

'True–> file is to be overwritten if it already exists else false
Set file = fso.CreateTextFile(file_location, True)  

Open a file

Set file= fso.OpenTextFile(“C:file_location”, ForWriting, True)

2nd argument can be ForReading, ForWriting, ForAppending
3rd argument is “True” if new file has to be created if the specified file doesn’t exist else false, blank signify false.

Read content from a file :

Using  ReadLine() method, we can read the content of the file.


Example:

Set file= fso.OpenTextFile(“C:file_location”, ForReading, True


'2nd argument should always be “ForReading” in order to read contents from a file
Do while file.AtEndofStream <> True
     data = file.ReadLine()
      msgbox data
Loop


Find the given string  “UFT”  in  line from the text file.

Option explicit
Dim fso,fo,l
Set fso=createobject("scripting.filesystemobject")
Set fo=fso.opentextfile("D:/Sample.txt",1,false)
While fo.atendofstream <> true
        l=fo.readline
        if instr(l,"UFT"then
           print(l)
        end if
wend
fo.close
set fo=nothing
set fso=nothing

Readline ( )-  We can use this method to read current line from file and move control to next line.

Syntax:-  fo.readline( )

Readall( ):- We can use this method to read all lines in the file at a time.

Syntax:-  fo.readall( )

Read( ):-  We can use this method to read specify no:of characters from current line.

Ex:- My name is khan


x=read(4)

Print(x)

o/p: My n


To display the “hi” string lines from the Given test file. ... ?

Option explicit
Dim fso,fo,l
Set fso=createobject("scripting.filesystemobject")

Set fo=fso.opentextfile("D:/Sample.txt",1,false)
While fo.atendofstream <> true
        l=fo.readline
        if instr(l,"hi"then
           print(l)
        end if
wend

fo.close
set fo=nothing
set fso=nothing


OutPut:-
hi how are you.
hey man hi
hey raju hi

To display existing lines in specified text file....?

Option explicit
Dim fso,fo,l
Set fso=createobject("scripting.filesystemobject")
Set fo=fso.opentextfile("D:/Sample.txt")
While fo.atendofstream<>true

       l=fo.readline
          print(l)
 wend
fo.close

set fo=nothing
set fso=nothing


Output:
Hi How are you.
What you doing.
Where are you.

Find the no:of lines in given text file....?

Option explicit
Dim fso,fo,l
Set fso=createobject("scripting.filesystemobject")
Set fo=fso.opentextfile("D:/Sample.txt",1,false)
l=0
While fo.atendofstream<>true
           fo.readline
           l=l+1
wend
Print("no:of lines in the file"&l)
fo.close
set fo=nothing
set fso=nothing


Output:
no:of lines in the file 3

Copy one file text into another file.....?


Option explicit
Dim fso,fo,l,fo1,fo2
Set fso=createobject("scripting.filesystemobject")
Set fo1=fso.opentextfile("D:\Sample.txt",1,false)
Set fo2=fso.opentextfile("D:\Sample1.txt",2,true)

While fo1.atendofstream<>true
      l=fo1.readline
      fo2.writeline(l)
wend
fo1.close

fo2.close
set fo1=nothing
set fo2=nothing
set fso=nothing


Copy numbers in file1 into file2....?

Option explicit
Dim fso,fo,l,fo1,fo2,nums,r,num
Set fso=createobject("scripting.filesystemobject")
Set fo1=fso.opentextfile("D:\Sample.txt",1,false)
Set fo2=fso.opentextfile("D:\Sample1.txt",2,true)

While fo1.atendofstream<>true
      l=fo1.readline
      set r=new regexp
      r.pattern="[0-9]+"
      r.global=true
      set nums=r.execute(l)
      for each num in nums
          fo2.writeline(num.value)
      next
      set nums=nothing
      set r=nothing
wend

fo1.close
fo2.close
set fo1=nothing
set fo2=nothing
set fso=nothing


Copy alphanumeric only from file1 to file2.....?

Option explicit
Dim fso,fo,l,fo1,fo2,nums,r,num
Set fso=createobject("scripting.filesystemobject")
Set fo1=fso.opentextfile("D:\Sample.txt",1,false)
Set fo2=fso.opentextfile("D:\Sample1.txt",2,true)

While fo1.atendofstream<>true
      l=fo1.readline
      set r=new regexp
      r.pattern="[0-9]+[a-z]"
      r.global=true
      set nums=r.execute(l)
      for each num in nums
          fo2.writeline(num.value)
      next
      set nums=nothing
      set r=nothing
wend

fo1.close
fo2.close
set fo1=nothing


set fo2=nothing
set fso=nothing


Copy dates in file1 into file2. Here date is “mm/dd/yy” ...?

Option explicit
Dim fso,fo,l,fo1,fo2,nums,r,num
Set fso=createobject("scripting.filesystemobject")
Set fo1=fso.opentextfile("D:\Sample.txt",1,false)
Set fo2=fso.opentextfile("D:\Sample1.txt",2,true)

While fo1.atendofstream<>true
      l=fo1.readline
      set r=new regexp
      r.pattern="(([0][0-9])|([1][0-2]))[/](([0][0-9])|([1][0-9])|([2][0-9])|([3][0-1]))[/][0-9]{2}"
      r.global=true
      set nums=r.execute(l)
      for each num in nums
          fo2.writeline(num.value)
      next
      set nums=nothing
      set r=nothing
wend

fo1.close
fo2.close
set fo1=nothing
set fo2=nothing
set fso=nothing



No comments:

Post a Comment

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