ebook img

Microsoft WSH and VBScript Programming for the Absolute Beginner PDF

520 Pages·2005·7.98 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Microsoft WSH and VBScript Programming for the Absolute Beginner

Microsoft WSH ® and VBScript Programming for the Absolute Beginner Second Edition JERRY LEE FORD, JR. © 2005 by Thomson Course Technology PTR. All rights reserved. No Publisher and part of this book may be reproduced or transmitted in any form or by General Manager of PTR: any means, electronic or mechanical, including photocopying, record- Stacy L. Hiquet ing, or by any information storage or retrieval system without written Associate Director of Marketing: permission from Thomson Course Technology PTR, except for the Sarah O’Donnell inclusion of brief quotations in a review. Marketing Manager: The Thomson Course Technology PTR logo and related trade dress are Heather Hurley trademarks of Thomson Course Technology PTR and may not be used without written permission. Manager of Editorial Services: Heather Talbot Microsoft, Windows, VBScript, and Windows Script Host and ActiveX are trademarks or registered trademarks of Microsoft Corporation. Acquisitions Editor: Mitzi Koontz All other trademarks are the property of their respective owners. Senior Editor: Important:Thomson Course Technology PTR cannot provide software Mark Garvey support. Please contact the appropriate software manufacturer’s tech- nical support line or Web site for assistance. Marketing Coordinator: Jordan Casey Thomson Course Technology PTR and the author have attempted throughout this book to distinguish proprietary trademarks from Project Editor: descriptive terms by following the capitalization style used by the Tarida Anantachai, manufacturer. Argosy Publishing Information contained in this book has been obtained by Thomson Technical Reviewer: Course Technology PTR from sources believed to be reliable. However, Zac Hester because of the possibility of human or mechanical error by our sources, Thomson Course Technology PTR, or others, the Publisher PTR Editorial does not guarantee the accuracy, adequacy, or completeness of any Services Coordinator: information and is not responsible for any errors or omissions or the Elizabeth Furbish results obtained from use of such information. Readers should be par- Copy Editor: ticularly aware of the fact that the Internet is an ever-changing entity. Linda Seifert Some facts may have changed since this book went to press. Interior Layout Tech: Educational facilities, companies, and organizations interested in mul- Shawn Morningstar tiple copies or licensing of this book should contact the publisher for quantity discount information. Training manuals, CD-ROMs, and por- Cover Designer: tions of this book are also available individually or can be tailored for Mike Tanamachi specific needs. Indexer: ISBN: 1-59200-731-7 Linda Buskus Library of Congress Catalog Card Number: 2004114912 Proofreader: Jan Cocker Printed in the United States of America 05 06 07 08 09 BH 10 9 8 7 6 5 4 3 2 1 Thomson Course Technology PTR, a division of Thomson Course Technology 25 Thomson Place Boston, MA 02210 http://www.courseptr.com To Alexander, William, Molly, and Mary. Acknowledgments T he second edition of Microsoft WSH and VBScript Programming for the Absolute Beginnerrepresents the culmination of efforts provided by a number of individuals over the last two years. Todd Jensen served as the acquisitions editor for the first edition. Estelle Manticas and Zac Hester were also key contributors to the first edition. Estelle served as both the book’s develop- ment and copy editor and Zac provided valuable guidance as technical editor. I also want to acknowledge Mitzi Koontz for serving as the acquisitions editor for the second edition of the book. Special recognition also goes to Linda Seifert for her copyediting services. I’d be remiss if I did not thank Zac Hester one more time for returning as technical editor. Finally, I’d like to thank everyone else at Course PTR for all their hard work. About the Author J erry Lee Ford, Jr.is an author, educator, and IT professional with over 16 years’ experience in information technology, including roles as an automation analyst, technical manager, technical support analyst, automation engineer, and security analyst. Jerry has a master’s degree in business administration from Virginia Commonwealth University in Richmond, Virginia. He also is the author of 13 other books, including VBScript Professional Projects, Microsoft Windows Shell Scripting and WSH Administrator’s Guide, Learn VBScript in a Weekend, Microsoft Windows Shell Scripting for the Absolute Beginner, Learn JavaScript in a Weekend, and Microsoft Windows XP Profes- sional Administrator’s Guide. He has over five years’ experience as an adjunct instructor teaching networking courses in Information Technology. Jerry lives in Richmond, Virginia, with his wife, Mary, and their children William, Alexander, and Molly. This page intentionally left blank Contents Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv PART I Introducing the WSH and VBScript . . . . . . . . . . . . .1 Getting Started with the WSH and VBScript . . . . .3 Chapter 1 Project Preview: The Knock Knock Game. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 What Is the WSH? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 WSH Scripting Engines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Selecting a WSH Script Execution Host. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Introducing the WSH Core Object Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 How Does the WSH Compare to Windows Shell Scripting? . . . . . . . . . . . . . . 8 Understanding How the Windows Shell Works. . . . . . . . . . . . . . . . . . . . . . . . . 9 How Does It All Work?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Operating System Compatibility. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 How Do You Install It? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 How Does It Work with VBScript?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 What Other Scripting Languages Does the WSH Support?. . . . . . . . . . . . . . 18 Introducing VBScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 VBScript Capabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 VBScript’s Roots. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 VBScript’s Cousins: Visual Basic and VBA. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Microsoft Scripting Technologies Web Page. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Back to the Knock Knock Game. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Designing the Game. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 The Final Result. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Overview of the Windows Script Host . . . . . . . . .35 Chapter 2 Project Preview: The Rock, Paper, and Scissors Game. . . . . . . . . . . . . . . . . . . . . . 36 A Detailed Examination of WSH Components. . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 The Core Object Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 WSH Objects and Their Properties and Methods. . . . . . . . . . . . . . . . . . . . . . . 39 viii Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition Configuring WSH Execution Hosts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Configuring WScript and CScript Command-Line Execution. . . . . . . . . . . . 51 Configuring WScript Desktop Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Overriding Command-Line Host Execution Settings . . . . . . . . . . . . . . . . . . . 54 Customizing WScript Settings for Individual Desktop Scripts. . . . . . . . . . . 56 Back to the Rock, Paper, and Scissors Game. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Designing the Game. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 The Final Result. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 PART II Learning VBScript & WSH Scripting . . . . . . . . . . .63 VBScript Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .65 Chapter 3 Project Preview: The Math Game. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 VBScript Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 VBScript Syntax Rules. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Reserved Characters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Adding Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Mastering the VBScript Object Model. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Working with VBScript Run-Time Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Using VBScript Run-Time Objects in Your Scripts. . . . . . . . . . . . . . . . . . . . . . . . . 79 Examining Built-in VBScript Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Demo: The Square Root Calculator. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Demo: A New and Improved Square Root Calculator. . . . . . . . . . . . . . . . . . . 82 Displaying Script Output. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 The WScript’s Echo() Method. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 The WshShell Object’s Popup() Method. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 The VBScript InputBox() Function. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 The VBScript MsgBox() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Back to the Math Game. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 A Quick Overview of the WshShell SendKeys() Method. . . . . . . . . . . . . . . . . 89 Designing the Game. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 The Final Result. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Contents ix Constants,Variables, and Arrays . . . . . . . . . . . . .101 Chapter 4 Project Preview: The Story of Captain Adventure. . . . . . . . . . . . . . . . . . . . . . . . . 101 Understanding How Scripts View Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 Working with Data That Never Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 Assigning Data to Constants. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 VBScript Run-Time Constants. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 Storing Data That Changes During Script Execution . . . . . . . . . . . . . . . . . . . . . 111 VBScript Data Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Defining Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 Variable Naming Rules. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Variable Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 Modifying Variable Values with Expressions . . . . . . . . . . . . . . . . . . . . . . . . . 117 Using the WSH to Work with Environment Variables. . . . . . . . . . . . . . . . . 121 Working with Collections of Related Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 Single-Dimension Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 Multiple-Dimension Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 Processing Array Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Getting a Handle on the Size of Your Arrays . . . . . . . . . . . . . . . . . . . . . . . . . 129 Resizing Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 Building Dynamic Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 Erasing Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 Processing Data Passed to a Script at Run-Time. . . . . . . . . . . . . . . . . . . . . . . . . . 135 Passing Arguments to Scripts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 Designing Scripts That Accept Argument Input . . . . . . . . . . . . . . . . . . . . . . 136 Back to the Story of Captain Adventure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 Designing the Game. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 The Final Result. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 Conditional Logic . . . . . . . . . . . . . . . . . . . . . . . . . . .145 Chapter 5 Project Preview: The Star Trek Quiz Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 Examining Program Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 The If Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 The Select Case Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 Performing More Complex Tests with VBScript Operators. . . . . . . . . . . . . . . . . 161 Back to the Star TrekQuiz Game. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 Game Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 The Fully Assembled Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172

Description:
"Microsoft WSH and VBScript for the Absolute Beginner, Second Edition," fills the need for a beginner level text on the WSH programming environment and the VBScript scripting language, and is a follow up to the best selling first edition. In recent years, programming has experienced a surge unlike a
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.