View Full Version : passing a string with quotes as parameter


Michael Moser
10-19-2005, 05:38 PM
Is there REALLY, ABSOLUTELY NO WAY of passing a string that contains
double-quotes to a program?

I am writing a batch-file and I need to call one program with a
parameter that contains a '"' (a douple quote). I am going circles
trying to persuade CMD.EXE to swallow this.

Michael :-(

Michael Moser
10-19-2005, 05:46 PM
Ah - and I forgot: it also contains blanks, which why I have to quote it
in the first place, i.e. I need something like:

prgmname 'argument with "quotes" and blanks'

But - alas - using ' or ` as quotes does not work. I faintly remember,
that one could escape a " within a quoted string using "" or ^" , but
none of these appear to work, either...

Michael


"Michael Moser" <michael-nospam.moser@nospam.freesurf.ch> wrote in
message news:OM8RNuM1FHA.2924@TK2MSFTNGP15.phx.gbl...
> Is there REALLY, ABSOLUTELY NO WAY of passing a string that contains
> double-quotes to a program?
>
> I am writing a batch-file and I need to call one program with a
> parameter that contains a '"' (a douple quote). I am going circles
> trying to persuade CMD.EXE to swallow this.
>
> Michael :-(
>
>

foxidrive
10-19-2005, 06:38 PM
On Wed, 19 Oct 2005 18:46:30 +0200, Michael Moser wrote:

Tell us more about this program.

> Ah - and I forgot: it also contains blanks, which why I have to quote it
> in the first place, i.e. I need something like:
>
> prgmname 'argument with "quotes" and blanks'
>
> But - alas - using ' or ` as quotes does not work. I faintly remember,
> that one could escape a " within a quoted string using "" or ^" , but
> none of these appear to work, either...
>
> Michael
>
>
> "Michael Moser" <michael-nospam.moser@nospam.freesurf.ch> wrote in
> message news:OM8RNuM1FHA.2924@TK2MSFTNGP15.phx.gbl...
>> Is there REALLY, ABSOLUTELY NO WAY of passing a string that contains
>> double-quotes to a program?
>>
>> I am writing a batch-file and I need to call one program with a
>> parameter that contains a '"' (a douple quote). I am going circles
>> trying to persuade CMD.EXE to swallow this.
>>
>> Michael :-(
>>
>>

Al Dunbar
10-20-2005, 12:58 AM
"foxidrive" <woohoo@gotcha.invalid> wrote in message
news:1ta8ryl876xv0$.132e1uik48fxf.dlg@40tude.net...
> On Wed, 19 Oct 2005 18:46:30 +0200, Michael Moser wrote:
>
> Tell us more about this program.

A good point. Perhaps it is not CMD that is having difficulty swallowing
this.

I believe that cmd.exe will pass whatever is given on the commandline. The
following demonstrates this on Windows XP:

@echo off

call:sub param
call:sub "param"
call:sub "par"ram"
call:sub "par""ram"
goto:eof

:sub
echo/[%1]
goto:eof

The values displayed in the brackets by the echo command are all exactly
character for character what appears in the respective call:sub lines.

/Al

>> Ah - and I forgot: it also contains blanks, which why I have to quote it
>> in the first place, i.e. I need something like:
>>
>> prgmname 'argument with "quotes" and blanks'
>>
>> But - alas - using ' or ` as quotes does not work. I faintly remember,
>> that one could escape a " within a quoted string using "" or ^" , but
>> none of these appear to work, either...
>>
>> Michael
>>
>>
>> "Michael Moser" <michael-nospam.moser@nospam.freesurf.ch> wrote in
>> message news:OM8RNuM1FHA.2924@TK2MSFTNGP15.phx.gbl...
>>> Is there REALLY, ABSOLUTELY NO WAY of passing a string that contains
>>> double-quotes to a program?
>>>
>>> I am writing a batch-file and I need to call one program with a
>>> parameter that contains a '"' (a douple quote). I am going circles
>>> trying to persuade CMD.EXE to swallow this.
>>>
>>> Michael :-(
>>>
>>>

David Candy
10-20-2005, 01:23 AM
The program gets verbatim what is on the command line. It is code in the program that breaks parameters up. Different languages and environment do it differently. EG In JS (but not WSH) one need to manually break it up. Ditto for VB (except VB tremoves the program part of the command line leaving only the parameters). In WSH parameters are nicely in an array for you.



--
--------------------------------------------------------------------------------------------------
Read David defending the concept of violence.
http://margokingston.typepad.com/harry_version_2/2005/10/entering_the_ga.html#more
=================================================
"foxidrive" <woohoo@gotcha.invalid> wrote in message news:1ta8ryl876xv0$.132e1uik48fxf.dlg@40tude.net...
> On Wed, 19 Oct 2005 18:46:30 +0200, Michael Moser wrote:
>
> Tell us more about this program.
>
>> Ah - and I forgot: it also contains blanks, which why I have to quote it
>> in the first place, i.e. I need something like:
>>
>> prgmname 'argument with "quotes" and blanks'
>>
>> But - alas - using ' or ` as quotes does not work. I faintly remember,
>> that one could escape a " within a quoted string using "" or ^" , but
>> none of these appear to work, either...
>>
>> Michael
>>
>>
>> "Michael Moser" <michael-nospam.moser@nospam.freesurf.ch> wrote in
>> message news:OM8RNuM1FHA.2924@TK2MSFTNGP15.phx.gbl...
>>> Is there REALLY, ABSOLUTELY NO WAY of passing a string that contains
>>> double-quotes to a program?
>>>
>>> I am writing a batch-file and I need to call one program with a
>>> parameter that contains a '"' (a douple quote). I am going circles
>>> trying to persuade CMD.EXE to swallow this.
>>>
>>> Michael :-(
>>>
>>>

David Candy
10-20-2005, 03:14 AM
It tries to make sense of the command line but if not passes it verbatim to CreateProcess. This is why cmd can execute programs renamed to text files as CP doesn't care about extensions (except to add .exe to files without an extension) but opens the file to see how to execute (as there are many types of executable files).

lpCommandLine
[in, out] Pointer to a null-terminated string that specifies the command line to execute.
Windows NT/2000/XP: The Unicode version of this function, CreateProcessW, will fail if this parameter is a const string.

The lpCommandLine parameter can be NULL. In that case, the function uses the string pointed to by lpApplicationName as the command line.

If both lpApplicationName and lpCommandLine are non-NULL, *lpApplicationName specifies the module to execute, and *lpCommandLine specifies the command line. The new process can use GetCommandLine to retrieve the entire command line. C runtime processes can use the argc and argv arguments. Note that it is a common practice to repeat the module name as the first token in the command line.

If lpApplicationName is NULL, the first white-space – delimited token of the command line specifies the module name. If you are using a long file name that contains a space, use quoted strings to indicate where the file name ends and the arguments begin (see the explanation for the lpApplicationName parameter). If the file name does not contain an extension, .exe is appended. Therefore, if the file name extension is ..com, this parameter must include the .com extension. If the file name ends in a period (.) with no extension, or if the file name contains a path, .exe is not appended. If the file name does not contain a directory path, the system searches for the executable file in the following sequence:

1.. The directory from which the application loaded.
2.. The current directory for the parent process.
3.. Windows 95/98/Me: The Windows system directory. Use the GetSystemDirectory function to get the path of this directory.
Windows NT/2000/XP: The 32-bit Windows system directory. Use the GetSystemDirectory function to get the path of this directory. The name of this directory is System32.

4.. Windows NT/2000/XP: The 16-bit Windows system directory. There is no function that obtains the path of this directory, but it is searched. The name of this directory is System.
5.. The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
6.. The directories that are listed in the PATH environment variable.
The system adds a null character to the command line string to separate the file name from the arguments. This divides the original string into two strings for internal processing.


--
--------------------------------------------------------------------------------------------------
Read David defending the concept of violence.
http://margokingston.typepad.com/harry_version_2/2005/10/entering_the_ga.html#more
=================================================
"Al Dunbar" <AlanNOSPAmDrub@hotmail.com> wrote in message news:uLQdp8Q1FHA.3896@TK2MSFTNGP10.phx.gbl...
>
> "foxidrive" <woohoo@gotcha.invalid> wrote in message
> news:1ta8ryl876xv0$.132e1uik48fxf.dlg@40tude.net...
>> On Wed, 19 Oct 2005 18:46:30 +0200, Michael Moser wrote:
>>
>> Tell us more about this program.
>
> A good point. Perhaps it is not CMD that is having difficulty swallowing
> this.
>
> I believe that cmd.exe will pass whatever is given on the commandline. The
> following demonstrates this on Windows XP:
>
> @echo off
>
> call:sub param
> call:sub "param"
> call:sub "par"ram"
> call:sub "par""ram"
> goto:eof
>
> :sub
> echo/[%1]
> goto:eof
>
> The values displayed in the brackets by the echo command are all exactly
> character for character what appears in the respective call:sub lines.
>
> /Al
>
>>> Ah - and I forgot: it also contains blanks, which why I have to quote it
>>> in the first place, i.e. I need something like:
>>>
>>> prgmname 'argument with "quotes" and blanks'
>>>
>>> But - alas - using ' or ` as quotes does not work. I faintly remember,
>>> that one could escape a " within a quoted string using "" or ^" , but
>>> none of these appear to work, either...
>>>
>>> Michael
>>>
>>>
>>> "Michael Moser" <michael-nospam.moser@nospam.freesurf.ch> wrote in
>>> message news:OM8RNuM1FHA.2924@TK2MSFTNGP15.phx.gbl...
>>>> Is there REALLY, ABSOLUTELY NO WAY of passing a string that contains
>>>> double-quotes to a program?
>>>>
>>>> I am writing a batch-file and I need to call one program with a
>>>> parameter that contains a '"' (a douple quote). I am going circles
>>>> trying to persuade CMD.EXE to swallow this.
>>>>
>>>> Michael :-(
>>>>
>>>>
>
>

Michael Moser
10-20-2005, 08:23 AM
No secret: it's SED (the stream editor - part of the cygwin utils
(http://www.cygwin.com). I am trying to add a argument to an element in
an XML file:

I.e. I want to change a an element <toc> to <toc href="some_url_here">
so I was trying to specify:
sed "s/<toc>/<toc href="some_url_here">/"

It's the quotes that need to surround the URL that cause the trouble.
And since there is a space after "toc" in the second string this needs
to be quoted (otherwise I would only need to escape the redirection
symbols "<" and ">").

Michael


"foxidrive" <woohoo@gotcha.invalid> wrote in message
news:1ta8ryl876xv0$.132e1uik48fxf.dlg@40tude.net...
> On Wed, 19 Oct 2005 18:46:30 +0200, Michael Moser wrote:
>
> Tell us more about this program.
>
>> Ah - and I forgot: it also contains blanks, which why I have to quote
>> it
>> in the first place, i.e. I need something like:
>>
>> prgmname 'argument with "quotes" and blanks'
>>
>> But - alas - using ' or ` as quotes does not work. I faintly
>> remember,
>> that one could escape a " within a quoted string using "" or ^" , but
>> none of these appear to work, either...
>>
>> Michael
>>
>>
>> "Michael Moser" <michael-nospam.moser@nospam.freesurf.ch> wrote in
>> message news:OM8RNuM1FHA.2924@TK2MSFTNGP15.phx.gbl...
>>> Is there REALLY, ABSOLUTELY NO WAY of passing a string that contains
>>> double-quotes to a program?
>>>
>>> I am writing a batch-file and I need to call one program with a
>>> parameter that contains a '"' (a douple quote). I am going circles
>>> trying to persuade CMD.EXE to swallow this.
>>>
>>> Michael :-(
>>>
>>>

foxidrive
10-20-2005, 10:03 AM
On Thu, 20 Oct 2005 09:23:50 +0200, Michael Moser wrote:

> No secret: it's SED (the stream editor - part of the cygwin utils
> (http://www.cygwin.com). I am trying to add a argument to an element in
> an XML file:

Under GnuSED you specify an embedded double quote as \x22
(using its ascii HEX value ).


> I.e. I want to change a an element <toc> to <toc href="some_url_here">
> so I was trying to specify:
> sed "s/<toc>/<toc href="some_url_here">/"
>
> It's the quotes that need to surround the URL that cause the trouble.
> And since there is a space after "toc" in the second string this needs
> to be quoted (otherwise I would only need to escape the redirection
> symbols "<" and ">").
>
> Michael
>
>
> "foxidrive" <woohoo@gotcha.invalid> wrote in message
> news:1ta8ryl876xv0$.132e1uik48fxf.dlg@40tude.net...
>> On Wed, 19 Oct 2005 18:46:30 +0200, Michael Moser wrote:
>>
>> Tell us more about this program.
>>
>>> Ah - and I forgot: it also contains blanks, which why I have to quote
>>> it
>>> in the first place, i.e. I need something like:
>>>
>>> prgmname 'argument with "quotes" and blanks'
>>>
>>> But - alas - using ' or ` as quotes does not work. I faintly
>>> remember,
>>> that one could escape a " within a quoted string using "" or ^" , but
>>> none of these appear to work, either...
>>>
>>> Michael
>>>
>>>
>>> "Michael Moser" <michael-nospam.moser@nospam.freesurf.ch> wrote in
>>> message news:OM8RNuM1FHA.2924@TK2MSFTNGP15.phx.gbl...
>>>> Is there REALLY, ABSOLUTELY NO WAY of passing a string that contains
>>>> double-quotes to a program?
>>>>
>>>> I am writing a batch-file and I need to call one program with a
>>>> parameter that contains a '"' (a douple quote). I am going circles
>>>> trying to persuade CMD.EXE to swallow this.
>>>>
>>>> Michael :-(
>>>>
>>>>

Marty List
10-20-2005, 05:17 PM
"David Candy" <.> wrote in message news:uT$HPyQ1FHA.3780@TK2MSFTNGP12.phx.gbl...
> The program gets verbatim what is on the command line. It is code in the
> program that breaks parameters up. Different languages and environment do
> it differently. EG In JS (but not WSH) one need to manually break it up.
> Ditto for VB (except VB tremoves the program part of the command line
> leaving only the parameters). In WSH parameters are nicely in an array
> for you.


What you posted above is basically true, but there is what I consider to be a
bug in CommandLineToArgvW. This API function will remove the surrounding double
quotes. A while back I wrote a quick C++ executable to demonstrate this:


http://www.optimumx.com/download/CommandLineTest.exe


C:\>CommandLineTest.exe "123" "4 5 6"


Display the entire command line using GetCommandLineA():

CommandLineTest.exe "123" "4 5 6"


Display the entire command line using GetCommandLineW():

CommandLineTest.exe "123" "4 5 6"


Display each parameter in the command line using argc/argv from main():

0=[CommandLineTest.exe]
1=[123]
2=[4 5 6]


Display each parameter in the command line using CommandLineToArgvW():

0=[CommandLineTest.exe]
1=[123]
2=[4 5 6]

billious
10-20-2005, 05:35 PM
"Michael Moser" <michael-nospam.moser@nospam.freesurf.ch> wrote in message
news:eTVW5cU1FHA.3864@TK2MSFTNGP12.phx.gbl...
>
> No secret: it's SED (the stream editor - part of the cygwin utils
> (http://www.cygwin.com). I am trying to add a argument to an element in an
> XML file:
>
> I.e. I want to change a an element <toc> to <toc href="some_url_here"> so
> I was trying to specify:
> sed "s/<toc>/<toc href="some_url_here">/"
>
> It's the quotes that need to surround the URL that cause the trouble. And
> since there is a space after "toc" in the second string this needs to be
> quoted (otherwise I would only need to escape the redirection symbols "<"
> and ">").
>
> Michael

Hmmm... I use HHSED, and I don't claim to be an expert on all the world's
SED versions.

I'd use

SED s/\x3ctoc\x3e/\x3ctoc href=\x22some_url_here\x22\x3c/

myself. Or possibly i'd put the
s/<toc>/<toc href="some_url_here">/
into a file and use SED -f filename

Perhaps Eric Pement's www.unixguide.net/unix/sedoneliner.shtml or
www.cornerstonemag.com/sed might be of aid in your quest...

HTH

....Bill

Joe Richards [MVP]
10-21-2005, 01:50 AM
You need to escape the quotes with a slash like so

"param1" "param2 \"some subparam of param 2\""


--
Joe Richards Microsoft MVP Windows Server Directory Services
www.joeware.net


Michael Moser wrote:
> Is there REALLY, ABSOLUTELY NO WAY of passing a string that contains
> double-quotes to a program?
>
> I am writing a batch-file and I need to call one program with a
> parameter that contains a '"' (a douple quote). I am going circles
> trying to persuade CMD.EXE to swallow this.
>
> Michael :-(
>
>

Michael Moser
10-22-2005, 10:15 PM
Bingo! You are my hero! Thanks a lot!

Michael

"Joe Richards [MVP]" <humorexpress@hotmail.com> wrote in message
news:O$M9tld1FHA.3376@TK2MSFTNGP14.phx.gbl...
> You need to escape the quotes with a slash like so
>
> "param1" "param2 \"some subparam of param 2\""
>
>
> --
> Joe Richards Microsoft MVP Windows Server Directory Services
> www.joeware.net
>
>
> Michael Moser wrote:
>> Is there REALLY, ABSOLUTELY NO WAY of passing a string that contains
>> double-quotes to a program?
>>
>> I am writing a batch-file and I need to call one program with a
>> parameter that contains a '"' (a douple quote). I am going circles
>> trying to persuade CMD.EXE to swallow this.
>>
>> Michael :-(
>>

Marty List
10-23-2005, 03:16 AM
"Michael Moser" <michael-nospam.moser@nospam.freesurf.ch> wrote in message
news:%230bvz201FHA.1564@tk2msftngp13.phx.gbl...
> Bingo! You are my hero! Thanks a lot!
>
> Michael

You're my hero too, Joe.

Joe Richards [MVP]
10-25-2005, 10:03 PM
LOL.

--
Joe Richards Microsoft MVP Windows Server Directory Services
www.joeware.net


Marty List wrote:
>
> You're my hero too, Joe.
>
>
>