Tải bản đầy đủ (.pdf) (10 trang)

Microsoft WSH and VBScript Programming for the Absolute Beginner Part 1 ppsx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.66 MB, 10 trang )

JERRY LEE FORD, JR.
Microsoft
®
WSH
and VBScript
Programming
for the Absolute
Beginner
Second Edition
© 2005 by Thomson Course Technology PTR. All rights reserved. No
part of this book may be reproduced or transmitted in any form or by
any means, electronic or mechanical, including photocopying, record-
ing, or by any information storage or retrieval system without written
permission from Thomson Course Technology PTR, except for the
inclusion of brief quotations in a review.
The Thomson Course Technology PTR logo and related trade dress are
trademarks of Thomson Course Technology PTR and may not be used
without written permission.
Microsoft, Windows, VBScript, and Windows Script Host and ActiveX
are trademarks or registered trademarks of Microsoft Corporation.
All other trademarks are the property of their respective owners.
Important:
Thomson Course Technology PTR cannot provide software
support. Please contact the appropriate software manufacturer’s tech-
nical support line or Web site for assistance.
Thomson Course Technology PTR and the author have attempted
throughout this book to distinguish proprietary trademarks from
descriptive terms by following the capitalization style used by the
manufacturer.
Information contained in this book has been obtained by Thomson


Course Technology PTR from sources believed to be reliable. However,
because of the possibility of human or mechanical error by our
sources, Thomson Course Technology PTR, or others, the Publisher
does not guarantee the accuracy, adequacy, or completeness of any
information and is not responsible for any errors or omissions or the
results obtained from use of such information. Readers should be par-
ticularly aware of the fact that the Internet is an ever-changing entity.
Some facts may have changed since this book went to press.
Educational facilities, companies, and organizations interested in mul-
tiple copies or licensing of this book should contact the publisher for
quantity discount information. Training manuals, CD-ROMs, and por-
tions of this book are also available individually or can be tailored for
specific needs.
ISBN: 1-59200-731-7
Library of Congress Catalog Card Number: 2004114912
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

Publisher and
General Manager of PTR:
Stacy L. Hiquet
Associate Director of Marketing:
Sarah O’Donnell
Marketing Manager:
Heather Hurley
Manager of Editorial Services:

Heather Talbot
Acquisitions Editor:
Mitzi Koontz
Senior Editor:
Mark Garvey
Marketing Coordinator:
Jordan Casey
Project Editor:
Tarida Anantachai,
Argosy Publishing
Technical Reviewer:
Zac Hester
PTR Editorial
Services Coordinator:
Elizabeth Furbish
Copy Editor:
Linda Seifert
Interior Layout Tech:
Shawn Morningstar
Cover Designer:
Mike Tanamachi
Indexer:
Linda Buskus
Proofreader:
Jan Cocker
To Alexander, William, Molly, and Mary.
T
he second edition of
Microsoft WSH and VBScript Programming for the
Absolute Beginner

represents 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.
Acknowledgments
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.
About the Author
This page intentionally left blank
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
PART I Introducing the WSH and VBScript . . . . . . . . . . . . .1
Chapter 1 Getting Started with the WSH and VBScript . . . . .3
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
Chapter 2 Overview of the Windows Script Host . . . . . . . . .35
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
Contents
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
Chapter 3 VBScript Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .65
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
viii
Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition
Chapter 4 Constants,Variables, and Arrays . . . . . . . . . . . . .101
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
Chapter 5 Conditional Logic . . . . . . . . . . . . . . . . . . . . . . . . . . .145
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 Trek Quiz Game. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Game Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
The Fully Assembled Script. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
ix
Contents

×