1

I am a new bee for vbScript. I have a script that I am calling with an arguments which contains double quote. Following is the call .vbs "a" 2 where variable a is JO"N

My VbScript:

If WScript.Arguments.Count > 0 Then     
MsgBox "NAME:" + WScript.Arguments.Item(0) 
MsgBox "NAME:" + WScript.Arguments.Item(1)
Else    
MsgBox "Please pass a parameter to this script" 
End if

*Note this works when a = JON but does not work when a= JO"N. I tried putting escape characters" (Example: "WScript.Arguments.Item(0)") but it does not work.

KRS
  • 19
  • 2
  • Does this answer your question? [About using Double quotes in Vbscript](https://stackoverflow.com/questions/15770599/about-using-double-quotes-in-vbscript) – user692942 Dec 07 '19 at 15:10

2 Answers2

1

You can't.

At least, as the WScript argument parser handles and removes all quotes from the values in Arguments collection, you can not use the default argument handling for this task.

note: And we are leaving out of the problem if the final command you are running when calling your script (you have not included how/from where you make the call) will or not have problems because the additional quote interferes argument quoting rules.

You need to use some workaround to get the double quote to reach the script. Some approachs could be:

  • Replace the quote and any other problematic character (use some kind of escape sequence) before calling the script and revert the process inside your script.

  • Save the value you want to pass into an environment variable (how to make it depends on how you are callign the script) and then retrieve the value from your script (ex. here).

  • Use WMI to retrieve the full command line used to start the script, including all the quotes (ex. here) and write your own argument parser routine.

From my point of view, I would use the second option.

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • "Replace the quote and any other problematic character (use some kind of escape sequence) before calling the script and revert the process inside your script." Worked well. Thanks! – KRS Dec 11 '19 at 15:50
0

This will work: Chr(34) & Wscript.Arguments.Item(0) & Chr(34). Here Chr(34) function returns a double quote using its ASCII code 34.

Wasif
  • 14,755
  • 3
  • 14
  • 34
  • I tried the above and it did not work. MsgBox "NAME:" + Chr(34) & WScript.Arguments.Item(2) & Chr(34) gives same error: 13 Type mismatch – KRS Dec 11 '19 at 14:13
  • Please note that you have used + in place of & so it gives the error. – Wasif Dec 11 '19 at 15:17
  • So the correct statement should be: MsgBox "NAME:" & Chr(34) & WScript.Arguments.Item(2) & Chr(34) ? – KRS Dec 11 '19 at 15:59