0% found this document useful (0 votes)
367 views

Working With Files: I) Computer File System

The document discusses various methods for working with files and folders in a computer file system such as creating, deleting, copying, and checking folders. It also covers reading from and writing to flat files and text files including reading line by line or character by character. Methods for working with Word documents and Excel sheets like creating, opening, saving, and comparing files are also provided. The document provides code examples for all these file system operations in Visual Basic Script.

Uploaded by

knani9090
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
367 views

Working With Files: I) Computer File System

The document discusses various methods for working with files and folders in a computer file system such as creating, deleting, copying, and checking folders. It also covers reading from and writing to flat files and text files including reading line by line or character by character. Methods for working with Word documents and Excel sheets like creating, opening, saving, and comparing files are also provided. The document provides code examples for all these file system operations in Visual Basic Script.

Uploaded by

knani9090
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

For QTP Scripts & Documents Visit: www.gcreddy.

com
Working with Files
I !omputer "ile System
In computing, a "ile system (often also written as "ilesystem) 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
!"#.
II Working with Dri#es $nd Folders
$ !re$ting $ Folder
"ption $%plicit
Dim o&'F(", o&'Folder, strDirectory
strDirectory ) *D+,logs*
(et o&'F(" ) Create"&'ect(*(cripting.File(ystem"&'ect*)
(et o&'Folder ) o&'F(".CreateFolder(strDirectory)
% Deleting $ Folder
(et oF(" ) Create"&'ect(*(cripting.File(ystem"&'ect*)
oF(".DeleteFolder(*$+,F("*)
c !opying Folders
(et oF(")createo&'ect(*(cripting.Filesystemo&'ect*)
oF(".CopyFolder *$+,gcr-*, *C+,'vr*, .rue
d !hecking we$ther the "older $#$il$%le or not& i" not cre$ting the "older
"ption $%plicit
Dim o&'F(", o&'Folder, strDirectory
strDirectory ) *D+,logs*
(et o&'F(" ) Create"&'ect(*(cripting.File(ystem"&'ect*)
If o&'F(".Folder$%ists(strDirectory) .hen
(et o&'Folder ) o&'F("./etFolder(strDirectory)
msg&o% strDirectory 0 * already created *
else
(et o&'Folder ) o&'F(".CreateFolder(strDirectory)
end if
e 'eturning $ collection o" Disk Dri#es
(et oF(" ) Create"&'ect(*(cripting.File(ystem"&'ect*)
(et colDrives ) oF(".Drives
For $ach oDrive in colDrives
#sg1o% *Drive letter+ * 0 oDrive.Drive2etter
3e%t
" (etting $#$il$%le sp$ce on $ Disk Dri#e
For So"tw$re Testing Documents #isit: www.gcreddy.net 1
For QTP Scripts & Documents Visit: www.gcreddy.com
(et oF(" ) Create"&'ect(*(cripting.File(ystem"&'ect*)
(et oDrive ) oF("./etDrive(*C+*)
#sg1o% *4vaila&le space+ * 0 oDrive.4vaila&le(pace
III Working with Fl$t Files
!re$ting $ Fl$t File
(et o&'F(" ) Create"&'ect(*(cripting.File(ystem"&'ect*)
(et o&'File ) o&'F(".Create.e%tFile(*$+,(cript2og.t%t*)
% !hecking we$ther the File is $#$il$%le or not& i" not cre$ting the File
strDirectory)*$+,*
strFile)*(cripting.t%t*
(et o&'F(" ) Create"&'ect(*(cripting.File(ystem"&'ect*)
If o&'F(".File$%ists(strDirectory 0 strFile) .hen
(et o&'Folder ) o&'F("./etFolder(strDirectory)
$lse
(et o&'File ) o&'F(".Create.e%tFile(*$+,(cript2og.t%t*)
$nd if
'e$ding D$t$ ch$r$cter %y ch$r$cter "rom $ Fl$t File
(et o&'F(" ) Create"&'ect(*(cripting.File(ystem"&'ect*)
(et o&'File ) o&'F("."pen.e%tFile(*$+,gcr.t%t*, 5)
Do 6ntil o&'File.4t$nd"f(tream
strCharacters ) o&'File.!ead(5)
msg&o% strCharacters
2oop
d) Deleting Data From a Flat File
e) Comparing Flat Files
f) (earching 7articular (trings in a Flat File
d 'e$ding D$t$ line %y line "rom $ Fl$t File
(et o&'F(" ) Create"&'ect(*(cripting.File(ystem"&'ect*)
(et o&'File ) o&'F("."pen.e%tFile(*$+,gcr.t%t*, 5)
Do 6ntil o&'File.4t$nd"f(tream
strCharacters ) o&'File.!eadline
msg&o% strCharacters
2oop
e 'e$ding d$t$ "rom $ "l$t "ile $nd using in d$t$ dri#en testing
Dim fso,myfile
(et fso)createo&'ect(*scripting.filesystemo&'ect*)
(et myfile) fso.opente%tfile (*F+,gcr.t%t*,5)
myfile.skipline
8hile myfile.atendofline 9: .rue
%)myfile.readline
s)split (%, *,*)
For So"tw$re Testing Documents #isit: www.gcreddy.net 2
For QTP Scripts & Documents Visit: www.gcreddy.com
(ystem6til.!un *C+,7rogram Files,#ercury Interactive,;uick.est
7rofessional,samples,flight,app,flight<a.e%e*,**,*C+,7rogram Files,#ercury
Interactive,;uick.est 7rofessional,samples,flight,app,*,*open*
Dialog(*2ogin*).4ctivate
Dialog(*2ogin*).8in$dit(*4gent 3ame+*).(et s(=)
Dialog(*2ogin*).8in$dit(*7assword+*).(et(ecure s(5)
Dialog(*2ogin*).8in1utton(*">*).Click
8indow(*Flight !eservation*).Close
8end
" Writing d$t$ to $ te)t "ile
Dim (tuff, myF(", 8rite(tuff, date(tamp
date(tamp ) Date()
(tuff ) *I am 7reparing this script+ * 0date(tamp
(et myF(" ) Create"&'ect(*(cripting.File(ystem"&'ect*)
(et 8rite(tuff ) myF("."pen.e%tFile(*e+,gcr.t%t*, ?, .rue)
8rite(tuff.8rite2ine((tuff)
8rite(tuff.Close
($. 8rite(tuff ) 3".@I3/
($. myF(" ) 3".@I3/
g Delete $ te)t "ile
(et o&'F(")createo&'ect(*(cripting.filesystemo&'ect*)
(et t%tFilepath ) o&'F("./etFile(*$+,gcr.t%t*)
t%tFilepath.Delete()
h !hecking we$ther the File is $#$il$%le or not& i" $#$il$%le delete the File
strDirectory)*$+,*
strFile)*gcr.t%t*
(et o&'F(" ) Create"&'ect(*(cripting.File(ystem"&'ect*)
If o&'F(".File$%ists(strDirectory 0 strFile) .hen
(et o&'File ) o&'F("./etfile(strDirectory 0 strFile)
o&'File.delete ()
$nd if
i !omp$ring two te)t "iles
Dim f5, fA
f5)*e+,gcr5.t%t*
fA)*e+,gcrA.t%t*
7u&lic Function CompareFiles (File7ath5, File7athA)
Dim F(, File5, FileA
(et F( ) Create"&'ect(*(cripting.File(ystem"&'ect*)
If F(./etFile(File7ath5).(ize 9: F(./etFile(File7athA).(ize .hen
CompareFiles ) .rue
$%it Function
For So"tw$re Testing Documents #isit: www.gcreddy.net 3
For QTP Scripts & Documents Visit: www.gcreddy.com
$nd If
(et File5 ) F(./etFile(File7ath5)."pen4s.e%t(tream(5, =)
(et FileA ) F(./etFile(File7athA)."pen4s.e%t(tream(5, =)
CompareFiles ) False
Do 8hile File5.4t$nd"f(tream ) False
(tr5 ) File5.!ead
(trA ) FileA.!ead
CompareFiles ) (trComp((tr5, (trA, =)
If CompareFiles 9: = .hen
CompareFiles ) .rue
$%it Do
$nd If
2oop
File5.Close()
FileA.Close()
$nd Function
Call Comparefiles(f5,fA)

If CompareFiles(f5, fA) ) False .hen
#sg1o% *Files are identical.*
$lse
#sg1o% *Files are different.*
$nd If
* !ounting the num%er o" times $ word $ppe$rs in $ "ile
sFile3ame)*$+,gcr.t%t*
s(tring)*gcreddy*
Const F"!B!$4DI3/ ) 5
Dim oFso, o.%tFile, s!ead.%t, o!eg$%, o#atches
(et oFso ) Create"&'ect(*(cripting.File(ystem"&'ect*)
(et o.%tFile ) oFso."pen.e%tFile(sFile3ame, F"!B!$4DI3/)
s!ead.%t ) o.%tFile.!ead4ll
(et o!eg$% ) 3ew !eg$%p
o!eg$%.7attern ) s(tring
o!eg$%.IgnoreCase ) &IgnoreCase
o!eg$%./lo&al ) .rue
(et o#atches ) o!eg$%.$%ecute(s!ead.%t)
#atchesFound ) o#atches.Count
(et o.%tFile ) 3othing + (et oFso ) 3othing + (et o!eg$% ) 3othing
msg&o% #atchesFound
IV Working with Word Docs
$ !re$te $ word document $nd enter some d$t$ & s$#e
Dim o&'8D
(et o&'8D ) Create"&'ect(*8ord.4pplication*)
For So"tw$re Testing Documents #isit: www.gcreddy.net 4
For QTP Scripts & Documents Visit: www.gcreddy.com
o&'8D.Documents.4dd
o&'8D.(election..ype.e%t *.his is some te%t.* 0 Chr(5C) 0 *.his is some more te%t*
o&'8D.4ctiveDocument.(ave4s *e+,gcreddy.doc*
o&'8D.;uit
V Working with +)cel Sheets
$ !re$te $n e)cel sheet $nd enter $ #$lue into "irst cell
Dim o&'e%cel
(et o&'$%cel ) createo&'ect(*$%cel.application*)
o&'e%cel.Disi&le ) .rue
o&'e%cel.8ork&ooks.add
o&'e%cel.Cells(5, 5).Dalue ) *.esting*
o&'e%cel.4ctive8ork&ook.(ave4s(*f+,gcreddy5.%ls*)
o&'e%cel.;uit
% !omp$re two e)cel "iles
(et o&'$%cel ) Create"&'ect(*$%cel.4pplication*)
o&'$%cel.Disi&le ) .rue
(et o&'8ork&ook5) o&'$%cel.8ork&ooks."pen(*$+,gcr5.%ls*)
(et o&'8ork&ookA) o&'$%cel.8ork&ooks."pen(*$+,gcrA.%ls*)
(et o&'8orksheet5) o&'8ork&ook5.8orksheets(5)
(et o&'8orksheetA) o&'8ork&ookA.8orksheets(5)
For $ach cell In o&'8orksheet5.6sed!ange
If cell.Dalue 9: o&'8orksheetA.!ange(cell.4ddress).Dalue .hen
msg&o% *value is different*
$lse
msg&o% *value is same*
$nd If
3e%t
o&'8ork&ook5.close
o&'8ork&ookA.close
o&'$%cel.Euit
set o&'$%cel)nothing
For So"tw$re Testing Documents #isit: www.gcreddy.net 5

You might also like