SlideShare a Scribd company logo
gcrindia@gmail.com



              Working with Files
I) Computer file System
In computing, a file system (often also written as filesystem) is a method for storing
and organizing computer files and the data they contain to make it easy to find and
access them. File systems may use a data storage device such as a hard disk or CD-
ROM.

II) Working with Drives and Folders

a) Creating a Folder

Option Explicit
Dim objFSO, objFolder, strDirectory
strDirectory = "D:logs"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.CreateFolder(strDirectory)

b) Deleting a Folder

Set oFSO = CreateObject("Scripting.FileSystemObject")
oFSO.DeleteFolder("E:FSO")

c) Copying Folders

Set oFSO=createobject("Scripting.Filesystemobject")
oFSO.CopyFolder "E:gcr6", "C:jvr", True

d) Checking weather the folder available or not, if not creating the folder

Option Explicit
Dim objFSO, objFolder, strDirectory
strDirectory = "D:logs"
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

e) 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

f) Getting available space on a Disk Drive


      G.C.Reddy, QTP Trainer, Hyderabad (9247837478)                                1
gcrindia@gmail.com


Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oDrive = oFSO.GetDrive("C:")
MsgBox "Available space: " & oDrive.AvailableSpace

III) Working with Flat Files
a) Creating a Flat File
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("E:ScriptLog.txt")

b) Checking weather the File is available or not, if not creating the File

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
c) Reading Data character by character from a Flat File
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("E:gcr.txt", 1)
Do Until objFile.AtEndOfStream
  strCharacters = objFile.Read(1)
  msgbox strCharacters
Loop
d) Deleting Data From a Flat File
e) Comparing Flat Files
f) Searching Particular Strings in a Flat File
d) Reading Data line by line from a Flat File

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("E:gcr.txt", 1)
Do Until objFile.AtEndOfStream
  strCharacters = objFile.Readline
  msgbox strCharacters
Loop

e) 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:gcr.txt",1)
myfile.skipline
While myfile.atendofline <> True
x=myfile.readline
s=split (x, ",")



      G.C.Reddy, QTP Trainer, Hyderabad (9247837478)                         2
gcrindia@gmail.com

SystemUtil.Run "C:Program FilesMercury InteractiveQuickTest Professionalsamples
flightappflight4a.exe","","C:Program FilesMercury InteractiveQuickTest
Professionalsamplesflightapp","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

f) 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:gcr.txt", 8, True)
WriteStuff.WriteLine(Stuff)
WriteStuff.Close
SET WriteStuff = NOTHING
SET myFSO = NOTHING

g) Delete a text file

Set objFSO=createobject("Scripting.filesystemobject")
Set txtFilepath = objFSO.GetFile("E:gcr.txt")
 txtFilepath.Delete()



h) 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

i) Comparing two text files

Dim f1, f2
f1="e:gcr1.txt"
f2="e:gcr2.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


      G.C.Reddy, QTP Trainer, Hyderabad (9247837478)                               3
gcrindia@gmail.com

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 file

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

IV) Working with Word Docs

a) Create a word document and enter some data & save

Dim objWD
Set objWD = CreateObject("Word.Application")


      G.C.Reddy, QTP Trainer, Hyderabad (9247837478)                        4
gcrindia@gmail.com

objWD.Documents.Add

objWD.Selection.TypeText "This is some text." & Chr(13) & "This is some more text"
objWD.ActiveDocument.SaveAs "e:gcreddy.doc"
objWD.Quit

V) Working with Excel Sheets

a) Create an excel sheet and enter a value into first cell

Dim objexcel
Set objExcel = createobject("Excel.application")
objexcel.Visible = True
objexcel.Workbooks.add
objexcel.Cells(1, 1).Value = "Testing"
objexcel.ActiveWorkbook.SaveAs("f:gcreddy1.xls")
objexcel.Quit

b) Compare two excel files

Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set objWorkbook1= objExcel.Workbooks.Open("E:gcr1.xls")
Set objWorkbook2= objExcel.Workbooks.Open("E:gcr2.xls")

Set objWorksheet1= objWorkbook1.Worksheets(1)

Set objWorksheet2= objWorkbook2.Worksheets(1)

  For Each cell In objWorksheet1.UsedRange
     If cell.Value <> objWorksheet2.Range(cell.Address).Value Then
         msgbox "value is different"
     Else
         msgbox "value is same"
     End If
    Next
