Microsoft _ icrosoft Script Host 2.0 Developer's Guide
Günter Born Contents
Introduction xv parti Introduction to the World of Script Programming
chapter i Introduction to Windows Script Host 3
WHAT YOU CAN DO WITH WSH SCRIPTS 4 A Few Remarks About VBScript and JScript 5 UPGRADING TO WSH 2 5
CREATING AND USING SCRIPTS 7 Creating Your First Script in VBScript 7 Creating the Same Script in JScript 9 Executing WSH Scripts 10 Submitting Arguments to a Script 14 The New .wsf File Format 19 Including External Files 20 Using Type Libraries 20 Debugging Features 21
chapter 2 Development Tools and Techniques 23
CREATING SCRIPTS 23 Using Script Templates 24
EDITING SCRIPTS 26 Invoking Script Editors 26
INSTALLING AND UNINSTALLING ACTIVEX CONTROLS 34 Registering a Control 34 Uninstalling a Control 35
DEBUGGING SCRIPTS 36 Tracing Your Programs 37 Using the Microsoft Script Debugger 39 Contents
chapter з Introduction to Programming with Objects 45
OBJECTS 45 A Simple Object 46 The Object Model 46 Collections 47 Methods 48 A Practical Example 49 AVAILABLE OBJECTS 49 Getting Information About Objects, Methods, and Properties 50
chapter 4 Introduction to VBScript 51
BASIC FEATURES 51 Statements, Continued Lines, and Comments 51 Constants and Variables 54 Operators 63 Control Structures 67 Loops 69 Functions and Procedures 71 ADVANCED FEATURES 80 Error Handling 80 Regular Expressions (in VBScript 5.0 and Later) 82 Classes (in VBScript 5.0) 85 Using the With Statement (in VBScript 5.0) 89
chapter 5 Introduction to JScript 91
WHAT is JSCRIPT? 91
THE STRUCTURE OF A JSCRIPT PROGRAM 92 Comments 93 Statements 93 Constants 94 Variables 94 Escape Sequences in Strings 98 OPERATORS 99 Assignment Operator 100 Comparison Operators 100 Calculation Operators 100 Contents
Increment and Decrement Operators 101 Logical Operators 102 Operator Precedence 103 CONTROL STRUCTURES 104 if Statement 104 Conditional Operator 105 for Loop 105 for...in Loop 107 while Loop 107 do...while Loop 108 switch Statement 109 break and continue Keywords 110 FUNCTIONS AND OBJECTS 110 User-Defined Functions 110 Built-in Functions 111 Objects 111 Arrays 112 Part и interactive Scripting chapter 6 Creating Simple Dialog Boxes in WSH 117
USING THE ECHO METHOD 117 Using Echo in VBScript 118 Using Echo in the Command Prompt Window 119 Using Echo in JScript 120 Using a Line Feed with Echo 121
USING THE MSGBOX FUNCTION IN VBSCRIPT 122 Defining the Icon and Buttons in a Dialog Box 125 Setting the Focus on a Button 127 Determining Which Button Was Used to Close a Dialog Box 128 Example: A Welcome Login Message in VBScript 130
USING THE POPUP METHOD 134 Using Popup in VBScript 137 Another JScript Example Using Popup 138 Contents
chapter 7 Working with WSH Objects 141
USING THE WSCRIPT OBJECT 141 Retrieving WSH and Script Properties 142 Retrieving Language Engine Properties 145 Accessing Script Arguments 147 ACCESSING ENVIRONMENT VARIABLES 152 Accessing Environment Variables in a Script 152 CREATING AND RELEASING OBJECTS 165 CreateObject vs. GetObject 167 Using DisconnectObject 168 LAUNCHING OTHER PROGRAMS FROM A SCRIPT 170 Launching Notepad from VBScript 173 Launching Calculator from JScript 174 Paths Containing Blanks 175 The Quit Method 176 Using the Run Method to Execute MS-DOS Commands 180
chapter 8 Retrieving User Input in WSH Scripts 183
INVOKING AN INPUT DIALOG BOX IN VBSCRIPT 183
INVOKING AN INPUT DIALOG BOX IN JSCRIPT 186 A WSH 2 Solution: Combining VBScript and JScript 186 Using the prompt Method from Internet Explorer 188
chapter 9 Working with Forms 195
USING INTERNET EXPLORER TO CREATE AN ABOUT DIALOG BOX 195 Using the showModalDialog Method to Display an HTML File 196 Displaying the About Dialog Box Using VBScript 199 Displaying the About Dialog Box Using JScript 201
USING INTERNET EXPLORER AND WSH то CREATE A FORM 202 HTML Code for the Form 202 Displaying the Form 203 A JScript WSH Script for Displaying the Form 207 Displaying a File Selection Dialog Box 209 Improving the Form 212 Contents
Part in Power Scripting chapter 10 Creating Shortcuts 225
SHORTCUT BASICS 225 Using the CreateShortcut Method 226
SHORTCUTS: BEYOND THE BASICS 231 Using the SpecialFolders Object 231 Creating a Shortcut on the Desktop 238 Creating a Shortcut on the Start Menu 242 Creating a Shortcut Using Arguments 248 Reading Shortcut Properties 253 Updating a Shortcut 259 Creating a Shortcut to a Web Site 263
chapter li Using Advanced WSH Features 267
RETRIEVING THE USER, DOMAIN, OR COMPUTER NAME 267 The JScript Implementation 269
CONNECTING TO A NETWORK PRINTER 270 Printer Mapping Using AddWindowsPrinterConnection 271 Printer Mapping Using AddPrinterConnection 272 Removing a Printer Mapping 276 Listing All Mapped Printers 278 Setting the Default Printer 279
MAPPING NETWORK DRIVES 281 The JScript Implementation 284 Logon Scripts 286 ACCESSING THE WINDOWS REGISTRY 286 Accessing the Registry in WSH 288 Accessing the Registry in VBScript 290 Run-Time Error Handling for Registry Access 293 Checking the Existence of a Key 296 Techniques for Registry Access in WSH 299 Enumerating Registry Keys and Values 305 Accessing the Registry Remotely 307 Changing the Windows 98 Installation Path 309 Hiding the Last User Name at Logon 311 Retrieving the Workgroup Name in Windows 98 312 chapter 12 Using File System and I/O Commands 317
THE FILESYSTEMOBJECT OBJECT MODEL 317 Creating a FileSystemObject Object 318 Methods of the FileSystemObject Object 319
ACCESSING DRIVES 320 Listing All Drives on a Machine 320 Showing Drive Properties 325 ACCESSING FILES AND FOLDERS 331 Listing All Subfolders in a Folder 332 Creating, Moving, Renaming, and Deleting Folders 335 Listing All Files in a Folder 343 Retrieving File Attributes and Dates 347 Copying and Deleting Files 355 Backing Up Folders 359
ACCESSING TEXT FILES 365 Reading from a Text File 365 Pitfalls of Using Dialog Boxes and Browser Windows to View Text Files 368 Writing to a Text File 376 Appending New Text to an Existing File 380 Replacing Text in a File 382 USING THE BROWSE FOR FOLDER DIALOG BOX 385 Using BrowseForFolder to Select Folders 386 Using BrowseForFolder to Select Files 391 chapter i3 Controlling Windows and Applications from Scripts 397
DELAYING SCRIPT EXECUTION 397 Using the Sleep Method to Reduce the CPU Load 398 Using a Delay to Solve a Problem with Asynchronous Processes 399 ACTIVATING AN APPLICATION USING THE APPACTIVATE METHOD 401 Pitfalls of Using the AppActivate Method 404 USING THE SENDKEYS METHOD TO SIMULATE KEYSTROKES 406 Using SendKeys in WSH 2 406 Manipulating Two Applications Using SendKeys 410 Contents
CUSTOMIZING WINDOWS USING LOGON SCRIPTS 413 Setting Up a Logon Script 413 Using Global Logon and Logoff Scripts 415 Using Startup and Shutdown Scripts 416 Startup and Logon Scripts in Windows 95 and Windows 98 417 chapter 14 Programming Techniques and Tips 419
RUN-TIME ERRORS 419 Handling Run-Time Errors in VBScript 419 Handling Run-Time Errors in JScript 420 Raising a Run-Time Error in VBScript 421
PATHS AND DATES 421 Getting the Script's Path 421 Getting the Current Directory 422 Setting the Default Folder 423 Getting the Current Drive Name 423 Calculating Date Differences 424
LONG FILENAMES, THE AT COMMAND, AND SYSTEM CALLS 425 Using the Windows NT Scheduler to Execute WSH Scripts 425 Using Long Filenames in Scripts 425 Using the Run Method to Execute System Calls 426 Locking a Windows 2000 Workstation 429 Invoking the Copy Disk Dialog Box 430 Invoking the Format Dialog Box 431 Invoking the Screen Saver Property Page 432 Calling Control Panel Modules 432 Using the Run Method to Handle Network Mappings 434
USER DIALOG BOXES AND OUTPUT 434 Using Tabs and Line Feeds 434 Displaying Console Input and Output 435 Writing to a Line and Reading from It 437 Piping Program Output 439 Using Files for Streaming 440 Logging Script Output 443 Printing from a WSH Script 446 Contents
FILE HANDLING Checking Whether a File or Folder Exists Checking Whether a Folder is Empty Checking Whether an Access Database Is in Use Copying a File Renaming a File or Folder Searching for a File Listing All Shortcut Files
PLAYING SOUND Using Internet Explorer to Play Sound
THE WINDOWS SHELL Testing the Shell Version Arranging the Desktop Windows Opening Folder Windows Accessing Windows Shell Dialog Boxes
CALLING A DUN CONNECTION pan IV Appendixes
Appendix A WSH Resources on the Internet
Appendix в Script Security
PARTIALLY DISABLING WSH
PREVENTING AUTOMATIC EXECUTION OF WSH SCRIPTS
SECURITY SETTINGS FOR WSH SCRIPTS
CONCLUDING REMARKS
Index