Create text file & adding a line to it :
Dim f1, fso
Set fso= createobject ("scripting.filesystemobject")
Set f1= fso.createtextfile ("D:\QTP\QTP_FSO\file2.txt", true)
f1.writeline "Hello This is new file"
Open a Text file & read line :
Dim f1, fso,ReadLineTextFile,
forreading=1
Set fso= createobject ("scripting.filesystemobject")
Set f1= fso.opentextfile ("D:\QTP\QTP_FSO\file2.txt" ,ForReading)
ReadLineTextFile = f1.ReadLine
msgbox ReadLineTextFile
Check if file already exist :
Dim f1, fso,ReadLineTextFile,file1
forreading=1
Set fso= createobject ("scripting.filesystemobject")
If fso.FileExists("D:\QTP\QTP_FSO\file2.txt") Then
msgbox "File Already Exists"
else
msgbox "File does not exist"
Set f1= fso.createtextfile ("D:\QTP\QTP_FSO\file2.txt", true)
f1.writeline "Hello This is new file"
End If
Set f1= fso.opentextfile ("D:\QTP\QTP_FSO\file2.txt" ,ForReading)
ReadLineTextFile = f1.ReadLine
msgbox ReadLineTextFile
f1.Close
set file1= fso.GetFile("D:\QTP\QTP_FSO\file2.txt")
file1.delete
Appending a file :
Dim f1, fso,ReadLineTextFile,Readfile
forappending=8,forReading= 1
Set fso= createobject ("scripting.filesystemobject")
If fso.FileExists("D:\QTP\QTP_FSO\file2.txt") Then
msgbox "File Already Exists"
else
msgbox "File does not exist"
Set f1= fso.createtextfile ("D:\QTP\QTP_FSO\file2.txt",false)
f1.writeline "Hello This is new file"
end if
Set f1= fso.OpenTextFile("D:\QTP\QTP_FSO\file2.txt", 8)
f1.WriteLine" This line is appended as second"
Set f1= fso.OpenTextFile("D:\QTP\QTP_FSO\file2.txt",1)
ReadFile = f1.readall
msgbox ReadFile
f1.close
Search a Specific String in a Text File :
Dim fso, f1, MyPos, SearchString, SearchChar
Set fso = CreateObject("Scripting.FileSystemObject")
Set f1 = fso.OpenTextFile("D:\QTP\QTP_FSO\file2.txt", 1,True)
Do while f1.AtEndofStream <> True
SearchString = f1.Readall
SearchChar = "Hello"
msgbox SearchString
msgbox SearchChar
MyPos = Instr( 1, SearchString, SearchChar, 1)
If MyPos >=0 Then
msgbox "TextFound"
Exit Do
else
msgbox "Text Not Found"
End If
Loop
msgbox MyPos
Dim f1, fso
Set fso= createobject ("scripting.filesystemobject")
Set f1= fso.createtextfile ("D:\QTP\QTP_FSO\file2.txt", true)
f1.writeline "Hello This is new file"
Open a Text file & read line :
Dim f1, fso,ReadLineTextFile,
forreading=1
Set fso= createobject ("scripting.filesystemobject")
Set f1= fso.opentextfile ("D:\QTP\QTP_FSO\file2.txt" ,ForReading)
ReadLineTextFile = f1.ReadLine
msgbox ReadLineTextFile
Check if file already exist :
Dim f1, fso,ReadLineTextFile,file1
forreading=1
Set fso= createobject ("scripting.filesystemobject")
If fso.FileExists("D:\QTP\QTP_FSO\file2.txt") Then
msgbox "File Already Exists"
else
msgbox "File does not exist"
Set f1= fso.createtextfile ("D:\QTP\QTP_FSO\file2.txt", true)
f1.writeline "Hello This is new file"
End If
Set f1= fso.opentextfile ("D:\QTP\QTP_FSO\file2.txt" ,ForReading)
ReadLineTextFile = f1.ReadLine
msgbox ReadLineTextFile
f1.Close
set file1= fso.GetFile("D:\QTP\QTP_FSO\file2.txt")
file1.delete
Appending a file :
Dim f1, fso,ReadLineTextFile,Readfile
forappending=8,forReading= 1
Set fso= createobject ("scripting.filesystemobject")
If fso.FileExists("D:\QTP\QTP_FSO\file2.txt") Then
msgbox "File Already Exists"
else
msgbox "File does not exist"
Set f1= fso.createtextfile ("D:\QTP\QTP_FSO\file2.txt",false)
f1.writeline "Hello This is new file"
end if
Set f1= fso.OpenTextFile("D:\QTP\QTP_FSO\file2.txt", 8)
f1.WriteLine" This line is appended as second"
Set f1= fso.OpenTextFile("D:\QTP\QTP_FSO\file2.txt",1)
ReadFile = f1.readall
msgbox ReadFile
f1.close
Search a Specific String in a Text File :
Dim fso, f1, MyPos, SearchString, SearchChar
Set fso = CreateObject("Scripting.FileSystemObject")
Set f1 = fso.OpenTextFile("D:\QTP\QTP_FSO\file2.txt", 1,True)
Do while f1.AtEndofStream <> True
SearchString = f1.Readall
SearchChar = "Hello"
msgbox SearchString
msgbox SearchChar
MyPos = Instr( 1, SearchString, SearchChar, 1)
If MyPos >=0 Then
msgbox "TextFound"
Exit Do
else
msgbox "Text Not Found"
End If
Loop
msgbox MyPos
Hi Neera,
ReplyDeleteThanks for the Detailed explanation about Working with files .
QTPbook