VBScript FileSystemObject

VBScript FileSystemObject

File System Operations
i) What is Computer File System?

It is a feature of Operating system used to work with Drives, Folders and Files.

ii) Examples for File System Operations

Create a Folder

Copy a Folder

Delete a Folder

Create a Text file

Delete a Text file

Read data

Write data

Comparisons

Search operations etc...
-------------------------------
iii) How end user performs File System Operations?

End user performs File System Operations manually with the help of Input devices.

iv) How to perform automatic File System Operations?

Using FileSystemObject we can perform automatic File system operations.

Create File System Object

Syntax:

Set variable = CreateObject("Class value")

Set -Statement

CreateObject- VBScript Built in Function

Example:

Set objFso = CreateObject("Scripting.FileSystemObject")

Examples:

1) Create a Folder

Dim objFso
Set objFso = CreateObject("Scripting.FileSystemObject")
objFso.CreateFolder "C:\Users\G C REDDY\Desktop\QTP"
Set objFso = Nothing 'To release the memory
----------------------------------
2) Check the existence of QTP folder if not exists then create the Folder.

Dim objFso
Set objFso = CreateObject("Scripting.FileSystemObject")

If Not objFso.FolderExists("C:\Users\G C REDDY\Desktop\QTP") Then
objFso.CreateFolder "C:\Users\G C REDDY\Desktop\QTP"   
End If

Set objFso = Nothing
----------------------------------
Dim objFso, FolderPath
FolderPath = "C:\Users\G C REDDY\Desktop\QTP"
Set objFso = CreateObject("Scripting.FileSystemObject")

If Not objFso.FolderExists(FolderPath) Then
objFso.CreateFolder FolderPath   
End If

Set objFso = Nothing
----------------------------------
3) Copy a Folder

Dim objFso, FolderPath

Set objFso = CreateObject("Scripting.FileSystemObject")
objFso.CopyFolder "C:\Users\G C REDDY\Desktop\QTP", "C:\QTP"

Set objFso = Nothing
----------------------------------
Dim objFso, FolderPath

Set objFso = CreateObject("Scripting.FileSystemObject")
objFso.CopyFolder "C:\Users\G C REDDY\Desktop\QTP", "C:\UFT"

Set objFso = Nothing
----------------------------------
4) Delete a Folder

Dim objFso
Set objFso = CreateObject("Scripting.FileSystemObject")
objFso.DeleteFolder "C:\Users\G C REDDY\Desktop\QTP"

Set objFso = Nothing
----------------------------------
5) Check the existence of QTP folder if exists then delete the folder.

Dim objFso
Set objFso = CreateObject("Scripting.FileSystemObject")

If objFso.FolderExists("C:\Users\G C REDDY\Desktop\QTP") Then
objFso.DeleteFolder "C:\Users\G C REDDY\Desktop\QTP"   
End If

Set objFso = Nothing
----------------------------------
6) Create a Text File.

Dim objFso
Set objFso = CreateObject("Scripting.FileSystemObject")
objFso.CreateTextFile "C:\Users\G C REDDY\Desktop\QTP.txt"
Set objFso = Nothing
----------------------------------
Dim objFso
Set objFso = CreateObject("Scripting.FileSystemObject")

objFso.CreateTextFile "C:\Users\G C REDDY\Desktop\QTP.txt"

objFso.CreateTextFile "C:\Users\G C REDDY\Desktop\QTP.doc"

objFso.CreateTextFile "C:\Users\G C REDDY\Desktop\QTP.xls"

objFso.CreateTextFile "C:\Users\G C REDDY\Desktop\QTP.pdf"

Set objFso = Nothing
----------------------------------
7) Delete Files

Dim objFso
Set objFso = CreateObject("Scripting.FileSystemObject")

objFso.DeleteFile "C:\Users\G C REDDY\Desktop\QTP.txt"

objFso.DeleteFile "C:\Users\G C REDDY\Desktop\QTP.doc"

objFso.DeleteFile "C:\Users\G C REDDY\Desktop\QTP.xls"

objFso.DeleteFile "C:\Users\G C REDDY\Desktop\QTP.pdf"

Set objFso = Nothing
----------------------------------
File System Operations

    a) High Level Operations/External Operations
    b) Low level operations / Internal Operations

a) High Level Operations:

Create a Folder

Copy a Folder

Delete a Folder

Create a Text file

Delete a Text file

b) Low level operations:

Reading
    Read Character by Character
    Read Line by Line
    Read All

Writing
    Write Continuously
    Write Line by Line
    Append

Comparison
    Compare two text files by Size (*High/External Operation)
    Compare two text files by Text
    Compare two text files by Binary values

Search Operations etc...
----------------------------------
Using File System Object we can perform High level Operations

Using Text Stream Object We can perform Internal operations

Create File System Object

Set variable = CreateObject("Scripting.FileSystemObject")

Create Text Stream Object

Set variable = FileSystemObject.CreateTextFile/OpenTextFile("File Path"), File Mode

File Modes

1 for Read
2 for Write
8 for Append

Read is default file mode.
----------------------------------
8) Read a Text file Character by Character

Dim objFso, objTextstream, myChar
Set objFso = Createobject("Scripting.FileSystemObject")
Set objTextstream = objFso.OpenTextFile("C:\Users\G C REDDY\Desktop\abcd.txt")

