Subscribe to Windows IT Pro

 

Get Newsletters

  • Get the Latest News
  • Product Updates
  • Helpful Tricks
  • Productivity Tips

Subscribe Now!

June 25, 2001 12:00 AM

Rem: Using the Chr Function

Windows IT Pro
InstantDoc ID #21279
Rating: (1)
Downloads
21279.zip

Do you have a scripting-related question or problem? You can send your question or problem to winscriptsol@win2000mag.com.

I found a .vbs logon script by Grant Ardern (http://cwashington.netreach.net/script_repository/view_scripts.asp?index=416&scripttype=vbscript) 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.

Related Content:

ARTICLE TOOLS

Comments
  • Justin Hackett
    8 years ago
    Mar 09, 2004

    Excellent article. It saved me from another day of frustration.

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

White Papers

Get your Windows 7 deployment off to the right start by implementing PC lockdown. A locked-down environment is easier and cheaper to support since users are less likely to make unnecessary changes to the core system configuration - read more here!

Essential Guides

Is your iSCSI "lossy"? The reality is that most off-the-shelf Ethernet hardware deployed for iSCSI can lose packets, resulting in slow performance or application downtime. Learn how to assess your current iSCSI infrastructure and engineer an advanced iSCSI SAN infrastructure.

Web Seminars

What's the best way to keep your network safe from malware? In this web seminar, security expert Greg Shields suggests an alternative method to the traditional blacklisting approach that is common with anti-virus and anti-malware solutions.

eLearning Series

We bring the experts direct to you to share their real-world perspective and expertise. During each event, three sessions stream in real time, so you can learn, ask questions, and get solutions.
Upcoming event: Getting the Most with Exchange 2010 with Paul Robichaux

Subscribe to Windows IT Pro!

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.