Page 1 of 1
How to put path with blanks into "for" command? Masking & quotes
Posted: 13 Jun 2022 01:39
by tobwz
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?
Re: How to put path with blanks into "for" command? Masking & quotes
Posted: 13 Jun 2022 05:10
by aGerman
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
Re: How to put path with blanks into "for" command? Masking & quotes
Posted: 13 Jun 2022 07:16
by tobwz
Sorry, same problem with your suggestion
Re: How to put path with blanks into "for" command? Masking & quotes
Posted: 13 Jun 2022 08:34
by aGerman
Are you absolutely sure that you don't have a typo in the path?
Steffen
Re: How to put path with blanks into "for" command? Masking & quotes
Posted: 13 Jun 2022 10:03
by tobwz
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
Re: How to put path with blanks into "for" command? Masking & quotes
Posted: 13 Jun 2022 12:01
by ShadowThief
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
Re: How to put path with blanks into "for" command? Masking & quotes
Posted: 16 Jun 2022 15:23
by tobwz
works
thank you
Re: How to put path with blanks into "for" command? Masking & quotes
Posted: 28 Feb 2024 13:21
by Squashman
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 "....."
Re: How to put path with blanks into "for" command? Masking & quotes
Posted: 29 Feb 2024 16:24
by mataha
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 "....."
Re: How to put path with blanks into "for" command? Masking & quotes
Posted: 29 Feb 2024 16:50
by Squashman
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.
Re: How to put path with blanks into "for" command? Masking & quotes
Posted: 06 Aug 2024 15:54
by mataha
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)