File System:
Its an operating system feature, it allows users to create, modify,
view and delete; drives,folders and files
VB Script is providing an object called scripting.filesystemobject
and some methods for performing file systems operations
File System Object Model:
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.
Examples:
1) Create a folder
Dim fso, strFolder
strFolder="D:\Documents and Settings\gcreddy\Desktop\Dibyalok"
Set fso=createobject("scripting.filesystemobject")
fso.CreateFolder(strFolder)
2) Create a folder
Dim fso, strFolder
strFolder="D:\Documents and Settings\gcreddy\Desktop\Dibyalok"
Set fso=createobject("scripting.filesystemobject")
If fso.FolderExists(strFolder) Then
msgbox "Folder already exists"
else
fso.CreateFolder(strFolder)
End If
3) Data Driven Testing by fetching Test data directly from a Text file.
'*********************************************************
'Test Requirement: Data Driven Testing by Fetching Test data directly from a Text file.
'Pre-requisites:
'vinod.txt (Test Data)
'Test Flow:
'Creating an Automation Object in FileSystem class
'Opening the External Test Data file using the Object
'Read the Data & Split the Data
'Generating the Login Operation
'Pass Parameters
'********************************************************
Dim objFso, myFile, myLine, myField
Set objFso=CreateObject("Scripting.FileSystemObject")
Set myFile=objFso.OpenTextFile("C:\Documents and Settings\gcr.GCRC-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
File System Scripts
Flat File Scripts
File System Scripts
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
Set oFSO = CreateObject("Scripting.FileSystemObject")
oFSO.DeleteFolder("E:\Gcreddy")
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
5) Returning a collection of Disk Drives
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set colDrives = oFSO.Drives
For Each oDrive in colDrives
MsgBox "Drive letter: " & oDrive.DriveLetter
Next
6) Getting available space on a Disk Drive
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oDrive = oFSO.GetDrive("C:")
MsgBox "Available space: " & oDrive.AvailableSpace
Flat File Scripts
Computer File System
It is a feature of the Operating System, used to Create/Modify/view/delete Drives, Folders and Files
OS Distribution
Operating System and Other Utilities
FileSystemObject
VBScript has Provided FileSystemObject to perform file system operations through scripting
Dim objFso
'Creating an Automation Object in File System class, that can be used to perform Operations on Computer File System
Set objFso=CreateObject("scripting.FileSystemObject")
1) Creating a File
Dim objFso
Set objFso=CreateObject("scripting.FileSystemObject")
objFso.CreateTextFile ("E:\Gcreddy.txt")
objFso.CreateTextFile ("E:\Gcreddy.doc")
objFso.CreateTextFile ("E:\Gcreddy.xls")
objFso.CreateTextFile ("E:\Gcreddy.pdf")
Note: We can Create other files also, but they act as Text/Flat Files
Set objFile = objFSO.CreateTextFile("E:\csreddy.txt")
2) 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:\csreddy.txt")
End if
3) Reading Data character by character from a Flat File
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("E:\csreddy.txt", 1)
Do Until objFile.AtEndOfStream
strCharacters = objFile.Read(1)
msgbox strCharacters
Loop
4) Reading Data line by line from a Flat File
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("E:\csreddy.txt", 1)
Do Until objFile.AtEndOfStream
strCharacters = objFile.Readline
msgbox strCharacters
Loop
5) Data Driven Testing by fetching Test data directly from a Text file.
**************************************************
'Test Requirement: Data Driven Testing by Fetching Test data directly from a Text file.
'Pre-requisites:
'gcr.txt (Test Data)
'Test Flow:
'Creating an Automation Object in FileSystem class
'Opening the External Test Data file using the Object
'Read the Data & Split the Data
'Generating the Login Operation
'Pass Parameters
'*************************************************
Dim objFso, myFile, myLine, myField
Set objFso=CreateObject("Scripting.FileSystemObject")
Set myFile=objFso.OpenTextFile("C:\Documents and Settings\gcr.GCRC-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
6) Writing data to a text file
Dim Stuff, myFSO, WriteStuff, dateStamp
dateStamp = Date()
Stuff = "I am Preparing this script: " &dateStamp
Set myFSO = CreateObject("Scripting.FileSystemObject")
Set WriteStuff = myFSO.OpenTextFile("e:\Gcreddy.txt", 8, True)
WriteStuff.WriteLine(Stuff)
WriteStuff.Close
SET WriteStuff = NOTHING
SET myFSO = NOTHING
7) Delete a text file
Set objFSO=createobject("Scripting.filesystemobject")
Set txtFilepath = objFSO.GetFile("E:\gcr.txt")
txtFilepath.Delete()
8) 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
9) Comparing two text files
Dim f1, f2
f1="e:\csreddy1.txt"
f2="e:\csreddy2.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
Its an operating system feature, it allows users to create, modify,
view and delete; drives,folders and files
VB Script is providing an object called scripting.filesystemobject
and some methods for performing file systems operations
File System Object Model:
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.
Examples:
1) Create a folder
Dim fso, strFolder
strFolder="D:\Documents and Settings\gcreddy\Desktop\Dibyalok"
Set fso=createobject("scripting.filesystemobject")
fso.CreateFolder(strFolder)
2) Create a folder
Dim fso, strFolder
strFolder="D:\Documents and Settings\gcreddy\Desktop\Dibyalok"
Set fso=createobject("scripting.filesystemobject")
If fso.FolderExists(strFolder) Then
msgbox "Folder already exists"
else
fso.CreateFolder(strFolder)
End If
3) Data Driven Testing by fetching Test data directly from a Text file.
'*********************************************************
'Test Requirement: Data Driven Testing by Fetching Test data directly from a Text file.
'Pre-requisites:
'vinod.txt (Test Data)
'Test Flow:
'Creating an Automation Object in FileSystem class
'Opening the External Test Data file using the Object
'Read the Data & Split the Data
'Generating the Login Operation
'Pass Parameters
'********************************************************
Dim objFso, myFile, myLine, myField
Set objFso=CreateObject("Scripting.FileSystemObject")
Set myFile=objFso.OpenTextFile("C:\Documents and Settings\gcr.GCRC-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
File System Scripts
Flat File Scripts
File System Scripts
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
Set oFSO = CreateObject("Scripting.FileSystemObject")
oFSO.DeleteFolder("E:\Gcreddy")
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
5) Returning a collection of Disk Drives
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set colDrives = oFSO.Drives
For Each oDrive in colDrives
MsgBox "Drive letter: " & oDrive.DriveLetter
Next
6) Getting available space on a Disk Drive
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oDrive = oFSO.GetDrive("C:")
MsgBox "Available space: " & oDrive.AvailableSpace
Flat File Scripts
Computer File System
It is a feature of the Operating System, used to Create/Modify/view/delete Drives, Folders and Files
OS Distribution
Operating System and Other Utilities
FileSystemObject
VBScript has Provided FileSystemObject to perform file system operations through scripting
Dim objFso
'Creating an Automation Object in File System class, that can be used to perform Operations on Computer File System
Set objFso=CreateObject("scripting.FileSystemObject")
1) Creating a File
Dim objFso
Set objFso=CreateObject("scripting.FileSystemObject")
objFso.CreateTextFile ("E:\Gcreddy.txt")
objFso.CreateTextFile ("E:\Gcreddy.doc")
objFso.CreateTextFile ("E:\Gcreddy.xls")
objFso.CreateTextFile ("E:\Gcreddy.pdf")
Note: We can Create other files also, but they act as Text/Flat Files
Set objFile = objFSO.CreateTextFile("E:\csreddy.txt")
2) 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:\csreddy.txt")
End if
3) Reading Data character by character from a Flat File
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("E:\csreddy.txt", 1)
Do Until objFile.AtEndOfStream
strCharacters = objFile.Read(1)
msgbox strCharacters
Loop
4) Reading Data line by line from a Flat File
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("E:\csreddy.txt", 1)
Do Until objFile.AtEndOfStream
strCharacters = objFile.Readline
msgbox strCharacters
Loop
5) Data Driven Testing by fetching Test data directly from a Text file.
**************************************************
'Test Requirement: Data Driven Testing by Fetching Test data directly from a Text file.
'Pre-requisites:
'gcr.txt (Test Data)
'Test Flow:
'Creating an Automation Object in FileSystem class
'Opening the External Test Data file using the Object
'Read the Data & Split the Data
'Generating the Login Operation
'Pass Parameters
'*************************************************
Dim objFso, myFile, myLine, myField
Set objFso=CreateObject("Scripting.FileSystemObject")
Set myFile=objFso.OpenTextFile("C:\Documents and Settings\gcr.GCRC-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
6) Writing data to a text file
Dim Stuff, myFSO, WriteStuff, dateStamp
dateStamp = Date()
Stuff = "I am Preparing this script: " &dateStamp
Set myFSO = CreateObject("Scripting.FileSystemObject")
Set WriteStuff = myFSO.OpenTextFile("e:\Gcreddy.txt", 8, True)
WriteStuff.WriteLine(Stuff)
WriteStuff.Close
SET WriteStuff = NOTHING
SET myFSO = NOTHING
7) Delete a text file
Set objFSO=createobject("Scripting.filesystemobject")
Set txtFilepath = objFSO.GetFile("E:\gcr.txt")
txtFilepath.Delete()
8) 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
9) Comparing two text files
Dim f1, f2
f1="e:\csreddy1.txt"
f2="e:\csreddy2.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
No comments:
Post a Comment