objWorkbook1.close
objWorkbook2.close
objExcel.quit
set objExcel=nothing




      G.C.Reddy, QTP Trainer, Hyderabad (9247837478)                                 5

More Related Content

What's hot (20)

PDF
NDC London 2013 - Mongo db for c# developers
Simon Elliston Ball
 
PPTX
Rapid and Scalable Development with MongoDB, PyMongo, and Ming
Rick Copeland
 
PDF
Realtime Database with iOS and Firebase
NSCoder Mexico
 
PDF
Fun Teaching MongoDB New Tricks
MongoDB
 
TXT
Tipo virus espia con esto aprenderan a espiar a personas etc jeropas de mrd :v
Arian Gutierrez
 
PPTX
MongoDB-SESSION03
Jainul Musani
 
PDF
Google App Engine Developer - Day3
Simon Su
 
PPTX
MongoDB: Easy Java Persistence with Morphia
Scott Hernandez
 
PPT
Tricks
MongoDB
 
PDF
The Ring programming language version 1.7 book - Part 32 of 196
Mahmoud Samir Fayed
 
PDF
MySQL flexible schema and JSON for Internet of Things
Alexander Rubin
 
PDF
Softshake - Offline applications
jeromevdl
 
PDF
San Francisco Java User Group
kchodorow
 
PDF
Building DSLs with Groovy
Sten Anderson
 
PPTX
Sequelize
Tarek Raihan
 
PDF
The Ring programming language version 1.5.1 book - Part 27 of 180
Mahmoud Samir Fayed
 
PPTX
Unit testing powershell
Matt Wrock
 
PDF
Indexing
Mike Dirolf
 
DOCX
โค้ด
MareenaHahngeh
 
PPTX
Introduction to NOSQL And MongoDB
Behrouz Bakhtiari
 
NDC London 2013 - Mongo db for c# developers
Simon Elliston Ball
 
Rapid and Scalable Development with MongoDB, PyMongo, and Ming
Rick Copeland
 
Realtime Database with iOS and Firebase
NSCoder Mexico
 
Fun Teaching MongoDB New Tricks
MongoDB
 
Tipo virus espia con esto aprenderan a espiar a personas etc jeropas de mrd :v
Arian Gutierrez
 
MongoDB-SESSION03
Jainul Musani
 
Google App Engine Developer - Day3
Simon Su
 
MongoDB: Easy Java Persistence with Morphia
Scott Hernandez
 
Tricks
MongoDB
 
The Ring programming language version 1.7 book - Part 32 of 196
Mahmoud Samir Fayed
 
MySQL flexible schema and JSON for Internet of Things
Alexander Rubin
 
Softshake - Offline applications
jeromevdl
 
San Francisco Java User Group
kchodorow
 
Building DSLs with Groovy
Sten Anderson
 
Sequelize
Tarek Raihan
 
The Ring programming language version 1.5.1 book - Part 27 of 180
Mahmoud Samir Fayed
 
Unit testing powershell
Matt Wrock
 
Indexing
Mike Dirolf
 
โค้ด
MareenaHahngeh
 
Introduction to NOSQL And MongoDB
Behrouz Bakhtiari
 

Similar to File System Operations (20)

DOC
Excel Scripting
G C Reddy Technologies
 
DOCX
Parameterization is nothing but giving multiple input
uanna
 
PDF
Everything is composable
Victor Igor
 
PDF
Basic file operations CBSE class xii ln 7
SATHASIVAN H
 
PPTX
Coding using jscript test complete
Viresh Doshi
 
PDF
The Ring programming language version 1.5.1 book - Part 65 of 180
Mahmoud Samir Fayed
 
DOCX
Inventory program in mca p1
rameshvvv
 
PDF
Goptuna Distributed Bayesian Optimization Framework at Go Conference 2019 Autumn
Masashi Shibata
 
DOC
Qtp test
G.C Reddy
 
PDF
The Ring programming language version 1.10 book - Part 37 of 212
Mahmoud Samir Fayed
 
PDF
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
julien.ponge
 
PDF
File and directories in python
Lifna C.S
 
TXT
Examplecode
Sailaja Rama
 
PPTX
Python IO
RTS Tech
 
PDF
The Ring programming language version 1.6 book - Part 71 of 189
Mahmoud Samir Fayed
 
PDF
Nosql hands on handout 04
Krishna Sankar
 
DOC
Adodb Scripts And Some Sample Scripts[1]
User1test
 
DOC
Adodb Scripts And Some Sample Scripts[1]
testduser1
 
PPT
Java Input Output and File Handling
Sunil OS
 
