Chapter 07 Scripting Quicktest Professional Page 1
Total Page:16
File Type:pdf, Size:1020Kb
Chapter 07 Scripting Quicktest Professional Page 1 SCRIPTING EXCEL ............................................................................................................... 2 CREATING AND TERMINATING AN INSTANCE OF MS-EXCEL ............................................... 2 RETURNING A COLLECTION OF DISK DRIVES ..................................................................... 3 BINDING TO A SPECIFIC DISK DRIVE ................................................................................. 3 ENUMERATING DISK DRIVE PROPERTIES ........................................................................... 4 ENSURING THAT A DRIVE IS READY .................................................................................. 4 MANAGING FOLDERS ............................................................................................................. 5 BINDING FOLDERS ............................................................................................................ 5 VERIFYING THAT A FOLDER EXISTS .................................................................................. 6 CREATING A FOLDER ........................................................................................................ 6 DELETING A FOLDER ......................................................................................................... 7 COPYING A FOLDER AND ITS CONTENTS ........................................................................... 8 MOVING A FOLDER AND ITS CONTENTS ............................................................................ 9 RENAMING A FOLDER ..................................................................................................... 10 ENUMERATING FOLDER PROPERTIES ............................................................................... 11 MANAGING FOLDER ATTRIBUTES ................................................................................... 12 MANAGING FILES ................................................................................................................ 18 BINDING TO A FILE ......................................................................................................... 18 VERIFYING THAT A FILE EXISTS ..................................................................................... 19 DELETING A FILE ............................................................................................................ 20 COPYING A FILE .............................................................................................................. 21 MOVING A FILE ............................................................................................................... 22 RENAME A FILE .............................................................................................................. 23 MANAGING FILE PROPERTIES .......................................................................................... 23 READING AND WRITING TEXT FILES .................................................................................. 27 READING TEXT FILES ...................................................................................................... 30 WRITE TO TEXT FILES ..................................................................................................... 34 MANAGING FILES AND FOLDERS USING WMI.................................................................... 36 COMPARING WMI AND THE FILE SYSTEM OBJECT ............................................................ 37 MANAGING FILES AND FOLDERS USING THE WINDOWS SHELL OBJECT ........................... 40 FOLDERS AND FOLDERS OBJECT ......................................................................................... 41 WIN 32_D IRECTORY CLASS ............................................................................................. 41 THE FILE SYSTEM OBJECT OBJECT ...................................................................................... 51 THE FSO OBJECT MODEL ............................................................................................... 51 PROGRAMMING THE FILE SYSTEM OBJECT ....................................................................... 53 THE FILE SYSTEM OBJECT PROPERTIES AND METHODS ..................................................... 53 DRIVES COLLECTION OBJECT ......................................................................................... 74 DRIVE OBJECT ................................................................................................................ 75 FOLDERS COLLECTION OBJECT ....................................................................................... 80 FOLDER OBJECT .............................................................................................................. 83 FILES COLLECTION OBJECT ............................................................................................. 91 FILE OBJECT ................................................................................................................... 93 TEXT STREAM OBJECT ....................................................................................................101 Q&A ...................................................................................................................................106 HOW TO ENUMERATE FOLDERS AND FOLDER PROPERTIES ? ...........................................106 HOW TO ENUMERATE ALL THE FOLDERS ON A COMPUTER ?...........................................108 HOW TO ENUMERATE THE SUBFOLDERS OF A FOLDER ?..................................................108 HOW TO RENAME ALL THE FILES IN A FOLDER ?.............................................................109 Dani Vainstein Working with Files Page 1 of 112 Chapter 07 Scripting Quicktest Professional Page 2 CAN I READ A TEXT FILE FROM THE BOTTOM UP ?............................................................109 HOW CAN I COUNT THE NUMBER OF LINES IN A TEXT FILE ?.............................................110 HOW CAN I COUNT THE NUMBER OF TIMES A WORD APPEARS IN A LOG FILE ?..................110 APPENDIX 5.A .....................................................................................................................111 Scripting Excel Drives, files and folders are the lifeblood of any organization; this makes file system administration one of the most important responsibilities assigned to system administrators. Of course, file system administration is also one of the more difficult responsibilities to carry out, simply because files and folders are scattered on multiple hard disks and multiple computers throughout the organization. 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 the Task Manager. You should see an instance of Excel.exe By default, any time you use a script to create an instance of a Microsoft Office application, that application runs in a window that is not visible on screen. Excel is there; you just can't see it This is a real, live instance of Microsoft Excel. As you'll soon see, you can programmatically read data from it or, for that matter, do pretty much anything else you can do with Excel. The only functionality you lose when Excel runs in an invisible window is the ability to type something on the keyboard and have the application reacts to those keystrokes. And that's what makes the default behavior useful. Suppose you were running a script that created a report using Excel, and suppose Excel 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 also causing your script to blow up. (After all, the script will be trying to send commands to an instance of Excel that no longer exists.) By running Excel invisibly, you can sidestep problems like that. What if you would like Excel to be visible on screen? No problem just set the Visible property to True. Set oXlsApp = CreateObject ("Excel.Application" ) oXlsAp.Workbooks.Add oXlsApp.Visible = True Dani Vainstein Working with Files Page 2 of 112 Chapter 07 Scripting Quicktest Professional Page 3 Wait 10 MsgBox "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 a message will appear telling you that the script is now complete. When you click OK, the script will immediately terminate (as soon as Microsoft® VBScript reaches the 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 disk drives are actually available on that computer. The FileSystemObject allows you to return a collection of all the drives installed on a computer, including removable drives and mapped network drives (in other words, any drive with a drive letter). To return this collection, create an instance of the FileSystemObject , and then create a reference to the Drives property.