Rem: Using the Chr Function


Do you have a scripting-related question or problem? You can send your question or problem to [email protected]

I found a .vbs logon script by Grant Ardern ( that contained the code that Listing 1 shows. I know that this code is using the Net Start command to start the specified service, but what's the purpose of Chr(34)?

VBScript's Chr function returns the character associated with the ANSI value you specify in the parentheses. Chr(34) returns a double quote ("). In VBScript, double quotes are special reserved characters that you use to identify the beginning and end of a string. However, you also use double quotes in VBScript code for other purposes, such as enclosing command arguments that have embedded spaces.

In Listing 1, Ardern uses a string that contains two related parts: The command to run, Net Start, followed by the command's argument, Network Associates McShield. Because Net Start's argument contains spaces, you must enclose it in double quotes. Otherwise, the scripting engine will interpret this part of the string as three distinct arguments..

Listing 2 shows the code when you include the necessary reserved and literal double quotes. However, if you were to run this code, it would fail because the scripting engine would read the second double quote as the end of the string and the Net Start command therefore wouldn't receive the argument it needs. Thus, you need to do more than just include the necessary double quotes. You also need to tell the scripting engine that the double quote at the beginning of "Network Associates McShield" is the beginning of a single command argument rather than the end of the string. In other words, you need to tell the scripting engine to interpret that double quote as a literal character rather than a reserved character. To convey this information, you flag, or escape, the double quote by preceding it with another double quote. Similarly, you escape the double quote at the end of "Network Associates McShield" by adding another double quote. Listing 3 shows the code with the escaped literal double quotes.

Some scriptwriters don't like working with consecutive double quotes, so they take another approach: They use the concatenation (&) operator to concatenate Chr(34) to the beginning and end of the string when they need to handle a portion of the string as a single argument or token. Ardern uses this approach in Listing 1. First, he broke the string "net start Network Associates McShield" into two strings—"net start" and "Network Associates McShield"—and concatenated them. Then, to include the literal double quotes around the "Network Associates McShield" argument, he concatenated Chr(34) to the beginning and end of the string that identifies the service to start. Without the extra double quotes, Net Start would interpret Network, Associates, and McShield as three arguments rather than one, which would result in an error.

The code samples in Listing 1 and Listing 3 produce the same result. Which approach you use is a matter of personal preference.

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.