The Rules In VBScript, there are several activities that can lead to the need to produce strings that have double quotes (") embedded in them. Most often this comes about due to the need to run an external command where spaces are present in a path.
First let's talk about the quoting rules in VBScript. A single double quote starts a string literal and another single double quote ends a string literal. So this code:
WScript.Echo "This is a string literal"
Would output this:
This is a string literal
To have a single double quote appear in a string you uses two double quotes (I know this sounds confusing). So this code:
WScript.Echo "This has a "" double quote in the middle"
Would output:
This has a " double quote in the middle
This means that if you want a string to have double quotes at the beginning and end, you need to start and end it with triple double quotes:
WScript.Echo
"""This has double quotes at the beginning and end""
" Output:
"This has double quotes at the beginning and end"
In that last example, the first and last double quotes (the ones in red) start and end the string. The others (the ones in black) make the double quotes appear in the string itself.
Making it easy
It is entirely possible to do the quoting using multiple quotes all stacked up to get whatever quoting you need. I'm lazy though and I hate having to think about how many quotes I need to get the result I want. Instead, I use a function to do the quoting for me:
Function qq(strIn)
qq = Chr(34) & strIn & Chr(34)
End Function
All this function does is take a string in and return that string with double quotes around it. Chr(34) is simply using the ASCII value of the double quote character instead of using "". I just find it less confusing to read. So now if I want to put double quotes around something I just include the function in my script then:
WScript.Echo qq("This has double quotes at the beginning and end")
Output:
"This has double quotes at the beginning and end"
Troubleshooting Quoting Problems
As I said earlier, the most common reason to need oddball quoting is paths with spaces. When you are trying to use .Run or .Exec and you are getting file not found errors, nine times out of ten it is a problem with quoting and spaces in paths. Let's work with an example:
Scenario:
You want the users to be able to enter the path and name of a zip file and you will output a list of the files in the archive. For this I wouls use 7-zip which is my favorite compression utility and has a good command line interface.
Process:
First I would work out the code to get the input from the user:
Option Explicit
Dim strArchivePath
strArchivePath = InputBox("Enter the path to the archive")
Note that for brevity's sake I'm going to leave out all the checking that you should be doing here to make sure that what the user enters is really a valid path to a valid file.
Next, I would try to build the command:
Option Explicit
Dim strArchivePath
Dim strCMD
strArchivePath = InputBox("Enter the path to the archive")
strCMD = "C:\Program Files\7-zip\7z.exe l " & strArchivePath
WScript.Echo strCMD
Notice that I stop developing my script and run it at this point. The key here is the echo. That echo shows you the command that you will be trying to run. This is critical. At this point you want to start playing with the quoting to get it to look the way that you want. I know that the path to the 7-zip exe has a space in it (thanks microsoft). So I know that needs to be quoted. I am going to assume that the user is evil and will at some point enter a path with a space in it, so I want to quote that too. So the next thing I do is to add my quoting function and quote those parts:
Option Explicit
Dim strArchivePath
Dim strCMD
strArchivePath = InputBox("Enter the path to the archive")
strCMD = qq("C:\Program Files\7-zip\7z.exe") & " l " & qq(strArchivePath)
WScript.Echo strCMD
Function qq(strIn)
qq = Chr(34) & strIn & Chr(34)
End Function
So now it looks like my command should be right. At this point I would verify that it actually is right by copying the output from the echo statement and pasting it into a command prompt to see if it really does run. In this case it did work for me so I would comment out the echo and go on with developing the rest of the script to actually run the command.
The key concept to take away form all this is to work out your command well before you ever try to run it in the script.
As always post comments or questions here if I missed something or anything was unclear.