Subscribe to Windows IT Pro

 

Get Newsletters

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

Subscribe Now!

November 05, 2002 12:00 AM

Rem: Understanding Quotation Marks in VBScript

Windows IT Pro
InstantDoc ID #26975
Rating: (6)
Downloads
26975.zip

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.

Related Content:

ARTICLE TOOLS

Comments
  • Anonymous User
    7 years ago
    Jun 14, 2005

    HERE IS AN EXAMPLE GUYS: strSQL = """This is a test"""
    After re-reading it a couple of times I figured it out. I would have given your artical a 5 if I din't have to sift through the technical information.

  • Anonymous User
    7 years ago
    Mar 25, 2005

    I think a simple example with an escape quote inside a single string should be placed before those concatenating variables. This confused me for a time, even though the article was really interesting.

  • Anonymous User
    7 years ago
    Mar 25, 2005

    I think a simple example with an escape quote inside a single string should be placed before those concatenating variables. This confused me for a time, even though the article was really interesting.

  • Anonymous User
    7 years ago
    Mar 09, 2005

    This solved my problem that VB help couldn't or any books I had.
    Thanks.

  • Anonymous User
    7 years ago
    Mar 01, 2005

    This article is a waste of hard disk space.

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.