Question about nested quotes

Discuss and share scripts and script files...
Post Reply
Huidong
Posts: 213
Joined: 18 May 2011 21:55

Question about nested quotes

Post 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.

PeterH
Posts: 2827
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: Question about nested quotes

Post 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)

Huidong
Posts: 213
Joined: 18 May 2011 21:55

Re: Question about nested quotes

Post 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...

bdeshi
Posts: 4256
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612
Contact:

Re: Question about nested quotes

Post 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
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: Question about nested quotes

Post 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

Huidong
Posts: 213
Joined: 18 May 2011 21:55

Re: Question about nested quotes

Post 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!

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: Question about nested quotes

Post 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!

Huidong
Posts: 213
Joined: 18 May 2011 21:55

Re: Question about nested quotes

Post by Huidong »

Thank you TheQwerty!

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

Filehero
Posts: 2731
Joined: 27 Feb 2012 18:50
Location: Windows 11@100%

Re: Question about nested quotes

Post by Filehero »

@TheQwerty

:appl:


Cheers,
Filehero

Filehero
Posts: 2731
Joined: 27 Feb 2012 18:50
Location: Windows 11@100%

Re: Question about nested quotes

Post 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

PeterH
Posts: 2827
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: Question about nested quotes

Post 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: )

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: Question about nested quotes

Post 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.

PeterH
Posts: 2827
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: Question about nested quotes

Post by PeterH »

Oh, sometimes, ... :oops:

:appl: :appl: :appl:

Huidong
Posts: 213
Joined: 18 May 2011 21:55

Re: Question about nested quotes

Post 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!

Post Reply