Do Until objTextstream.AtEndOfStream = True
myChar = objTextstream.Read(1)
Msgbox myChar
Loop

objTextstream.Close
Set objTextstream = Nothing
Set objFso = Nothing
----------------------------------
9) Read a Text file Line by Line

Dim objFso, objTextstream, myLine
Set objFso = Createobject("Scripting.FileSystemObject")
Set objTextstream = objFso.OpenTextFile("C:\Users\G C REDDY\Desktop\abcd.txt")

Do Until objTextstream.AtEndOfStream = True
myLine = objTextstream.ReadLine
Msgbox myLine
Loop

objTextstream.Close
Set objTextstream = Nothing
Set objFso = Nothing
----------------------------------
10) Read a Text File (Read All)

Dim objFso, objTextstream, myContent
Set objFso = Createobject("Scripting.FileSystemObject")
Set objTextstream = objFso.OpenTextFile("C:\Users\G C REDDY\Desktop\abcd.txt")

myContent = objTextstream.ReadAll
Msgbox myContent

objTextstream.Close
Set objTextstream = Nothing
Set objFso = Nothing
----------------------------------
Dim objFso, objTextstream, myContent
Set objFso = Createobject("Scripting.FileSystemObject")
Set objTextstream = objFso.OpenTextFile("C:\Users\G C REDDY\Desktop\UFT 23.txt")

myContent = objTextstream.ReadAll
Print myContent

objTextstream.Close
Set objTextstream = Nothing
Set objFso = Nothing
----------------------------------
11) Read Test data from a Text file and perform Data Driven Testing for Login Functionality.

Dim objFso, objTextstream, myLine, myField
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextstream = objFso.OpenTextFile("C:\Users\G C REDDY\Desktop\abcd.txt")
objTextstream.SkipLine

Do While objTextstream.AtEndOfStream = False
myLine=objTextstream.ReadLine
myField = Split(myLine,", ")

SystemUtil.Run "C:\Program Files\HP\Unified Functional Testing\samples\flight\app\flight4a.exe"
Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set myField(0)
Dialog("Login").WinEdit("Password:").Set myField(1)
wait 2
Dialog("Login").WinButton("OK").Click
Window("Flight Reservation").Close
Loop
objTextstream.Close
Set objTextstream = Nothing
Set objFso = Nothing
----------------------------------
12) Read Test data (4 to 7 Records) from a Text file and perform Data Driven Testing for Login Functionality.

Dim objFso, objTextstream, myLine, myField, LineCount
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextstream = objFso.OpenTextFile("C:\Users\G C REDDY\Desktop\abcd.txt")

LineCount=0
Do While objTextstream.AtEndOfStream = False
myLine=objTextstream.ReadLine
LineCount=LineCount+1

If LineCount > 4 And LineCount < 9 Then
myField = Split(myLine,", ")

SystemUtil.Run "C:\Program Files\HP\Unified Functional Testing\samples\flight\app\flight4a.exe"
Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set myField(0)
Dialog("Login").WinEdit("Password:").Set myField(1)
wait 2
Dialog("Login").WinButton("OK").Click
Window("Flight Reservation").Close
End If
Loop

objTextstream.Close
Set objTextstream = Nothing
Set objFso = Nothing
----------------------------------
13) Write data to a text file

Dim objFso, objTextStream, num1, num2, Res
num1=100
num2=200
Res= num1 + num2
Res1 = num1 * num2
Res2 = num1/num2
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextStream = objfso.CreateTextFile ("C:\Users\G C REDDY\Desktop\xyz.txt", 2)

objTextStream.Write "Addition of num1, num2 is: "&Res
objTextStream.Write "Multiplication of num1, num2 is: "&Res1
objTextStream.Write "Subtraction of num1, num2 is: "&Res2

objTextStream.Close
Set objTextStream = Nothing
Set objFso = Nothing
----------------------------------
14) Write line by line

Dim objFso, objTextStream, num1, num2, Res
num1=100
num2=200
Res= num1 + num2
Res1 = num1 * num2
Res2 = num1/num2
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextStream = objfso.CreateTextFile ("C:\Users\G C REDDY\Desktop\xyz.txt", 2)

objTextStream.WriteLine "Addition of num1, num2 is: "&Res
objTextStream.WriteLine "Multiplication of num1, num2 is: "&Res1
objTextStream.WriteLine "Subtraction of num1, num2 is: "&Res2

objTextStream.Close
Set objTextStream = Nothing
Set objFso = Nothing
----------------------------------
15) Append data

'Read Customer names from 1 to 10 Records and export to a text file

Dim objFso, objTextStream, num1, num2, Res

Set objFso = CreateObject("Scripting.FileSystemObject")
Set objTextStream = objFso.CreateTextFile ("C:\Users\G C REDDY\Desktop\xyz.txt", 8)
For i = 1 To 2 Step 1
Window("Flight Reservation").Activate
Window("Flight Reservation").WinButton("Button").Click
Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON"
Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set i
wait 2
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
CustomerName = Window("Flight Reservation").WinEdit("Name:").GetROProperty("text")
objTextStream.WriteLine CustomerName
Next

Set objTextStream = Nothing
Set objFso = Nothing
----------------------------------

0 comments:

Post a Comment