QTP:- Working with Flat Files examples
Working with Flat Files
a) Creating a Flat File
d) Write content to a file?
You can use Write() or WriteLine() Methods to write text into a file. The difference between the Write() and WriteLine() Method is that the latter automatically inserts a new line character while the former doesn’t insert a new line character.
For example:
e) delete content?
Use DeleteFile() method to delete a file from a particular location
Foe Example:
a) Reading Data character by character from a Flat File
c) Reading data from a flat file and using in data driven testing
i) Delete a text file
k) copy a file and/or folder
m) Read a Text File into an Array
Set objFSO =
CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("E:\ScriptLog.txt")
objFile.WriteLine "hello"
objFile.WriteLine "this is sample data"
objFile.Close
Set objFSO =nothing
b) Opening a fileSet objFile = objFSO.CreateTextFile("E:\ScriptLog.txt")
objFile.WriteLine "hello"
objFile.WriteLine "this is sample data"
objFile.Close
Set objFSO =nothing
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.
c) Read content from a file?//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.
Use ReadLine() method
For 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
For 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
d) Write content to a file?
You can use Write() or WriteLine() Methods to write text into a file. The difference between the Write() and WriteLine() Method is that the latter automatically inserts a new line character while the former doesn’t insert a new line character.
For example:
Set file= fso.OpenTextFile("C:\file_location",
ForWriting, True) //2nd argument should always be "ForWriting" in
order to write contents to a file
file.Write("This is a place to get all your qtp")
file.Write("questions and answers solved.")
//Output will be:
This is a place to get all your qtp questions and answers solved.
while
file.WriteLine("This is a place to get all your qtp")
file.Write("questions and answers solved.")
//Output will be:
This is a place to get all your qtp
questions and answers solved.
Function AppendToFile(sFilename, sLine)
Const ForAppending = 8
If sFilename = "" Then
sFilename = Environment("SystemTempDir") & "\QTDebug.txt"
End If
Set f = OpenFile(sFilename, ForAppending, True)
f.WriteLine sLine
f.Close
End Function
' Writes a line to a file.
' Destroys the current content of the file .
' Example of usage:
' WriteToFile "d: emp\beenhere.txt", Now
Function WriteToFile(sFilename, sLine)
Const ForWriting = 2
If sFilename = "" Then
sFilename = Environment("SystemTempDir") & "\QTDebug.txt"
End If
Set f = OpenFile(sFilename, ForWriting, True)
f.WriteLine sLine
f.Close
Sub OpenTextFileTest
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("c:\testfile.txt", ForWriting, True)
f.Write "Hello world!"
f.Close
End Sub
OpenTextFile
ForReading 1 Open a file for reading only. You can't write to this file.
ForWriting 2 Open a file for writing.
ForAppending 8 Open a file and write to the end of the file.
Function TextStreamTest
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim fso, f, ts
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CreateTextFile "test1.txt" ' Create a file.
Set f = fso.GetFile("test1.txt")
Set ts = f.OpenAsTextStream(ForWriting, TristateUseDefault)
ts.Write "Hello World"
ts.Close
Set ts = f.OpenAsTextStream(ForReading, TristateUseDefault)
TextStreamTest = ts.ReadLine
ts.Close
End Function
OpenAsTextStream
ForReading 1 Open a file for reading only. You can't write to this file.
ForWriting 2 Open a file for writing. If a file with the same name exists, its previous contents are overwritten.
ForAppending 8 Open a file and write to the end of the file.
file.Write("This is a place to get all your qtp")
file.Write("questions and answers solved.")
//Output will be:
This is a place to get all your qtp questions and answers solved.
while
file.WriteLine("This is a place to get all your qtp")
file.Write("questions and answers solved.")
//Output will be:
This is a place to get all your qtp
questions and answers solved.
Function AppendToFile(sFilename, sLine)
Const ForAppending = 8
If sFilename = "" Then
sFilename = Environment("SystemTempDir") & "\QTDebug.txt"
End If
Set f = OpenFile(sFilename, ForAppending, True)
f.WriteLine sLine
f.Close
End Function
' Writes a line to a file.
' Destroys the current content of the file .
' Example of usage:
' WriteToFile "d: emp\beenhere.txt", Now
Function WriteToFile(sFilename, sLine)
Const ForWriting = 2
If sFilename = "" Then
sFilename = Environment("SystemTempDir") & "\QTDebug.txt"
End If
Set f = OpenFile(sFilename, ForWriting, True)
f.WriteLine sLine
f.Close
Sub OpenTextFileTest
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("c:\testfile.txt", ForWriting, True)
f.Write "Hello world!"
f.Close
End Sub
OpenTextFile
ForReading 1 Open a file for reading only. You can't write to this file.
ForWriting 2 Open a file for writing.
ForAppending 8 Open a file and write to the end of the file.
Function TextStreamTest
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim fso, f, ts
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CreateTextFile "test1.txt" ' Create a file.
Set f = fso.GetFile("test1.txt")
Set ts = f.OpenAsTextStream(ForWriting, TristateUseDefault)
ts.Write "Hello World"
ts.Close
Set ts = f.OpenAsTextStream(ForReading, TristateUseDefault)
TextStreamTest = ts.ReadLine
ts.Close
End Function
OpenAsTextStream
ForReading 1 Open a file for reading only. You can't write to this file.
ForWriting 2 Open a file for writing. If a file with the same name exists, its previous contents are overwritten.
ForAppending 8 Open a file and write to the end of the file.
e) delete content?
Use DeleteFile() method to delete a file from a particular location
Foe Example:
file_location =
"C:\file_location"
Set fso = CreateObject(“Scripting.FileSystemObject”)
fso.DeleteFile(file_location)
f) Checking weather the File is available or not, if not creating the FileSet fso = CreateObject(“Scripting.FileSystemObject”)
fso.DeleteFile(file_location)
strDirectory="E:\"
strFile="Scripting.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(strDirectory & strFile) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFile = objFSO.CreateTextFile("E:\ScriptLog.txt")
End if
strFile="Scripting.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(strDirectory & strFile) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFile = objFSO.CreateTextFile("E:\ScriptLog.txt")
End if
a) Reading Data character by character from a Flat File
Set objFSO =
CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("E:\KKK.txt", 1)
Do Until objFile.AtEndOfStream
strCharacters = objFile.Read(1)
msgbox strCharacters
Loop
Or
While not objFile.atendofstream
a= objFile.readline()
print a
Wend
g) Reading Data line by line from a Flat FileSet objFile = objFSO.OpenTextFile("E:\KKK.txt", 1)
Do Until objFile.AtEndOfStream
strCharacters = objFile.Read(1)
msgbox strCharacters
Loop
Or
While not objFile.atendofstream
a= objFile.readline()
print a
Wend
Set objFSO =
CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("E:\KKK.txt", 1)
Do Until objFile.AtEndOfStream
strCharacters = objFile.Readline
msgbox strCharacters
Loop
Set objFile = objFSO.OpenTextFile("E:\KKK.txt", 1)
Do Until objFile.AtEndOfStream
strCharacters = objFile.Readline
msgbox strCharacters
Loop
c) Reading data from a flat file and using in data driven testing
Dim fso,myfile
Set fso=createobject("scripting.filesystemobject")
Set myfile= fso.opentextfile ("F:\KKK.txt",1)
myfile.skipline
While myfile.atendofline <> True
x=myfile.readline
s=split (x, ",")
SystemUtil.Run "C:\Program Files\Mercury Interactive\QuickTest Professional\samples\flight\app\flight4a.exe","","C:\Program Files\Mercury Interactive\QuickTest Professional\samples\flight\app\","open"
Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set s(0)
Dialog("Login").WinEdit("Password:").SetSecure s(1)
Dialog("Login").WinButton("OK").Click
Window("Flight Reservation").Close
Wend
Or
Dim objFso, myFile, myLine, myField
Set objFso=CreateObject("Scripting.FileSystemObject")
Set myFile=objFso.OpenTextFile("C:\Documents and Settings\KKK.KKKC-9A12FBD3D9\Desktop\vindod.txt",1) '1 for Read, 2-Write & 8-Append
myFile.SkipLine
Do Until myFile.AtEndOfStream
myLine=myFile.ReadLine
myField=Split(myLine,",")
SystemUtil.Run "C:\Program Files\HP\QuickTest Professional\samples\flight\app\flight4a.exe"
Dialog("text:=Login").Activate
Dialog("text:=Login").WinEdit("attached text:=Agent Name:").Set myField(0)
Dialog("text:=Login").WinEdit("attached text:=Password:").Set myField(1)
wait 2
Dialog("text:=Login").WinButton("text:=OK").Click
Window("text:=Flight Reservation").Close
Loop
myFile.Close
Set objFso=Nothing
h) Writing data to a text file Set fso=createobject("scripting.filesystemobject")
Set myfile= fso.opentextfile ("F:\KKK.txt",1)
myfile.skipline
While myfile.atendofline <> True
x=myfile.readline
s=split (x, ",")
SystemUtil.Run "C:\Program Files\Mercury Interactive\QuickTest Professional\samples\flight\app\flight4a.exe","","C:\Program Files\Mercury Interactive\QuickTest Professional\samples\flight\app\","open"
Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set s(0)
Dialog("Login").WinEdit("Password:").SetSecure s(1)
Dialog("Login").WinButton("OK").Click
Window("Flight Reservation").Close
Wend
Or
Dim objFso, myFile, myLine, myField
Set objFso=CreateObject("Scripting.FileSystemObject")
Set myFile=objFso.OpenTextFile("C:\Documents and Settings\KKK.KKKC-9A12FBD3D9\Desktop\vindod.txt",1) '1 for Read, 2-Write & 8-Append
myFile.SkipLine
Do Until myFile.AtEndOfStream
myLine=myFile.ReadLine
myField=Split(myLine,",")
SystemUtil.Run "C:\Program Files\HP\QuickTest Professional\samples\flight\app\flight4a.exe"
Dialog("text:=Login").Activate
Dialog("text:=Login").WinEdit("attached text:=Agent Name:").Set myField(0)
Dialog("text:=Login").WinEdit("attached text:=Password:").Set myField(1)
wait 2
Dialog("text:=Login").WinButton("text:=OK").Click
Window("text:=Flight Reservation").Close
Loop
myFile.Close
Set objFso=Nothing
Dim Stuff, myFSO, WriteStuff,
dateStamp
dateStamp = Date()
Stuff = "I am Preparing this script: " &dateStamp
Set myFSO = CreateObject("Scripting.FileSystemObject")
Set WriteStuff = myFSO.OpenTextFile("e:\KKK.txt", 8, True)
WriteStuff.WriteLine(Stuff)
WriteStuff.Close
SET WriteStuff = NOTHING
SET myFSO = NOTHING
Or
Set x=createobject("Scripting.FileSystemObject")
Set z= x.OpenTextFile("E:\Documents and Settings\Sudha\Desktop\lord.txt",2)
z.writeline("One")
z.writeline("two")
z.writeline("three")
z.writeline("four")
z.writeline("five")
dateStamp = Date()
Stuff = "I am Preparing this script: " &dateStamp
Set myFSO = CreateObject("Scripting.FileSystemObject")
Set WriteStuff = myFSO.OpenTextFile("e:\KKK.txt", 8, True)
WriteStuff.WriteLine(Stuff)
WriteStuff.Close
SET WriteStuff = NOTHING
SET myFSO = NOTHING
Or
Set x=createobject("Scripting.FileSystemObject")
Set z= x.OpenTextFile("E:\Documents and Settings\Sudha\Desktop\lord.txt",2)
z.writeline("One")
z.writeline("two")
z.writeline("three")
z.writeline("four")
z.writeline("five")
i) Delete a text file
Set
objFSO=createobject("Scripting.filesystemobject")
Set txtFilepath = objFSO.GetFile("E:\KKK.txt")
txtFilepath.Delete()
j) Checking weather the File is available or not, if available delete the FileSet txtFilepath = objFSO.GetFile("E:\KKK.txt")
txtFilepath.Delete()
strDirectory="E:\"
strFile="KKK.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(strDirectory & strFile) Then
Set objFile = objFSO.Getfile(strDirectory & strFile)
objFile.delete ()
End if
k) Comparing two text filesstrFile="KKK.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(strDirectory & strFile) Then
Set objFile = objFSO.Getfile(strDirectory & strFile)
objFile.delete ()
End if
Dim f1, f2
f1="e:\KKK1.txt"
f2="e:\KKK2.txt"
Public Function CompareFiles (FilePath1, FilePath2)
Dim FS, File1, File2
Set FS = CreateObject("Scripting.FileSystemObject")
If FS.GetFile(FilePath1).Size <> FS.GetFile(FilePath2).Size Then
CompareFiles = True
Exit Function
End If
Set File1 = FS.GetFile(FilePath1).OpenAsTextStream(1, 0)
Set File2 = FS.GetFile(FilePath2).OpenAsTextStream(1, 0)
CompareFiles = False
Do While File1.AtEndOfStream = False
Str1 = File1.Read
Str2 = File2.Read
CompareFiles = StrComp(Str1, Str2, 0)
If CompareFiles <> 0 Then
CompareFiles = True
Exit Do
End If
Loop
File1.Close()
File2.Close()
End Function
Call Comparefiles(f1,f2)
If CompareFiles(f1, f2) = False Then
MsgBox "Files are identical."
Else
MsgBox "Files are different."
End If
j) Counting the number of times a word appears in a filef1="e:\KKK1.txt"
f2="e:\KKK2.txt"
Public Function CompareFiles (FilePath1, FilePath2)
Dim FS, File1, File2
Set FS = CreateObject("Scripting.FileSystemObject")
If FS.GetFile(FilePath1).Size <> FS.GetFile(FilePath2).Size Then
CompareFiles = True
Exit Function
End If
Set File1 = FS.GetFile(FilePath1).OpenAsTextStream(1, 0)
Set File2 = FS.GetFile(FilePath2).OpenAsTextStream(1, 0)
CompareFiles = False
Do While File1.AtEndOfStream = False
Str1 = File1.Read
Str2 = File2.Read
CompareFiles = StrComp(Str1, Str2, 0)
If CompareFiles <> 0 Then
CompareFiles = True
Exit Do
End If
Loop
File1.Close()
File2.Close()
End Function
Call Comparefiles(f1,f2)
If CompareFiles(f1, f2) = False Then
MsgBox "Files are identical."
Else
MsgBox "Files are different."
End If
sFileName="E:\KKK.txt"
sString="kalyan"
Const FOR_READING = 1
Dim oFso, oTxtFile, sReadTxt, oRegEx, oMatches
Set oFso = CreateObject("Scripting.FileSystemObject")
Set oTxtFile = oFso.OpenTextFile(sFileName, FOR_READING)
sReadTxt = oTxtFile.ReadAll
Set oRegEx = New RegExp
oRegEx.Pattern = sString
oRegEx.IgnoreCase = bIgnoreCase
oRegEx.Global = True
Set oMatches = oRegEx.Execute(sReadTxt)
MatchesFound = oMatches.Count
Set oTxtFile = Nothing : Set oFso = Nothing : Set oRegEx = Nothing
msgbox MatchesFound
k) shows date created a filesString="kalyan"
Const FOR_READING = 1
Dim oFso, oTxtFile, sReadTxt, oRegEx, oMatches
Set oFso = CreateObject("Scripting.FileSystemObject")
Set oTxtFile = oFso.OpenTextFile(sFileName, FOR_READING)
sReadTxt = oTxtFile.ReadAll
Set oRegEx = New RegExp
oRegEx.Pattern = sString
oRegEx.IgnoreCase = bIgnoreCase
oRegEx.Global = True
Set oMatches = oRegEx.Execute(sReadTxt)
MatchesFound = oMatches.Count
Set oTxtFile = Nothing : Set oFso = Nothing : Set oRegEx = Nothing
msgbox MatchesFound
Function
ShowDateCreated(filespec)
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(filespec)
ShowDateCreated = f.DateCreated
End Function
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(filespec)
ShowDateCreated = f.DateCreated
End Function
k) copy a file and/or folder
Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile("c:\testfile.txt", True)
MyFile.WriteLine("This is a test.")
MyFile.Close
Set MyFile = fso.GetFile("c:\testfile.txt")
MyFile.Copy ("c:\windows\desktop\test2.txt")
Copies a specified file or folder from one location to another.
object.Copy( destination[, overwrite] );
Arguments
object
Required. Always the name of a File or Folder object.
destination
Required. Destination where the file or folder is to be copied. Wildcard characters are not allowed.
overwrite
Optional. Boolean value that is True (default) if existing files or folders are to be overwritten; False if they are not.
The results of the Copy method on a File or Folder are identical to operations performed using FileSystemObject.CopyFile or FileSystemObject.CopyFolder where the file or folder referred to by object is passed as an argument. You should note, however, that the alternative methods are capable of copying multiple files or folders.
FileSystemObject.CopyFile "c:\mydocuments\letters\*.doc", "c:\tempfolder\"
FileSystemObject.CopyFolder "c:\mydocuments\letters\*", "c:\tempfolder\"
14. how to read data from one notepad to wrte another notepad file
Set x=createobject("Scripting.FileSystemObject")
Set y= x.OpenTextFile("E:\Documents and Settings\Sudha\Desktop\god.txt",1)
Set z= x.OpenTextFile("E:\Documents and Settings\Sudha\Desktop\lord.txt",2)
While not y.atendofstream
a=y.readline()
z.writeline(a)
print a
Wend
i) How to replace from one particular notepad to another notepadSet fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile("c:\testfile.txt", True)
MyFile.WriteLine("This is a test.")
MyFile.Close
Set MyFile = fso.GetFile("c:\testfile.txt")
MyFile.Copy ("c:\windows\desktop\test2.txt")
Copies a specified file or folder from one location to another.
object.Copy( destination[, overwrite] );
Arguments
object
Required. Always the name of a File or Folder object.
destination
Required. Destination where the file or folder is to be copied. Wildcard characters are not allowed.
overwrite
Optional. Boolean value that is True (default) if existing files or folders are to be overwritten; False if they are not.
The results of the Copy method on a File or Folder are identical to operations performed using FileSystemObject.CopyFile or FileSystemObject.CopyFolder where the file or folder referred to by object is passed as an argument. You should note, however, that the alternative methods are capable of copying multiple files or folders.
FileSystemObject.CopyFile "c:\mydocuments\letters\*.doc", "c:\tempfolder\"
FileSystemObject.CopyFolder "c:\mydocuments\letters\*", "c:\tempfolder\"
14. how to read data from one notepad to wrte another notepad file
Set x=createobject("Scripting.FileSystemObject")
Set y= x.OpenTextFile("E:\Documents and Settings\Sudha\Desktop\god.txt",1)
Set z= x.OpenTextFile("E:\Documents and Settings\Sudha\Desktop\lord.txt",2)
While not y.atendofstream
a=y.readline()
z.writeline(a)
print a
Wend
Set x=createobject("Scripting.FileSystemObject")
Set y= x.OpenTextFile("E:\Documents and Settings\Sudha\Desktop\god.txt",1)
Set z= x.OpenTextFile("E:\Documents and Settings\Sudha\Desktop\lord.txt",2)
While not y.atendofstream
a=y.readline()
If instr(1,a,"99402") Then
a=replace(a,99,88)
end if
z.writeline(a)
print a
Wend
Set y= x.OpenTextFile("E:\Documents and Settings\Sudha\Desktop\god.txt",1)
Set z= x.OpenTextFile("E:\Documents and Settings\Sudha\Desktop\lord.txt",2)
While not y.atendofstream
a=y.readline()
If instr(1,a,"99402") Then
a=replace(a,99,88)
end if
z.writeline(a)
print a
Wend
m) Read a Text File into an Array
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
("e:\kalyan.txt", ForReading)
Do Until objTextFile.AtEndOfStream
strNextLine = objTextFile.Readline
arrServiceList = Split(strNextLine , ",")
Wscript.Echo "Server name: " & arrServiceList(0)
For i = 1 to Ubound(arrServiceList)
Wscript.Echo "Service: " & arrServiceList(i)
Next
Loop
n) Calculate size of a Text fileSet objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
("e:\kalyan.txt", ForReading)
Do Until objTextFile.AtEndOfStream
strNextLine = objTextFile.Readline
arrServiceList = Split(strNextLine , ",")
Wscript.Echo "Server name: " & arrServiceList(0)
For i = 1 to Ubound(arrServiceList)
Wscript.Echo "Service: " & arrServiceList(i)
Next
Loop
Dim objFso, File1,File2
File1="C:\Documents and Settings\1 RIGHATWAY\Desktop\xyz.txt"
Set objFso=CreateObject("
Scripting.FileSystemObject")
x= objFso.GetFile(File1).Size
Msgbox x& " Bytes"
File1="C:\Documents and Settings\1 RIGHATWAY\Desktop\xyz.txt"
Set objFso=CreateObject("
Scripting.FileSystemObject")
x= objFso.GetFile(File1).Size
Msgbox x& " Bytes"
o) Delete a Text file if it is an
empty file
Dim objFso, FilePath
FilePath="C:\Documents and
Settings\KKKeddy\Desktop\abc.txt"
Set
objFso=CreateObject("Scripting.FileSystemObject")
FileSize=objFso.GetFile(FilePath).Size
'Msgbox FileSize
If FileSize=0 Then
objFso.DeleteFile(FilePath)
End If
Set objFso=Nothing
Many a times you may need to
interact with text files using QTP. Interaction can be(but not limited to) in
the form of reading input from a file, writing output to a file. This post
describe in detail "File handling using QTP".
We use FSO object to do this.
What is FSO?
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.
set
fso=CreateObject("Scripting.FileSystemObject")
Set
myfile=fso.GetFile("D:\NewFolder\content2.txt")
Createdon=myfile.Attributes
Msgbox Createdon
Set
myfile=fso.CreateTextFile("D:\NewFolder\content2.txt", true)
myfile.writeLine(Createdon)
myfile.close
set
fso=CreateObject("Scripting.FileSystemObject")
Set
myfile=fso.GetFile("D:\NewFolder\content2.txt")
Createdon=myfile.DateCreated
Msgbox Createdon
Call Data
set
fso=CreateObject("Scripting.FileSystemObject")
Set
myfile=fso.GetFile("D:\NewFolder\content2.txt")
Createdon=myfile.Attributes
Msgbox Createdon
Call Data
Createdon=myfile.DateLastAccessed
Msgbox Createdon
Call Data
Createdon=myfile.DateLastModified
Msgbox Createdon
Call Data
Createdon=myfile.Drive
Msgbox Createdon
Call Data
Createdon=myfile.Name
Msgbox Createdon
Call Data
Createdon=myfile.ParentFolder
Msgbox Createdon
Call Data
Createdon=myfile.Path
Msgbox Createdon
Call Data
Createdon=myfile.ShortName
Msgbox Createdon
Call Data
Createdon=myfile.ShortPath
Msgbox Createdon
Call Data
Createdon=myfile.Size
Msgbox Createdon
Call Data
Createdon=myfile.Path
Msgbox Createdon
Call Data
Sub Data
Set
myfile=fso.CreateTextFile("D:\NewFolder\content2.txt", true)
myfile=myfile.writeLine(Createdon)
myfile=myfile& NewLine
myfile=myfile& NewLine
End Sub
''=====================================================================================================================================================================
'''Function to check if a string
matches a Regular Expression Pattern
''=====================================================================================================================================================================
Function IsRegExMatch(strText,
regEx_Pattern, blnMatchCompleteString)
Dim oRegExp, retVal
' Create regular expression object.
Set oRegExp = New RegExp
'Set pattern.
oRegExp.Pattern = regEx_Pattern
'Ignore case
oRegExp.IgnoreCase = True
'Match complete string or not
oRegExp.Global = blnMatchCompleteString
'Test the regular expression
IsRegExMatch = oRegExp.Test(strText)
Set oRegExp = Nothing
End Function
''=====================================================================================================================================================================
'''Function to Match a Regular
Expression and Replace with a particular text if a string matches a regular
expression pattern.
''=====================================================================================================================================================================
Function
RegExMatchandReplace(strText,regEx_Pattern,strReplacementText,blnMatchCompleteString)
Dim oRegExp, retVal,strReplacedText
' Create regular expression object.
Set oRegExp = New RegExp
'Set pattern.
oRegExp.Pattern = regEx_Pattern
'Ignore case
oRegExp.IgnoreCase = True
'Match complete string or not
oRegExp.Global = blnMatchCompleteString
'Test the regular expression
RegExMatch = oRegExp.Test(strText)
If RegExMatch = True Then
strReplacedText = oRegExp.Replace(strText,strReplacementText)
End If
RegExMatchandReplace = strReplacedText
Set
oRegExp = Nothing
End Function
''=====================================================================================================================================================================
'''Function to Get Total Number
of Matches of a Regular Expression Pattern in a particular string
''=====================================================================================================================================================================
Function GetRegExMatchCount(strText,
regEx_Pattern, blnMatchCompleteString)
Dim oRegExp, retVal
'Create regular expression object.
Set oRegExp = New RegExp
'Set pattern.
oRegExp.Pattern = regEx_Pattern
'Ignore case
oRegExp.IgnoreCase = True
'Match complete string or not
oRegExp.Global = blnMatchCompleteString
'Test the regular expression
RegExpMatch = oRegExp.Test(strText)
If RegExpMatch Then
Set oRegExMatchCount = oRegExp.Execute(strText)
GetRegExMatchCount = oRegExMatchCount.Count
End If
Set oRegExp = Nothing
End Function
''=====================================================================================================================================================================
'''Function to Trim white space
characters from end of a string
''=====================================================================================================================================================================
Public Function RTrimW(ByVal Text)
'String pattern ending with carriage return,
tab, new line or a space character
RTrimPattern = "[\s]*$"
Dim oRegExp
Set oRegExp = New RegExp
oRegExp.Pattern =RTrimPattern
oRegExp.Global = False
RTrimW = oRegExp.Replace(Text,"")
Set oRegExp = Nothing
End Function
''=====================================================================================================================================================================
'''Function to Trim white space
characters from start of a string
''=====================================================================================================================================================================
Public Function LTrimW(ByVal Text)
'String pattern ending with carriage return,
tab, new line or a space character
LTrimPattern = "^[\s]*"
Dim oRegExp
Set oRegExp = New RegExp
oRegExp.Pattern = LTrimPattern
oRegExp.Global = False
LTrimW = oRegExp.Replace(Text,"")
Set oRegExp = Nothing
End Function
''=====================================================================================================================================================================
''Function to Trim white space
characters from both start and end of a string
''=====================================================================================================================================================================
Public Function TrimW(ByVal Text)
TrimW = LTrimW(RTrimW(Text))
End Function
Comments
Post a Comment