Excel Scripting
G C Reddy Technologies
 
Parameterization is nothing but giving multiple input
uanna
 
Everything is composable
Victor Igor
 
Basic file operations CBSE class xii ln 7
SATHASIVAN H
 
Coding using jscript test complete
Viresh Doshi
 
The Ring programming language version 1.5.1 book - Part 65 of 180
Mahmoud Samir Fayed
 
Inventory program in mca p1
rameshvvv
 
Goptuna Distributed Bayesian Optimization Framework at Go Conference 2019 Autumn
Masashi Shibata
 
Qtp test
G.C Reddy
 
The Ring programming language version 1.10 book - Part 37 of 212
Mahmoud Samir Fayed
 
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
julien.ponge
 
File and directories in python
Lifna C.S
 
Examplecode
Sailaja Rama
 
Python IO
RTS Tech
 
The Ring programming language version 1.6 book - Part 71 of 189
Mahmoud Samir Fayed
 
Nosql hands on handout 04
Krishna Sankar
 
Adodb Scripts And Some Sample Scripts[1]
User1test
 
Adodb Scripts And Some Sample Scripts[1]
testduser1
 
Java Input Output and File Handling
Sunil OS
 
Ad

More from G.C Reddy (19)

DOC
Qtp training in hyderabad
G.C Reddy
 
DOC
Html
G.C Reddy
 
DOC
Functions
G.C Reddy
 
DOC
New features in qtp11
G.C Reddy
 
DOC
QTP Training
G.C Reddy
 
DOC
Software+struc+doc
G.C Reddy
 
DOC
Qtp (advanced)
G.C Reddy
 
DOC
Qtp (basics to advanced)
G.C Reddy
 
DOC
Qtp commands
G.C Reddy
 
DOC
Object Repository
G.C Reddy
 
DOC
Advanced Qtp
G.C Reddy
 
DOC
Advanced Qtp Book
G.C Reddy
 
DOC
QTP 10 00 Guide
G.C Reddy
 
PPT
Qtp Scripts
G.C Reddy
 
DOC
Manual Testing
G.C Reddy
 
DOC
Qtp Faq
G.C Reddy
 
DOC
Qtp Summary
G.C Reddy
 
DOC
Qtp Scripts
G.C Reddy
 
DOC
Web Dictionary
G.C Reddy
 
Qtp training in hyderabad
G.C Reddy
 
Html
G.C Reddy
 
Functions
G.C Reddy
 
New features in qtp11
G.C Reddy
 
QTP Training
G.C Reddy
 
Software+struc+doc
G.C Reddy
 
Qtp (advanced)
G.C Reddy
 
Qtp (basics to advanced)
G.C Reddy
 
Qtp commands
G.C Reddy
 
Object Repository
G.C Reddy
 
Advanced Qtp
G.C Reddy
 
Advanced Qtp Book
G.C Reddy
 
QTP 10 00 Guide
G.C Reddy
 
Qtp Scripts
G.C Reddy
 
Manual Testing
G.C Reddy
 
Qtp Faq
G.C Reddy
 
Qtp Summary
G.C Reddy
 
Qtp Scripts
G.C Reddy
 
Web Dictionary
G.C Reddy
 
Ad

Recently uploaded (20)

PDF
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
PPTX
Farrell_Programming Logic and Design slides_10e_ch02_PowerPoint.pptx
bashnahara11
 
PDF
Generative AI vs Predictive AI-The Ultimate Comparison Guide
Lily Clark
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PDF
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
PDF
TrustArc Webinar - Navigating Data Privacy in LATAM: Laws, Trends, and Compli...
TrustArc
 
PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
PPTX
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
PDF
Market Insight : ETH Dominance Returns
CIFDAQ
 
PDF
The Future of Artificial Intelligence (AI)
Mukul
 
PDF
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
PDF
Per Axbom: The spectacular lies of maps
Nexer Digital
 
PDF
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
Farrell_Programming Logic and Design slides_10e_ch02_PowerPoint.pptx
bashnahara11
 
Generative AI vs Predictive AI-The Ultimate Comparison Guide
Lily Clark
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
TrustArc Webinar - Navigating Data Privacy in LATAM: Laws, Trends, and Compli...
TrustArc
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
Brief History of Internet - Early Days of Internet
sutharharshit158
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
Market Insight : ETH Dominance Returns
CIFDAQ
 
The Future of Artificial Intelligence (AI)
Mukul
 
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
Per Axbom: The spectacular lies of maps
Nexer Digital
 
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 

