Discussion:
?? Passing Double-Quotes on the Command Line ??
(too old to reply)
Alan Foxmore
2006-04-12 18:11:13 UTC
Permalink
Hi Everyone,

I don't know if this is so much a VBS question as a Windows question.

I want to pass several command line parameters to a VBS. Some of the
command line parameters have embedded double-quotation marks.

For example:
MyScript.vbs "title=She said, "Hello""

Unfortunately, I don't know how to successfully pass double quotation
marks on the command line. Using a backslash to escape the double
quotes does not work.

For example, this fails:
MyScript.vbs "title=She said, \"Hello\""

Is there a way to pass embedded double-quotes on the command line? This
seems so simple, it must be possible. I've searched Google high and low
to no avail.

Thanks much.
--
Alan
JTW
2006-04-12 18:38:04 UTC
Permalink
have a look at:

http://www.windowsitpro.com/WindowsScripting/Article/ArticleID/43751/437
51.html
and
http://www.windowsitpro.com/Article/ArticleID/48219/48219.html

Command line can be sent as:

MyScript.vbs "title=She said, %22Hello%22"

and within:

WScript.Echo Unescape(WScript.Arguments.Item(0))
Alan Foxmore
2006-04-12 19:18:37 UTC
Permalink
Post by JTW
http://www.windowsitpro.com/WindowsScripting/Article/ArticleID/43751/437
51.html
and
http://www.windowsitpro.com/Article/ArticleID/48219/48219.html
MyScript.vbs "title=She said, %22Hello%22"
Oh God, that's ugly. I feel sorry for the poor user.
ekkehard.horner
2006-04-12 19:31:31 UTC
Permalink
Post by Alan Foxmore
Post by JTW
http://www.windowsitpro.com/WindowsScripting/Article/ArticleID/43751/437
51.html
and
http://www.windowsitpro.com/Article/ArticleID/48219/48219.html
MyScript.vbs "title=She said, %22Hello%22"
Oh God, that's ugly. I feel sorry for the poor user.
You could enter |"this is 'a' test"| and change the |'| to |"|
with Replace():

For Each sArg In WScript.Arguments
WScript.Echo "|" + sArg + "|"
WScript.Echo "|" + Unescape( sArg ) + "|"
WScript.Echo "|" + Replace( sArg, "'", """" ) + "|"
Next

but I (not a user) like the %nn method better, because next
the user will try to enter |title=Oh "God", that's ugly.|
Alan Foxmore
2006-04-12 20:01:56 UTC
Permalink
Post by ekkehard.horner
Post by Alan Foxmore
Post by JTW
http://www.windowsitpro.com/WindowsScripting/Article/ArticleID/43751/437
51.html
and
http://www.windowsitpro.com/Article/ArticleID/48219/48219.html
MyScript.vbs "title=She said, %22Hello%22"
Oh God, that's ugly. I feel sorry for the poor user.
You could enter |"this is 'a' test"| and change the |'| to |"|
For Each sArg In WScript.Arguments
WScript.Echo "|" + sArg + "|"
WScript.Echo "|" + Unescape( sArg ) + "|"
WScript.Echo "|" + Replace( sArg, "'", """" ) + "|"
Next
but I (not a user) like the %nn method better, because next
the user will try to enter |title=Oh "God", that's ugly.|
Right -- I think this is really a Windows issue. VBS just happens to be
the language in this case.

I just have to think Micorsoft has provided for this Embedded Double
Quote Issue. I want to believe there's some setting or escape sequence
we're missing here.

--
Alan
JTW
2006-04-12 20:16:05 UTC
Permalink
It isn't global within Windows; you can embed quotes on command lines
for many processes, the Windows Installer for example
(http://msdn.microsoft.com/library/en-us/msi/setup/command_line_options.
asp?frame=true).
Alan Foxmore
2006-04-12 22:28:18 UTC
Permalink
Post by JTW
It isn't global within Windows; you can embed quotes on command lines
for many processes, the Windows Installer for example
(http://msdn.microsoft.com/library/en-us/msi/setup/command_line_options.
asp?frame=true).
This turns out to be, I think, WSH issue, pure and simple. Windows
itself seems to have no problem with embedded double-quotes on the
command line -- you simply specify them as *two* juxtaposed
double-quotes (e.g., "").

Look at these examples. Here is a simple batch file that prints it's
first two command line parameters:

@echo off
echo --%1--
echo --%2--

When you run this batch file with these two command line arguments:
"This ""is"" a test" "and so is this"

Here's the output you get:
--"This ""is"" a test"--
--"and so is this"--

Notice the double-quotes are maintained.


Now let's write an analogous VBS program:

WScript.Echo "--" & WScript.Arguments.Item(0) & "--"
WScript.Echo "--" & WScript.Arguments.Item(1) & "--"

Running this VBS with the same command line yields:
--This is a test--
--and so is this--

So, by the time your VBS program gets hold of the WScript.Arguments
object the embedded double-quotes have been stripped away.

The reason non-VBS can use the embedded double-quotes onthe command
line is because they are not running as a client of scripting host
which massages the command line behind the scenes, removing any
embedded double quotes.

--
Alan
D.P. Roberts
2006-04-13 16:02:20 UTC
Permalink
Try this:

"title=She said, ""Hello"""
Post by Alan Foxmore
Hi Everyone,
I don't know if this is so much a VBS question as a Windows question.
I want to pass several command line parameters to a VBS. Some of the
command line parameters have embedded double-quotation marks.
MyScript.vbs "title=She said, "Hello""
Unfortunately, I don't know how to successfully pass double quotation
marks on the command line. Using a backslash to escape the double quotes
does not work.
MyScript.vbs "title=She said, \"Hello\""
Is there a way to pass embedded double-quotes on the command line? This
seems so simple, it must be possible. I've searched Google high and low to
no avail.
Thanks much.
--
Alan
Alan Foxmore
2006-04-13 16:39:34 UTC
Permalink
Post by D.P. Roberts
"title=She said, ""Hello"""
Did you try that? It doesn't work.
D.P. Roberts
2006-04-13 17:26:20 UTC
Permalink
Yes, I tried it in a msgbox function and it worked, whereas your posted
syntax did not work.
Post by Alan Foxmore
Post by D.P. Roberts
"title=She said, ""Hello"""
Did you try that? It doesn't work.
Alan Foxmore
2006-04-14 05:39:21 UTC
Permalink
Post by D.P. Roberts
Yes, I tried it in a msgbox function and it worked, whereas your
posted syntax did not work.
msgbox?? Who's talking about msgbox? This has nothing to do with
msgbox, man. Did you even read the original post?
Michael Harris (MVP)
2006-04-15 02:37:48 UTC
Permalink
Post by Alan Foxmore
Post by D.P. Roberts
Yes, I tried it in a msgbox function and it worked, whereas your
posted syntax did not work.
msgbox?? Who's talking about msgbox? This has nothing to do with
msgbox, man. Did you even read the original post?
Just to put the question to rest, the inability of including or escaping
literal double quotes ( "=chr(34) ) is a design shortcoming of WSH command
line parsing. It was reported to MS either shortly after WSB 5.6 released
or too late in the 5.6 public beta cycle to be fixed (I don't recall which).
At any rate, it is unikely to ever be changed or fixed (even minor bugs are
sometimes preserved for backward compatibility) due to the 'sustained
engineering' mode WSH (COM based) entered when .Net was released.
--
Michael Harris
Microsoft MVP Scripting
Continue reading on narkive:
Loading...