15

This is the problem I'm having:

@ECHO OFF

REM If this batch file is run in the same directory as "command.exe" then the
REM following line will work.
FOR /F "usebackq" %%A IN (`command.exe "C:\File Being Passed as a Parameter.txt"`) DO ECHO %%A

REM The following line does not work no matter where this batch file is run.
FOR /F "usebackq" %%A IN (`"C:\Folder With Spaces\command.exe" "C:\File Being Passed as a Parameter.txt"`) DO ECHO %%A

I would like to store this batch file wherever I want and not be forced to store it in the same folder as command.exe. Any suggestions?

0

3 Answers 3

29

Add CALL before the program name:

FOR /F "usebackq" %%A IN (`CALL "C:\Folder With Spaces\command.exe" "C:\File Being Passed as a Parameter.txt"`) DO ECHO %%A
Sign up to request clarification or add additional context in comments.

3 Comments

Why to use call with an EXE?
@jeb: In this particular case, to work around the problem in question.
You are right, I tested it and it seems that only the way with a call works
13

The call trick of Andriy M is clever and works fine, but I tried to understand the problem here.

This problem is caused by the cmd.exe, as you can read at cmd /help

....
the first and the last quote will be removed, when there are not exactly two quotes in the line.
...

So there is also another solution with simply adding two extra quotes (I modified it to use the hint of dbenham)

FOR /F %%A IN ('^""C:\Folder Space\myCmd.exe" "Param space"^"') DO (
    ECHO %%A
)

This variant is more stable than the CALL solution, because a CALL has the nasty side effect of doubling all carets.

FOR /F %%A IN ('call "C:\Docs & a single ^ caret\myCmd.exe" "Param space"') DO (
    ECHO %%A
)

This tries to start myCmd from the directory "C:\Docs & a single ^^ caret" (with to carets)

3 Comments

That is exactly right! Just adding some examples here. This works: FOR /F "usebackq" %A IN (`cmd.exe /C "findstr.exe" /?`) DO @ECHO %A (quotes only around one argument). This works too: FOR /F "usebackq" %A IN (`"cmd.exe" /C findstr.exe /?`) DO @ECHO %A (quotes only around the command). This does not work until another pair of double quotes is added like you've suggested: FOR /F "usebackq" %A IN (`"cmd.exe" /C "findstr.exe" /?`) DO @ECHO %a.
USEBACKQ is not needed, and better to use '^""c:\folder space\myCmd.exe" "param with ^poison char"^"' so that poison characters are still quoted properly.
So it seems like it would always be safe to use surrounding quotes as a best practice. This also keep you from having to escape the pipe as well.
3

Careful:

Using 'call' (as shown by Andriy M) seems the safest option.

I found a case where adding leading and trailing double quotes (as suggested as a possible solution by jeb) has a problem.

Problem:

for /f "delims=" %%i in ('""C:\path with spaces\hello.bat" "filename with an & ampersand.jpg""') do ( echo output=%%i )

cmd.exe's output: & was unexpected at this time.

Solution:

for /f "delims=" %%i in ('call "C:\path with spaces\hello.bat" "filename with an & ampersand.jpg"') do ( echo output=%%i )

1 Comment

That's right, other characters like () can lead to problems as well. This can easily happen with paths like C:\Program Files (x86)\....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.