File System Operations

  • 1. [email protected] Working with Files I) Computer file System In computing, a file system (often also written as filesystem) is a method for storing and organizing computer files and the data they contain to make it easy to find and access them. File systems may use a data storage device such as a hard disk or CD- ROM. II) Working with Drives and Folders a) Creating a Folder Option Explicit Dim objFSO, objFolder, strDirectory strDirectory = "D:logs" Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFolder = objFSO.CreateFolder(strDirectory) b) Deleting a Folder Set oFSO = CreateObject("Scripting.FileSystemObject") oFSO.DeleteFolder("E:FSO") c) Copying Folders Set oFSO=createobject("Scripting.Filesystemobject") oFSO.CopyFolder "E:gcr6", "C:jvr", True d) Checking weather the folder available or not, if not creating the folder Option Explicit Dim objFSO, objFolder, strDirectory strDirectory = "D:logs" 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 e) 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 f) Getting available space on a Disk Drive G.C.Reddy, QTP Trainer, Hyderabad (9247837478) 1
  • 2. [email protected] Set oFSO = CreateObject("Scripting.FileSystemObject") Set oDrive = oFSO.GetDrive("C:") MsgBox "Available space: " & oDrive.AvailableSpace III) Working with Flat Files a) Creating a Flat File Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.CreateTextFile("E:ScriptLog.txt") b) Checking weather the File is available or not, if not creating the File 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 c) Reading Data character by character from a Flat File Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile("E:gcr.txt", 1) Do Until objFile.AtEndOfStream strCharacters = objFile.Read(1) msgbox strCharacters Loop d) Deleting Data From a Flat File e) Comparing Flat Files f) Searching Particular Strings in a Flat File d) Reading Data line by line from a Flat File Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile("E:gcr.txt", 1) Do Until objFile.AtEndOfStream strCharacters = objFile.Readline msgbox strCharacters Loop e) 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:gcr.txt",1) myfile.skipline While myfile.atendofline <> True x=myfile.readline s=split (x, ",") G.C.Reddy, QTP Trainer, Hyderabad (9247837478) 2
  • 3. [email protected] SystemUtil.Run "C:Program FilesMercury InteractiveQuickTest Professionalsamples flightappflight4a.exe","","C:Program FilesMercury InteractiveQuickTest Professionalsamplesflightapp","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 f) 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:gcr.txt", 8, True) WriteStuff.WriteLine(Stuff) WriteStuff.Close SET WriteStuff = NOTHING SET myFSO = NOTHING g) Delete a text file Set objFSO=createobject("Scripting.filesystemobject") Set txtFilepath = objFSO.GetFile("E:gcr.txt") txtFilepath.Delete() h) 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 i) Comparing two text files Dim f1, f2 f1="e:gcr1.txt" f2="e:gcr2.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 G.C.Reddy, QTP Trainer, Hyderabad (9247837478) 3
  • 4. [email protected] 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 file 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 IV) Working with Word Docs a) Create a word document and enter some data & save Dim objWD Set objWD = CreateObject("Word.Application") G.C.Reddy, QTP Trainer, Hyderabad (9247837478) 4
  • 5. [email protected] objWD.Documents.Add objWD.Selection.TypeText "This is some text." & Chr(13) & "This is some more text" objWD.ActiveDocument.SaveAs "e:gcreddy.doc" objWD.Quit V) Working with Excel Sheets a) Create an excel sheet and enter a value into first cell Dim objexcel Set objExcel = createobject("Excel.application") objexcel.Visible = True objexcel.Workbooks.add objexcel.Cells(1, 1).Value = "Testing" objexcel.ActiveWorkbook.SaveAs("f:gcreddy1.xls") objexcel.Quit b) Compare two excel files Set objExcel = CreateObject("Excel.Application") objExcel.Visible = True Set objWorkbook1= objExcel.Workbooks.Open("E:gcr1.xls") Set objWorkbook2= objExcel.Workbooks.Open("E:gcr2.xls") Set objWorksheet1= objWorkbook1.Worksheets(1) Set objWorksheet2= objWorkbook2.Worksheets(1) For Each cell In objWorksheet1.UsedRange If cell.Value <> objWorksheet2.Range(cell.Address).Value Then msgbox "value is different" Else msgbox "value is same" End If Next objWorkbook1.close objWorkbook2.close objExcel.quit set objExcel=nothing G.C.Reddy, QTP Trainer, Hyderabad (9247837478) 5