File System Operations

File System Operations

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.

'Author: G C Reddy

'Date of Creation:
13-08-2010

'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 

for more Examples visit:


------------------------------------------
QTP Script examples

0 comments:

Post a Comment