How to put inner quotes in outer quotes in "for" loop?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
pstein
Posts: 125
Joined: 09 Nov 2011 01:42

How to put inner quotes in outer quotes in "for" loop?

#1 Post by pstein » 19 Jul 2015 09:39

Assume the following code:

Code: Select all

set mypath=D:\aaa\bbb ccc\ddd eee\
set myfile=D:\eee fff\ggg hhh\file123.txt
FOR /F %%A IN ('"%mypath%\myprog" -someparameter "%myfile%"') DO SET %%A

When I run this batch script I got an error

'D:\aaa\bbb' is not recognized as an internal or external command,
operable program or batch file.
( was unexpected at this time.

So the batch interpreter does not accept the inner double quotes.

How else can I quote the inner blank in pathes/filesnames inside outer single quotes?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to put inner quotes in outer quotes in "for" loop?

#2 Post by foxidrive » 19 Jul 2015 10:05

I've come across this in a tool written for multiple OS's.

Your syntax and logic is not faulty - the tool you are using is.

You would have to confirm that this still fails though:

Code: Select all

set "mypath=D:\aaa\bbb ccc\ddd eee\"
set "myfile=D:\eee fff\ggg hhh\file123.txt"
FOR /F %%A IN ('"%mypath%\myprog" -someparameter "%myfile%"') DO echo SET "%%~A"

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: How to put inner quotes in outer quotes in "for" loop?

#3 Post by Compo » 19 Jul 2015 10:38

You can try using changing to the directory containing your executable, using e.g. PUSHD like here, or by adding your executable location to PATH.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: How to put inner quotes in outer quotes in "for" loop?

#4 Post by dbenham » 19 Jul 2015 11:00

The problem has to do with how FOR /F executes the command. You can test via the following script:

Code: Select all

@echo off
for /f "delims=" %%A in ('echo %%cmdcmdline%%') do echo %%A

--OUTPUT--

Code: Select all

C:\Windows\system32\cmd.exe /c echo %cmdcmdline%

So now plug your command into the mix, and it attempts to execute:

Code: Select all

C:\Windows\system32\cmd.exe /c "C:\some path\myprog" -someparameter "some file"

You might think that is OK, but if fails because of item 2. from the following extract of CMD /?

Code: Select all

If /C or /K is specified, then the remainder of the command line after
the switch is processed as a command line, where the following logic is
used to process quote (") characters:

    1.  If all of the following conditions are met, then quote characters
        on the command line are preserved:

        - no /S switch
        - exactly two quote characters
        - no special characters between the two quote characters,
          where special is one of: &<>()@^|
        - there are one or more whitespace characters between the
          two quote characters
        - the string between the two quote characters is the name
          of an executable file.

    2.  Otherwise, old behavior is to see if the first character is
        a quote character and if so, strip the leading character and
        remove the last quote character on the command line, preserving
        any text after the last quote character.

The first and last quotes are stripped, so the command becomes:

Code: Select all

C:\some path\myprog" -someparameter "some file

The solution is pretty simple: Just add an extra pair of enclosing escaped quotes:

Code: Select all

FOR /F %%A IN ('^""%mypath%\myprog" -someparameter "%myfile%"^"') DO SET %%A


Dave Benham

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to put inner quotes in outer quotes in "for" loop?

#5 Post by foxidrive » 19 Jul 2015 11:36

So it wasn't the tool all along. Thanks Dave.

JFTR you don't need to escape the quotes. As shown here it works with plain quotes.

Code: Select all

for /f "tokens=1,2 delims=." %%b in (
 '" "c:\files\bat\util\MediaInfo\MediaInfo.exe" -f --output=General;%%Duration/String3%% "%%~a" "'
) do call :chapter "%%~b" "%%~c"

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: How to put inner quotes in outer quotes in "for" loop?

#6 Post by dbenham » 19 Jul 2015 11:59

foxidrive wrote:JFTR you don't need to escape the quotes. As shown here it works with plain quotes.

Code: Select all

for /f "tokens=1,2 delims=." %%b in (
 '" "c:\files\bat\util\MediaInfo\MediaInfo.exe" -f --output=General;%%Duration/String3%% "%%~a" "'
) do call :chapter "%%~b" "%%~c"

No, best to escape the quotes so that the correct characters are quoted at all times.

For example, if the command before incorporating into FOR /F is

Code: Select all

"some path\test.bat" "this&that"

Then this will fail because the & is not quoted during the first parsing pass:

Code: Select all

for /f "delims=" %%A in ('""some path\test.bat" "this&that""') do echo %%A

But this works properly:

Code: Select all

for /f "delims=" %%A in ('^""some path\test.bat" "this&that"^"') do echo %%A

You don't even need poison characters to have problems, because any number of consecutive token delimiters are collapsed into a single space if they are not quoted or escaped.

test.bat

Code: Select all

@echo %~1


test2.bat

Code: Select all

@echo off
for /f "delims=" %%A in ('""c:\test\test.bat"" "A = B""') do echo %%A
--OUTPUT--

Code: Select all

A B


test3.bat

Code: Select all

@echo off
for /f "delims=" %%A in ('^""c:\test\test.bat"" "A = B"^"') do echo %%A
--OUTPUT--

Code: Select all

A = B



Dave Benham

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to put inner quotes in outer quotes in "for" loop?

#7 Post by foxidrive » 19 Jul 2015 13:46

dbenham wrote:No, best to escape the quotes so that the correct characters are quoted at all times.

Dave Benham


I tested also and once again your logic is flawless, and well explained to boot. Thanks Dave.

It's clear that I had never encountered filenames to trip me up and so I thought I'd stumbled upon a workaround. :oops:

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: How to put inner quotes in outer quotes in "for" loop?

#8 Post by Aacini » 19 Jul 2015 17:15

foxidrive wrote:It's clear that I had never encountered filenames to trip me up and so I thought I'd stumbled upon a workaround. :oops:

You should be proud of that instead of embarrassed! :D

OT: I have NOT antivirus installed on my computers and I never got a computer virus! 8)

Antonio

pstein
Posts: 125
Joined: 09 Nov 2011 01:42

Re: How to put inner quotes in outer quotes in "for" loop?

#9 Post by pstein » 20 Jul 2015 06:45

Code: Select all

FOR /F %%A IN ('^""%mypath%\myprog" -someparameter "%myfile%"^"') DO SET %%A


This works great.
Thank you

Jerome
Posts: 1
Joined: 07 Mar 2022 23:58

Re: How to put inner quotes in outer quotes in "for" loop?

#10 Post by Jerome » 08 Mar 2022 00:11

Hi there,

The previous thread looks great but I'm still having trouble with quotes and was wondering if anyone can help as I'm going crazy trying to figure out the syntax!

Code: Select all

FOR /F "tokens=*" %%a IN ('^""\\mydomain.local\netlogon\system_files\10.0\dsquery.exe" * OU=MyOU,dc=mydomain,dc=local-l -attr emQualifications title mobile emPosition telephoneNumber mail emBanner emPosition emUniPosition emPrenameTitle -filter "cn=%FULLNAME%"^"') DO (
  SET /a X+=1
  SET USERINFO[!X!]=%%a
)
Any help massively appreciated.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: How to put inner quotes in outer quotes in "for" loop?

#11 Post by dbenham » 08 Mar 2022 11:31

I am not familiar with dsquery.exe, so I don't know if the commas in OU=MyOU,dc=mydomain,dc=local-l are important, or if they can be converted to spaces.

But the FOR /F will convert unquoted commas into spaces. If that is the problem, then I think the following should work:

Code: Select all

FOR /F "tokens=*" %%a IN ('^""\\mydomain.local\netlogon\system_files\10.0\dsquery.exe" * OU=MyOU^,dc=mydomain^,dc=local-l -attr emQualifications title mobile emPosition telephoneNumber mail emBanner emPosition emUniPosition emPrenameTitle -filter "cn=%FULLNAME%"^"') DO (
  SET /a X+=1
  SET USERINFO[!X!]=%%a
)

Izya Kurvitch
Posts: 16
Joined: 15 Jul 2019 15:14

Re: How to put inner quotes in outer quotes in "for" loop?

#12 Post by Izya Kurvitch » 07 Jul 2022 10:01

Does anybody know what's happened with Dave and where his pure integral perfect code? :) :?:

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: How to put inner quotes in outer quotes in "for" loop?

#13 Post by dbenham » 07 Jul 2022 16:28

I am lurking in the shadows. :twisted:

Actually I have found another hobby that has been sucking up my time - VCV Rack, a Eurorack modular synth emulator for the computer. I am learning all I can about synthesis, as well as creating patches that play themselves so I can use them as accompaniment for my Native American flute improvisations. I perform a couple pieces nearly every week in a Virtual Open Mic over Zoom. Many of them I record and mix down and post to YouTube. You can see a bunch at https://www.youtube.com/c/DaveBenhamMusic/videos

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: How to put inner quotes in outer quotes in "for" loop?

#14 Post by Compo » 08 Jul 2022 04:53

Jerome wrote:
08 Mar 2022 00:11
<SNIP />

Code: Select all

FOR /F "tokens=*" %%a IN ('^""\\mydomain.local\netlogon\system_files\10.0\dsquery.exe" * OU=MyOU,dc=mydomain,dc=local-l -attr emQualifications title mobile emPosition telephoneNumber mail emBanner emPosition emUniPosition emPrenameTitle -filter "cn=%FULLNAME%"^"') DO (
  SET /a X+=1
  SET USERINFO[!X!]=%%a
)
<SNIP />
I would assume that you would need to escape not only the unquoted comma's but also the unquoted equals characters.

Code: Select all

('^""\\mydomain.local\netlogon\system_files\10.0\dsquery.exe" * OU^=MyOU^,dc^=mydomain^,dc^=local-l -attr emQualifications title mobile emPosition telephoneNumber mail emBanner emPosition emUniPosition emPrenameTitle -filter "cn=%FULLNAME%"^"')

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to put inner quotes in outer quotes in "for" loop?

#15 Post by aGerman » 08 Jul 2022 09:31

Good to hear you're doing well, Dave :D 👍

Post Reply