Page 1 of 1

Question about nested quotes

Posted: 03 Jun 2014 07:10
by Huidong
Sorry but I couldn't figure out how to make 3rd-level quotes work. For instance, I'm using Console to run cmd commands, the general syntax is as follows:

Code: Select all

console -r "some command"
In particular, I'm trying to run a utility "pdfinfo" that takes file path-names as arguments

Code: Select all

run "Console.exe -r ""/k D:\Xpdf\pdfinfo.exe """"<curitem>""""""";
I thought since <curitem> is the 3rd-level nested quotes, it should have 4 pairs of double-quotes around it. But that doesn't work as long as the path-name contains space.

So my question is, in such scenarios requiring more than 2 levels of quotes, how to do the quoting properly to handle path-names with space?

Thank you very much.

Re: Question about nested quotes

Posted: 03 Jun 2014 11:47
by PeterH
1) add step; command before run, and have a look at what debug window shows.

2) as long as there are no XY vars inside the quotes, use single quotes as outer quotes - then you must not double the double quotes inside. (step can help here too to verify the result...)
(Sorry for the double ... double :wink: but it's just like that)

Re: Question about nested quotes

Posted: 03 Jun 2014 14:39
by Huidong
Hmm, wondering what's the rationality behind just changing double-quotes to single-quotes?
as long as there are no XY vars inside the quotes, use single quotes as outer quotes
but I do have a variable inside quotes, so single quotes won't work here? Or did you mean something different here?

btw I tried using all single quotes but not working...

Re: Question about nested quotes

Posted: 03 Jun 2014 14:50
by bdeshi
Try different quotings with SC text, see which output matches the command you want to execute.

Code: Select all

text "Console.exe -r /k """"D:\Xpdf\pdfinfo.exe ""<curitem>""""""";
outputs

Code: Select all

Console.exe -r /k ""D:\Xpdf\pdfinfo.exe "C:\Portable\XYplorer\XYData\servers.dat"""
So exactly this command will run when the same string is used with SC run. Of course the Console app has to support nested quotes itself...

edit:quote->code

Re: Question about nested quotes

Posted: 03 Jun 2014 15:27
by TheQwerty
You have to start by figuring out how console.exe handles accepting the command parameter of '-r' and in particular how it handles the need for quoting within the command.

For this, completely ignore XY, hop on down to command prompt town, and start experimenting. It seems that to use quotes within the command console wants them to be escaped with a '\' so you might have:

Code: Select all

console.exe -r "/k dir \"C:\Program Files\""
Now using that knowledge attempt to run it via XY's run command - keep in mind that the whole argument needs quotes and therefore any quotes within the argument must be escaped by doubling them:

Code: Select all

run "console.exe -r ""/k dir \""C:\Program Files\""""";
Once you have that working, try replacing the literal path with the desired variables:

Code: Select all

run "console.exe -r ""/k dir \""<curpath>\""""";
So your final result might be something like:

Code: Select all

run "console.exe -r ""/k D:\Xpdf\pdfinfo.exe \""<curitem>\""""";
To better understand that mess of quotes let's break it down a little with some white space and explain the 3 sets of surrounding quotes:

Code: Select all

run "console.exe -r ""/k D:\Xpdf\pdfinfo.exe \""<curitem>\"" "" ";
    1               2                         3           3  2  1
1: These are the quotes that must surround the entire argument of XY's run.
2: These are the quotes that surround console.exe -r's command parameter, because they are within 1 they need to be escaped for XY by doubling them.
3: These are the quotes within the command parameter that surround the argument to pdfinfo.exe. Like 2, because these are within 1 they need to be doubled for XY. However they are also within 2 which means they need to be escaped by '\' for console.exe to handle them correctly.


HTH

Re: Question about nested quotes

Posted: 03 Jun 2014 15:48
by Huidong
TheQwerty wrote:You have to start by figuring out how console.exe handles accepting the command parameter of '-r' and in particular how it handles the need for quoting within the command.

For this, completely ignore XY, hop on down to command prompt town, and start experimenting. It seems that to use quotes within the command console wants them to be escaped with a '\' so you might have:

Code: Select all

console.exe -r "/k dir \"C:\Program Files\""
Now using that knowledge attempt to run it via XY's run command - keep in mind that the whole argument needs quotes and therefore any quotes within the argument must be escaped by doubling them:

Code: Select all

run "console.exe -r ""/k dir \""C:\Program Files\""""";
Once you have that working, try replacing the literal path with the desired variables:

Code: Select all

run "console.exe -r ""/k dir \""<curpath>\""""";
So your final result might be something like:

Code: Select all

run "console.exe -r ""/k D:\Xpdf\pdfinfo.exe \""<curitem>\""""";
To better understand that mess of quotes let's break it down a little with some white space and explain the 3 sets of surrounding quotes:

Code: Select all

run "console.exe -r ""/k D:\Xpdf\pdfinfo.exe \""<curitem>\"" "" ";
    1               2                         3           3  2  1
