Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Save to My Library
Look up keyword or section
Like this
0Activity
0 of .
Results for:
No results containing your search query
P. 1
Working With Files

Working With Files

Ratings: (0)|Views: 3 |Likes:
Published by Venkatesh.S
QTP-Working With Files
QTP-Working With Files

More info:

Published by: Venkatesh.S on Feb 26, 2010
Copyright:Public Domain

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

10/11/2012

pdf

text

original

 
Chapter 07 Scripting Quicktest Professional Page 1
Dani Vainstein Working with Files Page 1 of 112
SCRIPTING EXCEL...............................................................................................................2
 
C
REATING AND
T
ERMINATING AN
I
NSTANCE OF
MS-E
XCEL
...............................................2
 
ETURNING A
C
OLLECTION OF
D
ISK 
D
RIVES
..................................................................... 3
 
B
INDING TO A
S
PECIFIC
D
ISK 
D
RIVE
................................................................................. 3
 
E
 NUMERATING
D
ISK 
D
RIVE
P
ROPERTIES
........................................................................... 4
 
E
 NSURING
T
HAT A
D
RIVE IS
EADY
.................................................................................. 4
 
M
ANAGING
F
OLDERS
.............................................................................................................5
 
B
INDING
F
OLDERS
............................................................................................................ 5
 
V
ERIFYING
T
HAT A
F
OLDER 
E
XISTS
.................................................................................. 6
 
C
REATING A
F
OLDER 
........................................................................................................ 6
 
D
ELETING A
F
OLDER 
......................................................................................................... 7
 
C
OPYING A
F
OLDER AND
I
TS
C
ONTENTS
........................................................................... 8
 
M
OVING A
F
OLDER AND
I
TS
C
ONTENTS
............................................................................ 9
 
ENAMING A
F
OLDER 
..................................................................................................... 10
 
E
 NUMERATING
F
OLDER 
P
ROPERTIES
............................................................................... 11
 
M
ANAGING
F
OLDER 
A
TTRIBUTES
................................................................................... 12
 
M
ANAGING
F
ILES
................................................................................................................18
 
B
INDING TO A
F
ILE
......................................................................................................... 18
 
V
ERIFYING
T
HAT A
F
ILE
E
XISTS
..................................................................................... 19
 
D
ELETING A
F
ILE
............................................................................................................ 20
 
C
OPYING A
F
ILE
.............................................................................................................. 21
 
M
OVING A
F
ILE
............................................................................................................... 22
 
ENAME A
F
ILE
.............................................................................................................. 23
 
M
ANAGING
F
ILE
P
ROPERTIES
.......................................................................................... 23
 
EADING AND
W
RITING
T
EXT
F
ILES
..................................................................................27
 
EADING
T
EXT
F
ILES
...................................................................................................... 30
 
W
RITE TO
T
EXT
F
ILES
..................................................................................................... 34
 
M
ANAGING
F
ILES AND
F
OLDERS
U
SING
WMI....................................................................36
 
C
OMPARING
WMI
AND THE
F
ILE
S
YSTEM
O
BJECT
............................................................ 37
 
M
ANAGING
F
ILES AND
F
OLDERS
U
SING THE
W
INDOWS
S
HELL
O
BJECT
...........................40
 
F
OLDERS AND
F
OLDERS
O
BJECT
.........................................................................................41
 
W
IN
32_D
IRECTORY
C
LASS
............................................................................................. 41
 
T
HE
F
ILE
S
YSTEM
O
BJECT
O
BJECT
......................................................................................51
 
T
HE
FSO
 
O
BJECT
M
ODEL
............................................................................................... 51
 
P
ROGRAMMING THE
F
ILE
S
YSTEM
O
BJECT
....................................................................... 53
 
T
HE
F
ILE
S
YSTEM
O
BJECT
P
ROPERTIES AND METHODS
..................................................... 53
 
D
RIVES
C
OLLECTION
O
BJECT
......................................................................................... 74
 
D
RIVE
O
BJECT
................................................................................................................ 75
 
F
OLDERS
C
OLLECTION
O
BJECT
....................................................................................... 80
 
F
OLDER 
O
BJECT
.............................................................................................................. 83
 
F
ILES
C
OLLECTION
O
BJECT
............................................................................................. 91
 
F
ILE
O
BJECT
................................................................................................................... 93
 
T
EXT
S
TREAM
O
BJECT
....................................................................................................101
 
Q&A...................................................................................................................................106
 
H
OW TO
E
 NUMERATE
F
OLDERS AND
F
OLDER 
P
ROPERTIES
?...........................................106
 
H
OW TO
E
 NUMERATE
A
LL THE
F
OLDERS ON A
C
OMPUTER 
?...........................................108
 
H
OW TO
E
 NUMERATE THE
