Rem: Understanding Quotation Marks in VBScript


I don't understand the purpose behind different combinations of quotation marks in VBScript. Can you explain the use of quotation marks in the code segment that Listing 3, page 6, shows?

The key to understanding quotation marks in VBScript is learning two basic rules about VBScript strings. First, a VBScript string is simply a series of characters enclosed in quotation marks (" "). Second, to embed quotation marks (") inside a VBScript string (quotation marks you don't intend to mark the end of the string), you need to escape the quotation marks with an escape character. But the VBScript escape character is also the quotation mark, which is the cause of the confusion.

Let's look at a simple example, then compare the example with the string in Listing 3. The following script displays status information as it runs:

WScript.Echo "Update Registry: Success"

This statement simply echoes (i.e., displays) the string Update Registry: Success. Suppose you want to enhance the script by storing the status portion of the string in a separate variable rather than coding it into a string. One approach that you can use is

strStatus = "Success"
WScript.Echo "Update Registry:" & strStatus

So far, the snippet is fairly straightforward. You simply echo a string constructed from two substrings: the literal prefix ("Update Registry: ") concatenated with the value stored in the variable named strStatus. Figure 2 shows the result echoed to the screen.

Now let's suppose you want to enclose the status portion of the message inside quotes. (This example is a stretch, but it illustrates my point.) You write the code required to enclose the value of strStatus in quotes, as Listing 4 shows. Look familiar? When run, the example script would display the message that Figure 3 shows.

How does VBScript interpret the three substrings passed to the Echo method? The first quote at the beginning of the first substring marks the start of the string to echo. The second quote would usually mark the end of the string; however, because the second quote is immediately followed by another quote, VBScript interprets the second quote as an escape character for the third quote, which has the effect of inserting a single quote at the end of the first substring. The fourth quote marks the end of the first substring.

Next, VBScript concatenates the value stored in strStatus with the first substring. VBScript interprets the third substring (the four consecutive quotes) as a string that consists of a single quote. The first and fourth quotes mark the beginning and end of the substring. The second quote escapes the third quote, which results in the quote at the end of the echoed message.

The string in Listing 3 follows the same pattern as this example, with one exception. Rather than echoing the resulting string, VBScript assigns the string in Listing 3 to the variable named strWQLQuery.

Listing 3 shows the result of concatenating the value of the variable named strStartDateTime with another string (the SELECT statement) and enclosing the variable's value in quotation marks (" "), as mandated by the Windows Management Instrumentation (WMI) Query Language (WQL). Although the result can look confusing at first, you can easily interpret the string by separately analyzing each substring.

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.