1: These are the quotes that must surround the entire argument of XY's run.
2: These are the quotes that surround console.exe -r's command parameter, because they are within 1 they need to be escaped for XY by doubling them.
3: These are the quotes within the command parameter that surround the argument to pdfinfo.exe. Like 2, because these are within 1 they need to be doubled for XY. However they are also within 2 which means they need to be escaped by '\' for console.exe to handle them correctly.


HTH
This absolutely works, and, absolutely makes absolute sense! I didn't expect an answer of this level of expertise and kindness. Thank you HTH.

SammaySarkar raised the question about how Console handles nested quotes, and I figured it just wasn't able to handle deeper quotes within the argument for -r. But you even figured out the proper escaping to make it work!

Just a note, previously I idiotically thought that for each deeper level of nested quotes, you'd have to double the number of quotes, so that'd be an exponential growth of quotes, obviously that's studpid!

Re: Question about nested quotes

Posted: 03 Jun 2014 15:53
by TheQwerty
Huidong wrote:Thank you HTH.
You're welcome, but "HTH" was "Hope that helps" not a sign off. ;)

Just glad it makes some sense now!

Re: Question about nested quotes

Posted: 03 Jun 2014 15:56
by Huidong
Thank you TheQwerty!

And thanks again for kindly pointing out what HTH means. It's kind of embarrassing but good to know.

Re: Question about nested quotes

Posted: 03 Jun 2014 18:51
by Filehero
@TheQwerty

:appl:


Cheers,
Filehero

Re: Question about nested quotes

Posted: 04 Jun 2014 06:43
by Filehero
Full-quote and bumping on purpose.
TheQwerty wrote:You have to start by figuring out how console.exe handles accepting the command parameter of '-r' and in particular how it handles the need for quoting within the command.

For this, completely ignore XY, hop on down to command prompt town, and start experimenting. It seems that to use quotes within the command console wants them to be escaped with a '\' so you might have:

Code: Select all

console.exe -r "/k dir \"C:\Program Files\""
Now using that knowledge attempt to run it via XY's run command - keep in mind that the whole argument needs quotes and therefore any quotes within the argument must be escaped by doubling them:

Code: Select all

run "console.exe -r ""/k dir \""C:\Program Files\""""";
Once you have that working, try replacing the literal path with the desired variables:

Code: Select all

run "console.exe -r ""/k dir \""<curpath>\""""";
So your final result might be something like:

Code: Select all

run "console.exe -r ""/k D:\Xpdf\pdfinfo.exe \""<curitem>\""""";
To better understand that mess of quotes let's break it down a little with some white space and explain the 3 sets of surrounding quotes:

Code: Select all

run "console.exe -r ""/k D:\Xpdf\pdfinfo.exe \""<curitem>\"" "" ";
    1               2                         3           3  2  1
1: These are the quotes that must surround the entire argument of XY's run.
2: These are the quotes that surround console.exe -r's command parameter, because they are within 1 they need to be escaped for XY by doubling them.
3: These are the quotes within the command parameter that surround the argument to pdfinfo.exe. Like 2, because these are within 1 they need to be doubled for XY. However they are also within 2 which means they need to be escaped by '\' for console.exe to handle them correctly.
This is textbook quality & a real helper -> deserves to made sticky somewhere!
Maybe Tips: Managing Scripts could/should be extended to collect those scripting gems?


Cheers,
Filehero

Re: Question about nested quotes

Posted: 04 Jun 2014 12:59
by PeterH
I see: it's personal preference! But here is mine:

Compare:

Code: Select all

 run "console.exe -r ""/k D:\Xpdf\pdfinfo.exe \""<curitem>\"" "" ";
     1               2                         3           3  2  1

 run 'console.exe -r "/k D:\Xpdf\pdfinfo.exe \"'.<curitem>.'\" " ';
     1               2                        31           1 3 2 1
(OK: I had to take the variable out of the single quotes, and concatenate it :roll: )

But probably I would use:

Code: Select all

   $cmd = 'console.exe -r "/k D:\Xpdf\pdfinfo.exe \"'.<curitem>.'\" " ';
// Msg "cmd = '$cmd'";
   run $cmd;
(You see: prepared to debug :biggrin: )

Re: Question about nested quotes

Posted: 04 Jun 2014 14:28
by TheQwerty
We're all forgetting HEREDOCs...

Code: Select all

run <<<CMD
console.exe -r "/k D:\Xpdf\pdfinfo.exe \"<curitem>\""
CMD;
No need to escape for XY - only for Console.
This does however requires a multi-line script, but that's not a huge hurdle as you can make it a UDC or script file and then call that where only single-line scripts are allowed.

Re: Question about nested quotes

Posted: 04 Jun 2014 14:30
by PeterH
Oh, sometimes, ... :oops:

:appl: :appl: :appl:

Re: Question about nested quotes

Posted: 04 Jun 2014 17:41
by Huidong
TheQwerty wrote:We're all forgetting HEREDOCs...

Code: Select all

run <<<CMD
console.exe -r "/k D:\Xpdf\pdfinfo.exe \"<curitem>\""
CMD;
No need to escape for XY - only for Console.
This does however requires a multi-line script, but that's not a huge hurdle as you can make it a UDC or script file and then call that where only single-line scripts are allowed.
Damn, this is so convenient, and much neater, easier to read! Thanks for letting us know, again!