Tuesday, June 18, 2013

Working with File System Object- Part 2

Hey folks,

'Scenario- To create new text file (notepad) and open it. Write something on it using single line, separate lines and with blank lines .
'Read text file by some characters, by line and by whole file.
'close the text file and set object to nothing

Dim fso
'First, create a FileSystemObject object by using the CreateObject method.
Set fso= createobject("Scripting.Filesystemobject")

'Create a non existing file "qtptest.txt "  with overwrite option as True
Set qfile=fso.CreateTextFile("D:\Trupti.txt",True)

'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"

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

'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  characters from the file
Msgbox  qfile.Read(10)                 'Output --> "Welcome to"  will be read

'Read  the entire contents of  priously written file
Msgbox  qfile.ReadAll  'Output --> Displays the entire file.

'Read  the entire contents of  priously written file
Do while qfile.AtEndOfStream <> true
 'Output --> The file will contain the above written content  in  single line.
Msgbox  qfile.ReadLine
Loop

Do while qfile.AtEndOfLine <> true
Msgbox  qfile.Read(1)  'Output --> The file will be read word by word.
Loop

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

Hope, this will helpful.
Thanks,
Trupti

No comments:

Post a Comment