Microsoft WSH and VBScript Programming for the Absolute Beginner Part 1 - Pdf 16


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

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

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

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


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status