Escolar Documentos
Profissional Documentos
Cultura Documentos
com
Visit:
www.gcreddy.com
for QTP Info
The File System Object (FSO) model provides an object-based tool for working with
folders and files. It allows us to use the familiar object. method syntax with a rich set
of properties, methods, and events to process folders and files. We can also employ
the traditional Visual Basic statements and commands.
The FSO model gives our application the ability to create, alter, move, and delete
folders, or to determine if and where particular folders exist. It also enables us to get
information about folders, such as their names and the date they were created or
last modified.
The FSO model makes processing files much easier as well. When processing files,
our primary goal is to store data in an efficient, easy-to-access format. We need to
be able to create files, insert and change the data, and output (read) the data.
Although we can store data in a database, doing so adds a significant amount of
overhead to our application. We may not want to have such overhead, or our data
access requirements may not call for the extra functionality associated with a full-
featured database. In this case, storing our data in a text file or binary file is the
most efficient solution.
The FSO model, contained in the Scripting type library (Scrrun.dll), supports the
creation and manipulation of text files through the TextStream object; however, the
FSO model does not support binary files. To manipulate binary files, use the FileOpen
Function with the Binary keyword.
QTP Training 1
gcreddy.com
Examples
1) Create a Folder
Option Explicit
Dim objFSO, objFolder, strDirectory
strDirectory = "D:\Gcreddy"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.CreateFolder(strDirectory)
2) Delete a Folder
3) Copying Folders
Set oFSO=createobject("Scripting.Filesystemobject")
oFSO.CopyFolder "E:\gcr", "C:\jvr", True
4) Checking weather the folder available or not, if not creating the folder
Option Explicit
Dim objFSO, objFolder, strDirectory
strDirectory = "D:\Gcreddy"
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(strDirectory) Then
Set objFolder = objFSO.GetFolder(strDirectory)
msgbox strDirectory & " already created "
else
Set objFolder = objFSO.CreateFolder(strDirectory)
end if
QTP Training 2
gcreddy.com
8) Checking weather the File is available or not, if not creating the File
strDirectory="E:\"
strFile="Gcreddy.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(strDirectory & strFile) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFile = objFSO.CreateTextFile("E:\Gcreddy.txt")
End if
11) 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:\Gcreddy.txt",1)
myfile.skipline
While myfile.atendofline <> True
x=myfile.readline
s=split (x, ",")
QTP Training 3
gcreddy.com
Set objFSO=createobject("Scripting.filesystemobject")
Set txtFilepath = objFSO.GetFile("E:\gcr.txt")
txtFilepath.Delete()
14) Checking weather the File is available or not, if available delete the File
strDirectory="E:\"
strFile="gcr.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:\Gcreddy1.txt"
f2="e:\Gcreddy2.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()
QTP Training 4
gcreddy.com
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:\gcr.txt"
sString="gcreddy"
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
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H0001
strPathtoTextFile = "C:\Databases\"
Do Until objRecordset.EOF
Wscript.Echo "Name: " & objRecordset.Fields.Item("Name")
Wscript.Echo "Department: " & _
objRecordset.Fields.Item("Department")
Wscript.Echo "Extension: " & objRecordset.Fields.Item("Extension")
objRecordset.MoveNext
QTP Training 5
gcreddy.com
Loop
Const ForReading = 1
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
QTP Training 6