S
UBFOLDERS OF A
F
OLDER 
?..................................................108
 
H
OW TO
ENAME
A
LL THE
F
ILES IN A
F
OLDER 
?.............................................................109
 
 
Chapter 07 Scripting Quicktest Professional Page 2
Dani Vainstein Working with Files Page 2 of 112
C
AN I READ A TEXT FILE FROM THE BOTTOM UP
?............................................................109
 
H
OW CAN I COUNT THE NUMBER OF LINES IN A TEXT FILE
?.............................................110
 
H
OW CAN I COUNT THE NUMBER OF TIMES A WORD APPEARS IN A LOG FILE
?..................110
 
A
PPENDIX
5.A.....................................................................................................................111
 
Scripting Excel
Drives, files and folders are the lifeblood of any organization; this makes filesystem administration one of the most important responsibilities assigned tosystem administrators. Of course, file system administration is also one of themore difficult responsibilities to carry out, simply because files and folders arescattered on multiple hard disks and multiple computers throughout theorganization. Scripts can help make file system management much easier,particularly when the files and folders managed, are located on remote computers.
Creating and Terminating an Instance of MS-Excel
Let's start with the simplest possible script, one that creates an instance of Microsoft Excel and then adds a new workbook to that instance:
Set
oXlsApp =
CreateObject
("Excel.Application")oXlsAp.Workbooks.Add
By running the preceding script, you really did create a brand-new instance of Microsoft Excel. Press CTRL-ALT-DEL and take a look at the Processes tab in theTask Manager. You should see an instance of 
Excel.exe
 By default, any time you use a script to create an instance of a Microsoft Officeapplication, that application runs in a window that is not visible on screen. Excel isthere; you just can't see itThis is a real, live instance of Microsoft Excel. As you'll soon see, you canprogrammatically read data from it or, for that matter, do pretty much anythingelse you can do with Excel.The only functionality you lose when Excel runs in an invisible window is the abilityto type something on the keyboard and have the application reacts to thosekeystrokes. And that's what makes the default behavior useful.Suppose you were running a script that created a report using Excel, and supposeExcel was visible the whole time the script was running. A user (even yourself)could accidentally hit a key on the keyboard and ruin the entire report. A user(even yourself) could simply close Excel, ruining not only the report, but alsocausing your script to blow up. (After all, the script will be trying to sendcommands to an instance of Excel that no longer exists.) By running Excelinvisibly, you can sidestep problems like that.What if you would like Excel to be visible on screen? No problem just set theVisible property to True.
Set
oXlsApp =
CreateObject
("Excel.Application")oXlsAp.Workbooks.AddoXlsApp.Visible =
True
 
Chapter 07 Scripting Quicktest Professional Page 3
Dani Vainstein Working with Files Page 3 of 112
Wait 10MsgBox"The script is now complete." 
What happens when you run this script? Well, an instance of Excel will be created,and it will appear on your screen. There will be a 10-second pause, and then amessage will appear telling you that the script is now complete. When you clickOK, the script will immediately terminate (as soon as Microsoft® VBScript reachesthe end of a script, the script process terminates).
Returning a Collection of Disk Drives
Before you can manage disk drives on a computer, you need to know which diskdrives are actually available on that computer. The
FileSystemObject
allows youto return a collection of all the drives installed on a computer, including removabledrives and mapped network drives (in other words, any drive with a drive letter).To return this collection, create an instance of the
FileSystemObject
, and thencreate a reference to the
Drives
property. After the collection has been returned,you can use a
For
 
Each
loop to iterate through the collection.For example, the following script returns a collection of all the drives installed on acomputer and then echoes the drive letter for each drive in the collection.
Set
oFSO =
CreateObject
("Scripting.FileSystemObject")
Set
colDrives = oFSO.Drives
For
 
Each
oDrive
in
oDrivesCol
 MsgBox
 "Drive letter: "& oDrive.DriveLetter
 Next
 
Binding to a Specific Disk Drive
If you know in advance which drive you want to bind to (for example, drive C, orthe shared folder \\accounting\receivables), you can use the
GetDrive
method tobind directly to the drive. This allows you to retrieve information for a specificdrive, without having to return and iterate through an entire collection.The
GetDrive
method requires a single parameter: the driver letter of the drive orthe
UNC
path to the shared folder. To specify a drive letter, you can use any of the following formats:
CC:C:\
 
The following script creates an instance of the
FileSystemObject
, uses the
GetDrive
method to bind directly to drive C, and then echoes the amount of available space on the drive.
Set
oFSO =
CreateObject
("Scripting.FileSystemObject")
Set
 
oDrive = oFSO.GetDrive("C:")
 MsgBox
 "Available space: "& oDrive.AvailableSpace
 

You're Reading a Free Preview

Download
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->