batch-file

Batch File: FOR /F doesn't work if path has spaces


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?


Solution

  • 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