VBScript Special Characters

By The Microsoft Scripting Guys

Sesame Script

Welcome to Sesame Script, the column for beginning script writers. The goal of this column is to teach the very basics of Windows scripting for system administration automation. We’ll provide you with the information you’ll need to begin reading and understanding scripts and to start modifying those scripts to suit your own needs. If there’s anything in particular about scripting you’re finding confusing, let us know; you’re probably not alone.

Check the Sesame Script Archives to see past articles.

On This Page

Special Characters
What’s My Line?
This & That
To Be Continued
' Comments
Advanced: Troubleshooting with Comments

Special Characters

It’s a beautiful day here in Redmond, but it looks like our softball team is going to pretend they work too hard to have some fun and we’re going to have to forfeit our game due to lack of players. (Peter insists he’s out sick today, I guess we’ll believe him this time. But no one else has a good excuse, we know no one around here works that hard.)

So now that our sad excuse for a softball team has forced some extra time on us, we’ll try to be productive and write another column. We’re going to go very beginner here today and take a quick look at some special characters that are used in almost every VBScript script you’ll ever see, and explain what those characters are doing. Then we’ll go out and enjoy the sunshine, even without the softball team.

What’s My Line?

Before we actually look at the special characters, we’re going to cover a fundamental aspect of writing a script: the line. As we showed you in a previous article, this one line is a script:

Wscript.Echo "My very first script."

If you want to echo two lines, you might try something like this:

Wscript.Echo "Line 1" Wscript.Echo "Line 2"

You might try it, but it won’t work. As a general rule, with VBScript you can have only one command per line. So you’d have to write the script like this:

Wscript.Echo "Line 1"
Wscript.Echo "Line 2"

Note: Okay, there is a way to put more than one command on a line. That’s another special character, the colon (:). This script will work:

Wscript.Echo "Line 1" : Wscript.Echo "Line 2"

As you can see, we just separated the two commands with a colon. However, if you want the advice of the Scripting Guys (and who doesn’t?), we would advise you never to use this type of notation. It makes your scripts hard to read, and is just plain ugly. If you do script like this, well, just don’t tell anyone you learned your scripting technique from us.

Another requirement of VBScript is that the entire command must be on one line. So this won’t work either:

Wscript.Echo
"Line 1"

Also, multiple spaces are pretty much ignored (unless of course they’re inside a string). So this:

Wscript.Echo "Line 1"

will work the same as this:

Wscript.Echo             "Line 1"

(but not the same as this:)

Wscript.Echo "         Line 1"

Given that, you can infer that it doesn’t matter where on the line you start. This allows for indenting that helps make your script more readable. For example:

For i = 1 to 5
    Wscript.Echo i
Next

This script simply counts from 1 to 5 and echoes the number. We’ll talk about loops in a different article, but for now just know that everything between the For line and the Next line will happen five times. One quick look at this loop tells you that Wscript.Echo is between a For line and a Next line, and will therefore be called five times. You would have to look much more closely to notice that fact in this next script, especially if it were part of a larger script or we had more than one Echo statement between the For and the Next:

For i = 1 to 5
Wscript.Echo i
Next

And with that foundation, let’s move on.

This & That

A character you’ll see quite often in VBScript is the ampersand (&). One of the primary purposes of the ampersand is to concatenate things, such as strings or variables. For instance, suppose we want the first and last name of a particular user. The problem is that you have to retrieve the first and last names from different places, such as two different fields in Active Directory or different records in a database, but you want to display them on a single line, like this:

Ken Myer

We already know we can’t do this:

First = "Ken"
Last = "Myer"
Wscript.Echo First Wscript.Echo Last

That’s too many commands on one line. We also can’t do this:

First = "Ken"
Last = "Myer"
Wscript.Echo First Last

Wscript.Echo lets you specify only one thing to echo at a time. So how do we get the first and last names together? That’s where the ampersand comes in:

First = "Ken"
Last = "Myer"
Wscript.Echo First & Last

Nice, huh? No? What’s wrong? Oh yes, the space. The above script gave us output like this:

KenMyer

But we want to show a distinct first and last name. That’s easy enough. You can use as many & signs as you want on a single line:

First = "Ken"
Last = "Myer"
Wscript.Echo First & " " & Last

That’s better:

Ken Myer

So you might be thinking, "Wait a minute. You guys said we could echo only one thing at a time, this is three things: First, Last, and a space!" That’s the beauty of the ampersand, it lets you bend the rules a little. Say you’re in the grocery store and the lines are really long. There’s one line that’s moving quickly, but it’s the express line where you must have 15 items or less. You have three six-packs of Pepsi. Do you have 18 items, banning you from the express line, or only three? Because they’re bundled up together, you would say you have three and go through the express lines, and chances are no one would complain. The ampersand basically bundles things together for you so you don’t have to deal with separate strings, variables, and commands, and VBScript won’t complain. Much quicker and easier, just like the express line.

But what happens if your script has a really long line? A single line can get pretty hard to read when it stretches out off the screen. For example:

