How to put path with blanks into "for" command? Masking & quotes

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
tobwz
Posts: 30
Joined: 25 Feb 2022 03:21

How to put path with blanks into "for" command? Masking & quotes

#1 Post by tobwz » 13 Jun 2022 01:39

Assume I want to execute a command like the following from within a DOS batch script:

for /F "Tokens= Usebackq" %%G in (' "D:\some\path\with blanks inside\myprog.exe" -p "......" %1') do set "....."

then I get an error like:

'D:\some\path\with' is not recognized as an internal or external command,operable program or batch file.

When I put myprog,.exe into a directory without blanks everything work.
However I prefer to have a path WITH blanks.

How can I get the command working anyway?
How can I mask the blanks?
Mind the single quote after opening bracket which should enclose all inner double quotes (from my point of view)

Similar question:

What if I want to put the path to program into a variable. The following does not work:

set pathtoprog=D:\some\path\with blanks inside
for /F "Tokens= Usebackq" %%G in (' "%pathtoprog%\myprog.exe" -p "......" %1') do set "....."

How can I code this successfully?

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

Re: How to put path with blanks into "for" command? Masking & quotes

#2 Post by aGerman » 13 Jun 2022 05:10

Actually USEBACKQ makes only sense if you want to read a file. The way you used it for processing the output of an application is just wrong.

Code: Select all

set "pathtoprog=D:\some\path\with blanks inside"
for /F "Tokens=*" %%G in ('"%pathtoprog%\myprog.exe" -p "......" %1') do set "....."
Steffen

tobwz
Posts: 30
Joined: 25 Feb 2022 03:21

Re: How to put path with blanks into "for" command? Masking & quotes

#3 Post by tobwz » 13 Jun 2022 07:16

Sorry, same problem with your suggestion

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

Re: How to put path with blanks into "for" command? Masking & quotes

#4 Post by aGerman » 13 Jun 2022 08:34

Are you absolutely sure that you don't have a typo in the path?

Steffen

tobwz
Posts: 30
Joined: 25 Feb 2022 03:21

Re: How to put path with blanks into "for" command? Masking & quotes

#5 Post by tobwz » 13 Jun 2022 10:03

Yes, 1000%.
I copied again the whole path from File Explorer into batch script
...same error.

And I verified it in another way:

if I eliminate the blank in path in File Explorer folder name and in script variable assignment
.... it works

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: How to put path with blanks into "for" command? Masking & quotes

#6 Post by ShadowThief » 13 Jun 2022 12:01

I feel like I've seen this before and I had to

Code: Select all

for /F "Tokens=*" %%G in ('call "D:\some\path\with blanks inside\myprog.exe" -p "......" %1') do set "....."
in order to actually get it working but I can't find where I used it

tobwz
Posts: 30
Joined: 25 Feb 2022 03:21

Re: How to put path with blanks into "for" command? Masking & quotes

#7 Post by tobwz » 16 Jun 2022 15:23

works
:-)
thank you

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: How to put path with blanks into "for" command? Masking & quotes

#8 Post by Squashman » 28 Feb 2024 13:21

An extra set of escaped double quotes would have done the trick.

Code: Select all

for /F "Tokens=*" %%G in (' ^""D:\some\path\with blanks inside\myprog.exe" -p "......" %1^"') do set "....."

mataha
Posts: 35
Joined: 27 Apr 2023 12:34

Re: How to put path with blanks into "for" command? Masking & quotes

#9 Post by mataha » 29 Feb 2024 16:24

Escaping the quotes shouldn't be necessary:

Code: Select all

for /f delims^= %%g in ('""D:\some\path\with blanks inside\myprog.exe" -p "......" %1"') do set "....."

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: How to put path with blanks into "for" command? Masking & quotes

#10 Post by Squashman » 29 Feb 2024 16:50

mataha wrote:
29 Feb 2024 16:24
Escaping the quotes shouldn't be necessary:

Code: Select all

for /f delims^= %%g in ('""D:\some\path\with blanks inside\myprog.exe" -p "......" %1"') do set "....."
We have a discussion about it here on DosTips. I will see if I can dig up that thread.

mataha
Posts: 35
Joined: 27 Apr 2023 12:34

Re: How to put path with blanks into "for" command? Masking & quotes

#11 Post by mataha » 06 Aug 2024 15:54

Squashman wrote:
29 Feb 2024 16:50
mataha wrote:
29 Feb 2024 16:24
Escaping the quotes shouldn't be necessary:

Code: Select all

for /f delims^= %%g in ('""D:\some\path\with blanks inside\myprog.exe" -p "......" %1"') do set "....."
We have a discussion about it here on DosTips. I will see if I can dig up that thread.
I've answered myself - those escaped quotes lose their semantics, but preserve their syntactic meaning; that is, the path within the command will be quoted properly, preventing metacharacters from wreaking havoc.

So the following won't work.

Code: Select all

for /f delims^= %%g in ('""C:\Program Files\Hammer & Chisel\something.exe" -p "ABC XYZ""') do (echo(%%~g)
But this will:

Code: Select all

for /f delims^= %%g in ('^""C:\Program Files\Hammer & Chisel\something.exe" -p "ABC XYZ"^"') do (echo(%%~g)

Post Reply