Back to description
A chapter covering the basics of VBScript is the best place to begin this book. This is because of the type of language VBScript... more
A chapter covering the basics of VBScript is the best place to begin this book. This is because of the type of language VBScript is and the kind of users the authors see turning to it. In this chapter, you get a crash course in programming basics. You might not need this chapter because you’ve come to VBScript with programming skills from another language (Visual Basic, Visual Basic .NET, C, C++, Delphi, C#) and are already both familiar with and comfortable using programming terminology. In that case, feel free to skip this chapter and move on to the next one. However, if you come from a non-programming background, then this chapter will give you the firm foundation you need to begin using VBScript confidently.
If you’re still reading, chances are you fall into one of three distinct categories:
You’re a Network/Systems administrator who probably wants to use VBScript and the Windows Script Host or PowerShell to write logon scripts or to automate administration tasks.
You might be a web designer who feels the need to branch out and increase your skill set, perhaps in order to do some ASP work.
You’re interested in programming (possibly Visual Basic or Visual Basic .NET) and want to check it out before getting too deeply involved.
Programming is a massive subject. Over the years countless volumes have been written about it, both in print and on the Internet. In this chapter, in a single paragraph, we might end up introducing several unfamiliar concepts. We’ll be moving pretty fast, but if you read along carefully, trying out your hand at the examples along the way, you’ll be just fine.
Also, do bear in mind that there will be a lot that we don’t cover here, such as:
Architecture
System design
Database design
Documenting code
Advanced testing, debugging, and beta testing
Rollout and support
Think of this chapter as a brief introduction to the important building blocks of programming. It certainly won’t make you an expert programmer overnight, but it will hopefully give you the know-how you’ll need to get the most out of the rest of the book.
... less
VBScript (or Microsoft’s Visual Basic Scripting Edition) is a powerful interpreted scripting language that brings active... more
VBScript (or Microsoft’s Visual Basic Scripting Edition) is a powerful interpreted scripting language that brings active scripting to a variety of environments, both client and server side. But VBScript is part of a bigger programming worldthe world of Visual Basic.
This chapter gives you a peek into this bigger programming world and shows you how VBScript fits in with the bigger picture. As the chapter name suggests, you’ll look at what VBScript is and also what it isn’t (this, hopefully, will dispel any myths that you might have read about VBScript).
Before you go any further, you should spend a little time clearing up a few points and getting the terminology, not just that of VBScript, but also that of related terminology, clear.
This chapter introduces VBScript data types, which, jumping ahead a little, is linked to the subject of the next chapter,... more
This chapter introduces VBScript data types, which, jumping ahead a little, is linked to the subject of the next chapter, “Variables and Procedures.” The concepts of variables and data types are closely related. A variable is a name given to a location in memory where some data used by a program is stored. For example, a program that manages your music collection might have a variable called Artist that might store the value “James Brown”. The variable named Artist is a pointer to a location in the computer’s memory where the value “James Brown” is stored. (Lucky for us, for the most part VBScript keeps us from having to worry about things like pointers and memory.)
Artist
“James Brown”
Variables can hold different types of data: numbers, dates, text, and other more specialized, or complex, categories. These categories into which values can be divided are called data types.
A full discussion of programming language design relative to the strengths and weaknesses of, and alternate techniques for, the use of data types is out of the scope of this book. Suffice to say that in VBScript programming, data types help simplify the logic of a programming language compiler and also help ensure proper and correct results during the program’s execution. Even if you did not know a lot about how compilers work, you could imagine that the instructions given to your computer for adding numbers together, computing the length of time between two dates, and searching a long string of text for the occurrence of the word “apple” would be much different from each other. Data types help the compiler figure out what you’re trying to make your program do.
Here is why this chapter is important, even if you’re an experienced programmer in other languages: While your success as a VBScript programmer does not depend on your understanding of low-level details such as compilers and machine instructions, it is critical to understand how VBScript handles data types and variables, including the particulars of VBScript’s “universal” data type, the Variant. VBScript has some features and behaviors that are unique and, on the surface, confusing.
Variant
In this chapter, the discussion of VBScript variables continues and also expands to include VBScript procedures and functions... more
In this chapter, the discussion of VBScript variables continues and also expands to include VBScript procedures and functions. Some important variable-specific topics have not been discussed yet, including rules for naming and declaring variables, the important Option Explicit statement, and the concepts of variable scope and lifetime. You also learn the syntax for defining procedures and functions, including arguments and return values, and get introduced to some “design strategies” for your scripts.
Option
Explicit
If you are already an experienced programmer in another language and tempted to skip this chapter, you may try just skimming it instead. Even where the material is rudimentary programming-wise you will pick up some useful information that is particular to VBScript.
The VBScript language provides certain mechanisms to allow you to manipulate the execution of the code in your script. For... more
The VBScript language provides certain mechanisms to allow you to manipulate the execution of the code in your script. For example, you can use branching logic to skip some lines of code. You can also execute some lines of code multiple times through the use of looping logic. The common term for the way in which you use these techniques is called control of flow.
Branching logic is implemented in VBScript with statements such as If, Else, and Select Case. Loops are defined with the For, Do, and While blocks. The sections in this chapter prepare you with all of the information you need on branching and looping, which are as essential to programming as variables. If you are relatively new to programming, this is an important chapter. Like all of the chapters up to this point, this chapter explains essential programming fundamentals while also teaching you the VBScript-specific techniques and syntax.
If
Else
Select Case
For
Do
While
If you are an experienced programmer in another language, you might only skim this chapter for some of the VBScript particulars. VBScript’s branching and looping capabilities are basically the same as any mature procedural language, and are virtually identical to Visual Basic’s. If you are looking only for syntax details, the language reference in Appendix A might be your best source of information.
No explanation or tutorial of a programming language is complete without thorough coverage of error handling... more
No explanation or tutorial of a programming language is complete without thorough coverage of error handling (also known as exception handling). It is of course important to learn the syntax of a language and to use correct logic in your programs. What truly gives a program or script professional polishwhat separates throwaway from production qualityis error handling.
Writing a computer program, even a simple one, is a delicate matter. You have to get the syntax exactly right. You have to place the quote marks and the parentheses just so. You have to name the files in a particular way. You have to follow a certain kind of logic.
What’s more, your program does not exist in a vacuum. A VBScript program can interact directly or indirectly with the scripting host, the operating system, the user, the network, and the Internet. A script is beset by the possibility of full disks, invalid user entries, scrambled file formats, and the electricity suddenly dropping out. Things can, and will, go wrong.
Error handling is the programmer’s main line of defense against this inherent unpredictability. The term error handling refers not only to how a program responds when an error occurs, but also to how it prevents errors from happening in the first place.
The topic of debugging goes hand in hand with that of error handling. Debugging, as the name suggests, is the process of detecting, locating, and removing bugs from a program. The removing part of that process is most often the easiest part. The real art of debugging is in the use of tools and techniques for finding a bug in the first place. Basic proficiency with debugging brings with it a reduction in frustration and an increase in productivity. If you are charging your clients by the hour, or if you are working in a high-pressure situation, the ability to track down a bug quickly is of key importance.
This chapter delves into the closely related subjects of error handling and debugging. If you are a new programmer, this chapter explains not only the VBScript mechanics of error handling and debugging, but also the universal principles and techniques at work. If you are an experienced programmer in other programming languages, this chapter is still worth your while; error handling and debugging in VBScript are quite unique and likely different from your experience with other languages. In fact, it’s fair to characterize error handling as VBScript’s primary area of weakness.
Note
Note that error handling associated with the Script Control (Chapter 21) is slightly different than with other VBScript hosts; in the Script Control you can also allow the host application to handle runtime errors. For more specific information, see Chapter 21.
This chapter introduces some powerful objects that are available for use in your VBScript code. You can think of these as... more
This chapter introduces some powerful objects that are available for use in your VBScript code. You can think of these as utility objects, because they are designed to be reusable in a variety of situations. This chapter also introduces the Dictionary object, which is a useful and more versatile replacement for arrays, as well as the family of objects in the FileSystemObject hierarchy. The objects in the FileSystemObject family offer pretty much everything you need for interacting with the Windows file system.
Dictionary
FileSystemObject
This chapter starts off with a brief overview of the basic syntax, rules, and recommendations for using objects in VBScript. For those who have been working through the previous chapters on VBScript fundamentals, the first sections of this chapter continues along that course. The rest of the chapter introduces some powerful objects you will find useful in many of your scripts. If you are already familiar with the basics of using objects in VBScript, and if you are looking primarily for how-to information on the runtime objects, you may want to skip ahead to those sections of the chapter.
Even though the feature has existed for some time, most people don’t know that you can define classes in VBScript. This gives... more
Even though the feature has existed for some time, most people don’t know that you can define classes in VBScript. This gives VBScript programmers a powerful tool, especially in more extensive script-based projects. Granted, classes defined in VBScript do not have the more properly object-oriented capability of Java, C++, VB.NET, or even Visual Basic 6, but they do let the VBScript programmer take advantage of a few of the object oriented benefits available when programming in those other languages.
If you’ve skipped previous chapters and are not familiar with how to use COM objects from VBScript, then you might benefit from reading the first sections of Chapter 7 before tackling this chapter. This chapter assumes that you are familiar with the basics of instantiating objects and calling their properties and methods.
Version 5 and later of VBScript fully support regular expressions. Before that, this was one feature that was sorely lacking... more
Version 5 and later of VBScript fully support regular expressions. Before that, this was one feature that was sorely lacking within VBScript, and one that made it inferior to other scripting languages, including JavaScript. This chapter begins with a brief introduction to regular expressions before taking a look at what regular expressions have to offer the programmer. It then looks at how to make use of regular expressions in VBScript code.
In this chapter, you take VBScript straight to your web site visitor’s browser. You take a look at how VBScript and Internet... more
In this chapter, you take VBScript straight to your web site visitor’s browser. You take a look at how VBScript and Internet Explorer can be combined on the client side to create interesting and exciting HTML pages for your visitors.
Going straight for the client side is the easiest, now-tech, no-special-server needed way to get VBScript-enabled pages to the visitor. You can do it using any server, with no Active Server Pages (ASP) needed.
In this chapter you learn what you need to deliver VBScript-enabled content straight to the browser, as well as find out how it works and what you can do with it. You start by exploring the tools you’ll need to write client-side VBScript.
Windows Vista brings with it a range of new features for both users and developers. One new feature is the Windows Sidebar... more
Windows Vista brings with it a range of new features for both users and developers. One new feature is the Windows Sidebar. Windows Sidebar is the application that allows users to run gadgets on Windows desktop or docked together as part of the Sidebar window. It also allows users to manage gadgets in the Gadget Gallery.
Gadgets might seem complicated at first but they're not (well, they can be but that depends on what you want the gadget to do). Gadgets are created using two things that VBScript developers know well: HTML and script. Gadgets have access to information about its own state and settings as well as about Windows. This means that a gadget can interact with files and folders on the host system. Gadgets can also display settings dialog boxes and store user specific settings using the System.Gadget object.
System.Gadget
The Windows Task Scheduler allows the user or administrator to schedule a program to be run at a particular time or in response... more
The Windows Task Scheduler allows the user or administrator to schedule a program to be run at a particular time or in response to a specific event. Task Scheduler can also be controlled via script. Windows Vista and Windows Server 2008 ship with the new redesigned Task Scheduler 2.0 and in this chapter you’ll see the changes made to Task Scheduler and how to make use of it. Here are some of the new features and capabilities:
Sends automatic email notifications when there are problems with the local or remote system
Launches new diagnostic programs when a problem with Task Scheduler or a scheduled task is detected
Can execute tasks when the PC is unattended
Offers the ability to run tasks in a particular sequence
Carries out actions based on an event logged in the system event log
Configures systems to automatically respond to system problems
Runs tasks in response to system changes of multiple triggers
Configures a system to wake up from standby or hibernation so that routine tasks can be carried out
Task Scheduler 2.0 also includes new tools and features, such as:
Triggers and actions
Credential management
Improved user interface
MMC snap-in
Task settings
Improved scripting support
Task Scheduler XML schema
PowerShell (or Windows PowerShell) is the name that Microsoft gave its new extensible command-line interface shell and scripting... more
PowerShell (or Windows PowerShell) is the name that Microsoft gave its new extensible command-line interface shell and scripting language. PowerShell has gone under other names before being releasedMicrosoft Shell, MSH, and Monad. These terms are now obsolete and have been superseded by PowerShell.
This chapter introduces PowerShell and looks at how VBScript programmers can leverage the power offered by this new shell and scripting language.
In this chapter, you continue to investigate client-side scripting, but you examine advanced technologies that give much... more
In this chapter, you continue to investigate client-side scripting, but you examine advanced technologies that give much needed functionality and extensibility to client-side pages. This includes:
Scriptlets
Behaviors
HTML components
Each of these are subjects broad and deep enough to be books of their own, so this chapter focuses on small, well-tested examples that cover the major techniques required for you to begin using these technologies. In reality, to achieve maximum gain from these technologies, you’d have to read masses of documentationa lot of which is very poorly written. This chapter shows you what is possible and how to go about doing it. The authors will have achieved what they’ve set out to do if, by the time you have finished this chapter, you can make any sense of the official documentation!
Microsoft’s documentation is available as a free download at www.microsoft.com/scripting.
www.microsoft.com/scripting
Ask programmers if they use VBScript and most will answer “Yes, for ASP” or “Yes, on an intranet” or maybe even “Yes, for... more
Ask programmers if they use VBScript and most will answer “Yes, for ASP” or “Yes, on an intranet” or maybe even “Yes, for client-side scripting” (if their audience is made up entirely of people using Internet Explorer, unlikely nowadays). But you need to remember that these are nothing more than contexts where VBScript can be used to solve problems that are in need of scripting solutions. Because VBScript is designed as an ActiveX script engine, it can be used to provide scripting capability for any ActiveX scripting host environment.
Two of the most common hosts are:
Active Server Pages (ASP)
Internet Explorer
Both of these hosts provide the programmer with a lot of power but both also come with certain limitations. An example is that Internet Explorer does not provide a capability for script to interact with the local computer (such as file system access, registry access, and so on) unless the user explicitly sets permissions for this (and doing this can cause enormous security risks. For this reason, this is usually done only for trusted sites and intranets). So what’s the point of having extended power within the VBScript language when you can’t do anything with it? Well, this is where Windows Script Host (WSH) comes in. WSH is a totally scripting language–neutral host interface that will work with any ActiveX script engine. This means that programmers who want to use WSH can use VBScript, JScript, PerlScript, or any other scripting language that exposes the ActiveX scripting interfaces. The WSH host interface thus provides Windows platforms with a powerful, yet easy-to-use scripting platform that can be accessed from both the Windows GUI and the command prompt.
In this chapter, you examine the following aspects of WSH:
The tools required for WSH development
What WSH can be used for
The two methods of execution for WSH scripts
The use of .wsh files to customize script behavior
.wsh
The WSH object model
The .wsf file format, used for creating more advanced scripts
.wsf
The use of WSH for disk and network administration
In this chapter, you examine Windows Script Components including their structure and how to create and register them. Later... more
In this chapter, you examine Windows Script Components including their structure and how to create and register them. Later in the chapter, you'll see at how to use classes in your components. If you are used to using XML, then the structure of the script here will be familiar to you and that will be a huge advantage. If not, work carefully through the examples and all will become clear!
One thing that becomes immediately obvious to those who write script coming from a programming background is that anything... more
One thing that becomes immediately obvious to those who write script coming from a programming background is that anything they create using script is easily and readily visible to anyone and everyone else. You have no compiler to turn the code into something that only a machine can make sense of. This easily visible code was a real worry, but on the whole it didn’t stop people from writing and using scripts, who saw massive advantages in scripting. Just as the open, plain-text nature of HTML made learning web design easier than say learning C#, the same “open book” format for scripts encouraged a huge explosion of script use on the Web. Thinking back to those times, some of it was pretty bad and looked ugly, but we didn’t care because it was scripting and it made pages look more exciting than the others out there. Because it was so easy to see how scripts worked (and to copy the code from one page and paste it into another), the future of scripting was guaranteed.
But now the future of script use is well established and a bit of privacy isn’t a bad thing. Programmers who use script want to regain some of the mystique that they once had using C# or VB (using script to solve a problem is a bit like a magician in a see-through jacket). Things have moved on a little bit from the days of scripts being a total free-for-all and you can do a few things to make it harder for script snoopers to see your secrets! Protecting your scripts is much more than just protecting your intellectual propertyalthough there’s nothing wrong with doing that!
Although most people out there are honest and don't bother to look at plain-text scripts, taking precautions against accidental changes (such as those down with Windows Script Host) makes sense. Take the real-life example of a WSH script that searches folders for particular files, copies them, and then deletes the originals; you don't want someone to tamper with that script so that it just deletes files (this actually happened and it wasn’t done maliciously, but rather a genuine accident caused by someone unfamiliar with the script). If you use the Microsoft Script Encoder, the advantage is that if someone changes an encoded script modifyjust one character changedit no longer works. Encoding scripts can help protect curious users from themselves as much as from malicious attack.
In this chapter, you see primarily how to encode VBScript code with the Microsoft Script Encoder, and then examine other things that you can do to discourage script snooping. These won’t be as effective as the Microsoft Script Encoder but they are worth bearing in mind and can be quite effective in appropriate circumstances. But before you get into all that, the next two sections discuss the limitations of encoding as well as some dos and don’ts you should keep in mind.
The remote scripting technology makes web applications substantially more powerful and more closely resemble client/server... more
The remote scripting technology makes web applications substantially more powerful and more closely resemble client/server applications developed using languages like C++, Visual Basic, or Java. By doing this, programmers can overcome the inherent limitations of web applications. Without either using remote scripting or resorting to the newer (and popular) AJAX (Asynchronous JavaScript and XML), a web browser has only one way to request new information from the serverto load an entirely new page. With remote scripting, it becomes possible for the client page to execute a method on an ASP page without navigating away from the current page. More important, the requested data is available as the return value of the remote method called by the client page.
Combined with Dynamic HTML (DHTML), this technology greatly simplifies all the applications that were previously forced to use cookies, hidden HTML input fields, or other dirty tricks to rebuild the new page as similar as possible to the previous one.
This chapter shows you how to install remote scripting, enable it, and then leverage it with a few lines of code.
Previous chapters focused on web development, but there are times when you don’t want your application to look like a web... more
Previous chapters focused on web development, but there are times when you don’t want your application to look like a web page with all of the browser components, such as toolbars, exposed. In the past, C/C++/C#, Java, and Visual Basic programmers cornered the market for traditional Windows applications, but the introduction of HTML applications in Internet Explorer changed that. Now you can use technologies, such as Dynamic HTML (DHTML), Cascading Style Sheets (CSS), and scripting, to write full-fledged Windows applications.
HTML applications are what they sound likean HTML-based application. They are often referred to as HTAs, after the file extension (.hta) that HTML applications use. The parent process of mshta.exe (the application that actually runs an HTA) is Internet Explorer; so almost anything (you’ll see exceptions later) that you can do using Internet Explorer 5 or later (including Internet Explorer 7), you can do in HTA.
.hta
mshta.exe
Talk to most people about VBScript and usually the first thing that pops into their heads is Active Server Pages... more
Talk to most people about VBScript and usually the first thing that pops into their heads is Active Server Pages (ASP). The two technologies are linked together in the minds of many web developers. Sure, you can do a lot with VBScript on the client side and even create stand-alone applications, but ASP is where all the power is!
The preceding chapter focused mainly on client-side scripting and applications. Now it’s time to take a look at the server side of things. Creating web sites with only client-side scripting is all well and good, but your functionality and power are severely limited. By adding server-side scripting, you add a whole new dimension to the web site and gain a huge advantage. You can draw upon the wealth of data available to you on the server and across the enterprise in various databases. You can customize pages to the needs of each different user that comes to your web site. In addition, by keeping your code on the server side, you can build a library of functionality. You can draw from this library again and again to further enhance other web sites. Best of all, using server-side script libraries allows your web sites to scale to multitier, or distributed, web applications. To do all this, you need a good understanding of the HTTP protocol, and how an HTTP server interacts with a browser. This model is important to understand when developing web applications that exist on the client and server side.
Next, you’re introduced to Active Server Pages, Microsoft’s server-side scripting environment, which you can use to create everything from simple, static web pages to database-aware dynamic sites, using HTML and scripting. Another important use is as programming glue. Through the use of ASP you can create and manipulate server-side components. These components can perhaps provide data to your application, such as graphic image generation, or may link to a mainframe database. The important thing to always remember is that the ASP code does nothing more than to facilitate the use of these components on the Internet.
ASP comes with some built-in objects that are important to understand before their full potential can be unleashed. These objects are covered in depth. Finally, you’ll see some real-world examples of using ASP on a web site. These should give you some idea of the power and beauty of server-side scripting with ASP.
By now, it should be clear that VBScript is useful in many contexts within Windows. Not surprisingly, along with a variety... more
By now, it should be clear that VBScript is useful in many contexts within Windows. Not surprisingly, along with a variety of different technologies, Microsoft provides yet another component capable of supporting VBScriptthe Script Control. This ActiveX control provides a simple way for your application written in Visual Basic (or any other language that supports ActiveX controls) to host its own scripting environment, allowing you, or your users, to customize the application. Thanks to Microsoft’s attention to backward compatibility in the .NET framework, the Script Control, and by extension VBScript can also be used to automate and extend .NET applications.
In the past, programmers had to struggle to provide customizability to their projects, or pay license fees for other products such as Microsoft’s portable VB variant, Visual Basic for Applications (VBA). In 1997, Microsoft released Windows Script Interfaces (WSI) as an interface to script engines, and eventually followed up with Script Control. WSI was intended for C++ programmers, and the Script Control was tailor-made for use in a Visual Basic (VB) applicationand as noted, it works great with .NET, too.
At the time of the writing of the third edition of this VBScript Programmer’s Reference, Visual Basic 6 is considered “legacy” technology, and most new applications for the Windows platform are being written using the .NET framework. One may wonder why the Script Control is still relevant, almost ten years after its initial release, and why the authors are including this chapter in this edition of the book. In short, there are two reasons:
Millions of lines of VB 6 code are still in use in enterprises around the world. It may be some time yet before those applications are ported to .NET. The Script Control and VBScript actually offer an excellent opportunity to extend and modify existing VB 6 applications without adding or changing actual VB 6 code.
While Microsoft does offer the Microsoft.Vsa namespace for .NET and Visual Studio for Applications as viable .NET scripting options, the Script Control is so simple and easy to adopt, you might be tempted to use it instead.
This appendix contains a complete reference of functions and keywords in VBScript 5.6. You will also find a list of the VB/VBA... more
This appendix contains a complete reference of functions and keywords in VBScript 5.6. You will also find a list of the VB/VBA functions and keywords that are not supported in VBScript. Where appropriate, an alternative to an unsupported function or keyword is shown.
Because variables are used to hold values, it makes sense to choose names for them that describe their purpose or what they... more
Because variables are used to hold values, it makes sense to choose names for them that describe their purpose or what they contain. The bigger the project you are working on is, the more important it is to keep track of its variables. This appendix offers some things to keep in mind, including standards:
You’ll want to keep your naming consistent. For example if you use Cnt as a variable in one part of the script and Count in another when you are in fact dealing with the same data, you’re very likely to introduce runtime errors by confusing the variables.
Cnt
Count
It is useful to include the scope of a variable by prefixing it with g for global, or l for local (to a subprocedure), such as gstrCompanyName and lstrDepartmentName.
g
l
gstrCompanyName
lstrDepartmentName
One of the best ways to organize variable names is to prefix all variable names you use with a shorthand representation of the data type that the variable will hold. The standard prefixes used to accomplish this are called Hungarian notation.
This appendix covers coding conventions and, like the previous appendix covering variable naming, will help you to produce... more
This appendix covers coding conventions and, like the previous appendix covering variable naming, will help you to produce code that is easily readable and understandable, minimizing errors and speeding up the inevitable debugging process.
This appendix is primarily aimed at the Visual Basic programmer who wants to make the jump to VBScript. It covers all of... more
This appendix is primarily aimed at the Visual Basic programmer who wants to make the jump to VBScript. It covers all of the Visual Basic constants that are supported in VBScript. Constants are useful in script because they allow you to use a specific value without explicitly writing it.
Here you’ll find all the error codes associated with VBScript along with the... more
Here you’ll find all the error codes associated with VBScript along with the Err object.
Err
The default scripting languages installed with Microsoft Windows Vista and XP, Office 2007 and XP, ASP 3.0, and many other... more
The default scripting languages installed with Microsoft Windows Vista and XP, Office 2007 and XP, ASP 3.0, and many other applications provide a scripting runtime library in the file scrrun.dll, which implements a series of objects that can be used in ASP on the server and in client-side code running on the client.
scrrun.dll
The Dictionary object: This provides a useful storage object that can be used to store values, accessed and referenced by their name rather than by index as would be the case in a normal arrayit’s ideal for storing the name/value pairs that you retrieve from the ASP Request object, for example.
Request
The FileSystemObject object: This provides you with access to the underlying file system on the server (or on the client in Internet Explorer 5/6 when used in conjunction with a special type of page named HTML Application or HTA)you can use the FileSystemObject object to iterate through the machine’s local and networked drives, folders, and files.
The TextStream object: This provides access to files stored on disk, and is used in conjunction with the FileSystemObject objectit can read from or write to text (sequential) files.
This appendix gives details about the Windows Script Host objects. You can find further details and examples in Chapter 12... more
This appendix gives details about the Windows Script Host objects. You can find further details and examples in Chapter 12.
Windows Script Host has fifteen objects outlined.
A regular expression is a pattern of text that consists of ordinary characters (such as letters a through z) and special... more
A regular expression is a pattern of text that consists of ordinary characters (such as letters a through z) and special characters that are known as metacharacters. The pattern is used to describe one or more strings to match when searching a body of text. The regular expression acts as a template for matching a character pattern to the string that is being searched for.
The following table contains the complete list of metacharacters and their behavior in the context of a regular expression.
The reference material in this appendix is a companion to the detailed explanation in Chapter 3 of data types... more
The reference material in this appendix is a companion to the detailed explanation in Chapter 3 of data types, in particular the VBScript Variant data type. However, a brief description of these concepts follows.
VBScript is what is known as a weakly typed programming language, which is the opposite of a strongly typed language. A weakly typed language does not allow you to declare variables with specific data types such as String, Date, or Boolean. Instead, in VBScript, all variables are automatically and implicitly assigned a special data type called Variant. The Variant data type is actually many data types in one. VBScript still has the concept of data types such as String, Date, and Boolean, but they are embedded within the “container” of the Variant type. The more specific type inside of the Variant type is called a subtype. A Variant variable can have one of many different subtypesbut only one subtype at a time.
String
Date
Boolean
The subtype of a Variant variable can change in one of two ways: implicitly and explicitly.
An implicit change in subtype occurs when a new value is assigned to a Variant variable. Before the new value being assigned to the variable is committed, the VBScript engine uses its own logic to examine the new value and automatically decide what the subtype should be. If the new value fits within the bounds of the already assigned subtype, VBScript does not change it. This automatic subtype change process is called implicit type coercion. As a VBScript programmer, it is very important to understand how implicit type coercion works and when and how it occurs.
The other method of initiating a change in a variable’s subtype is called explicit coercion. This means that the VBScript programmer is taking specific measures to deliberately control the subtype of a variable. This can be done using certain syntax techniques (see Chapter 3) and also VBScript’s conversion functions. As an example of the latter, the CLng() function will change the subtype to Long. Similarly, the CStr() function will change it to String. Sometimes it is necessary to use a conversion function to force a subtype change so that a particular operation or function call will work properly.
CLng()
Long
CStr()
The Variant data type, the subtypes, and the ins and outs of implicit and explicit type coercion are covered in great detail in Chapter 3. Even an experienced programmer coming from another language would do well to read this chapter. Management of the Variant subtypes in your scripts is a key to your success as a VBScript programmer. The following two sections discuss the Variant subtypes as well as the Visual Basic data types.
ActiveX Data Objects (ADO) is a data access component from Microsoft that you can use by VBScript running under a variety... more
ActiveX Data Objects (ADO) is a data access component from Microsoft that you can use by VBScript running under a variety of hosts, including Active Server Pages and the Windows Script Host. ADO was the primary data access component used by VBScript, Visual Basic, and Visual C++ developers in the years before Microsoft introduced the .NET platform. While ADO is no longer in the spotlight, it is a very mature data access component that is still supported by Microsoft. Better yet, it’s a safe bet that ADO is available by default on most installations of Windows. You can use ADO in your VBScript programs to write to and read from structure data sources such as databases like Access, Oracle, and SQL Server as well as spreadsheets and flat files.
ADO is too large a subject to cover thoroughly in one appendix. Therefore, the purpose here is not to discuss the details and powerful features of ADO, but rather, because ADO is so often used in combination with VBScript, to give a brief overview and reference. This overview assumes that you are familiar with basic relational database concepts such as tables, columns, queries, stored procedures, and so on. If you make significant use of ADO in your scripts, you may want to purchase a book dedicated to ADO. This guide to ADO covers ADO 2.8, the latest version of ADO at press time.
The appendix discussion includes the ADO objects shown in the following table.
Connection
Command
Parameters
Parameter
Recordset
Error
Errors
This appendix either does not cover or limits discussion on the following:
Because VBScript is a pre-.NET technology, the appendix doesn’t discuss ADO.NET, which is the latest version of ADO. ADO.NET is intended for use by .NET languages such as C# and VB.NET. It is not natively compatible with VBScript. To ensure that you have the latest version of ADO, you can download the latest release of Microsoft Data Access Components (MDAC) from msdn.microsoft.com/data.
msdn.microsoft.com/data
The appendix does not cover the Record or Stream objects because these are more advanced than the basic ADO overview.
Record
Stream
This appendix does not have separate sections for the Fields and Field objects, but the basic usage of these are covered in the section on the Recordset object. The example code in this appendix is based on the Northwind sample database that ships with Microsoft Access.
Fields
Field
The downloadable script files are designed for running under the Windows Script Host (see Chapter 15), but the code is easily transferable to other hosts. The discussion focuses on using ADO for relational database access, because that is the most common use for ADO, but because ADO is part of Microsoft’s “Universal Database” strategy, it can be used to access other structured data formats such as spreadsheets, email systems, text files, and so on.
Certain advanced or seldom used properties and methods may be excluded for some objects.
Purchase Before purchasing this product, please be sure you have met all software and system requirements, and that you understand any limits placed upon its use.
Return Policy Wrox Chapters on Demand are non-returnable and non-refundable.
Reader Software Wrox Chapters on Demand are offered as PDFs, and they must be viewed using the Adobe Reader. If you do not have the Reader installed, it can be downloaded for free at Adobe.com.
Test Download As Wrox Chapters on Demand purchases are non-returnable, it is advisable that you test your system and software configurations with a free sample download before you place an order.
Usage Rights for a Wrox Chapter on Demand File Any Wrox Chapter on Demand product you purchase from this site will come with certain restrictions that allow Wiley to protect the copyrights of its products. After you purchase and download this title, you:
If you have any questions about these restrictions, you may contact Customer Care at (877) 762-2974 (8 a.m. - 5 p.m. EST, Monday - Friday). If you have any issues related to Technical Support, please contact us at 800-762-2974 (United States only) or 317-572-3994 (International) 8 a.m. - 8 p.m. EST, Monday - Friday).
Related Books
Visual Basic Resources