How to put path with blanks into "for" command? Masking & quotes
Moderator: DosItHelp
How to put path with blanks into "for" command? Masking & quotes
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?
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
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.
Steffen
Code: Select all
set "pathtoprog=D:\some\path\with blanks inside"
for /F "Tokens=*" %%G in ('"%pathtoprog%\myprog.exe" -p "......" %1') do set "....."
Re: How to put path with blanks into "for" command? Masking & quotes
Sorry, same problem with your suggestion
Re: How to put path with blanks into "for" command? Masking & quotes
Are you absolutely sure that you don't have a typo in the path?
Steffen
Steffen
Re: How to put path with blanks into "for" command? Masking & quotes
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
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
-
- 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
I feel like I've seen this before and I had to
in order to actually get it working but I can't find where I used it
Code: Select all
for /F "Tokens=*" %%G in ('call "D:\some\path\with blanks inside\myprog.exe" -p "......" %1') do set "....."
Re: How to put path with blanks into "for" command? Masking & quotes
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
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
We have a discussion about it here on DosTips. I will see if I can dig up that thread.mataha wrote: ↑29 Feb 2024 16:24Escaping 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
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.Squashman wrote: ↑29 Feb 2024 16:50We have a discussion about it here on DosTips. I will see if I can dig up that thread.mataha wrote: ↑29 Feb 2024 16:24Escaping 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 "....."
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)
Code: Select all
for /f delims^= %%g in ('^""C:\Program Files\Hammer & Chisel\something.exe" -p "ABC XYZ"^"') do (echo(%%~g)