intAnswer = 42
Wscript.Echo "The answer to life, the universe and everything is: " & intAnswer

Granted, this line isn’t outrageously long, but just go with us here. We already saw that we can’t just put part of the command on the next line, like this:

intAnswer = 42
Wscript.Echo "The answer to life, the universe and everything is: " 
    & intAnswer

So what do we do? Technically we don’t have to do anything; lines can be as long as we want. However, to ensure our scripts are easily readable we use the line continuation character, otherwise known as the underscore (_).

More Information

Concatenation Operators - VBScript Language Reference

Concatenating Strings - Windows 2000 Scripting Guide

To Be Continued

The primary use of the underscore is to make scripts easier to read. Suppose you had a single command with 100 or more characters in it. The line might go out past the end of your Notepad window and you’d have to keep scrolling over to see the whole script. We can’t even give you a script to try here because that would stretch this Web page out and make this whole article hard to read. So we’ll show you a picture:

Long Lines

You could always turn on Word Wrap in Notepad, but that gets a little ugly too:

Long Lines with Word Wrap

So what do we do about this? We use the underscore to break everything up. Now we can show you a script:

intComputers = 40
intServer = 5
intDesktop = 35
Wscript.Echo "There are " & intComputers & _
    " computers in use by the Accounting department: " & _
    intServer & " servers and " & intDesktop & " desktop machines."

And here’s what it looks like in Notepad:

Long Lines Formatted

Notice that we also indented a few spaces so you can tell at a glance that the two continued lines are still part of the Wscript.Echo statement. The script is much easier to read now. And you know how much the Scripting Guys like to do things the easy way.

More Information

Statement Breaks in VBScript - Windows 2000 Scripting Guide

' Comments

One of the Scripting Guys is a big fan of the television game show Jeopardy! That’s not to say this Scripting Guy is particularly good at this game; most of the show is spent saying "I know that, I know that…ummm…." This same sort of memory lapse can occur when you write scripts. "Why did I write it this way?" "What was this script doing?" "I know I had a good reason for this…"

The easiest way to ensure you’ll always know what a script is doing is to comment it. Now, you might hear people saying things like "comment every line," or "put as many comments in as you possibly can." We’re going to be a little more sensible about it. After all, commenting means more typing, and we like to avoid excess typing whenever possible.

So what is a comment? Well, this brings us to the final character we’re going to discuss today. In VBScript, a comment is any line that starts with a single quote character (').

' This script echoes a name
Wscript.Echo "Ken Myer"

So you know how we just said we’d be sensible? That comment isn’t sensible. One look at the Wscript.Echo statement and you know exactly what the script is doing, you don’t need a comment to tell you that. Just to show you a better example, we’ve taken a script from a recent Tales from the Script column on scripting popup blockers and added some comments:

' Returns whether or not the registry key associated with 
'  the MSN Toolbar Suite popup blocker exists. If it does, the
'  popup blocker is already installed.
Const HKEY_CURRENT_USER = &H80000001

strComputer = "."
Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
 
strKeyPath = "Software\Microsoft\MSN Apps\MSN Popup Blocker"
strValueName = "Enabled"
objRegistry.GetDWORDValue HKEY_CURRENT_USER,strKeyPath,strValueName,dwValue

If IsNull(dwValue) Then
    Wscript.Echo "The registry key does not exist."
Else
    Wscript.Echo "The registry key exists."
End If

One important thing to note is that you can’t use the continuation character (underscore) with comments. Every line that has a comment must start with a single quote.

You can also put a comment at the end of a line. Try this:

Wscript.Echo "Line 1" ' This echoes one line

You know how we said you can put only one thing after the Echo statement unless you’ve used an &? Well, comments don’t count as being a "thing." Everything on a line following the single quote is ignored; as far as VBScript is concerned, it doesn’t even exist.

More Information

Commenting Scripts - Windows 2000 Scripting Guide

VBScript Coding Conventions - VBScript User’s Guide

Advanced: Troubleshooting with Comments

So there’s a really important use of the comment character other than putting in comments to explain your code. As we said, if VBScript sees a single quote mark, it ignores that quote and everything on the line after it. That means you can use the quote mark as a strategic means of troubleshooting. Try this script, which you already know won’t run:

intAnswer = 42
Wscript.Echo "The answer to life, the universe and everything is: " 
    & intAnswer

This script produces an error message that states the problem is on line 3 (that’s what the 3 in (3, 5) means):

C:\Scripts\test.vbs(3, 5) Microsoft VBScript compilation error: Expected statement

So let’s turn line 3 into a comment (know as "commenting out" line 3) by placing a single quote mark at the beginning of the line. Try the script now:

intAnswer = 42
Wscript.Echo "The answer to life, the universe and everything is: " 
'    & intAnswer

As you can see we no longer have an error, which means that yes, the problem is with the third line, the rest of the script is fine.

Armed with this information, you can probably come up with other uses for commenting, or commenting out, lines of code.

And that’s it for now. Anyone want to play softball?

More Information

Using Comments as a Debugging Aid - Windows 2000 Scripting Guide