这是我遇到的问题:
@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
我想将此批处理文件存储在任何我想要的位置,而不是强制将其存储在与command.exe相同的文件夹中。有什么建议吗?
答案 0 :(得分:27)
在程序名称前添加CALL
:
FOR /F "usebackq" %%A IN (`CALL "C:\Folder With Spaces\command.exe" "C:\File Being Passed as a Parameter.txt"`) DO ECHO %%A
答案 1 :(得分:13)
Andriy M的call
技巧很聪明且工作正常,但我试图在这里理解这个问题。
此问题是由cmd.exe
引起的,您可以在cmd /help
....
当有时,第一个和最后一个引用将被删除 不完全是两行中的引号。
...
因此,还有另一种解决方案,只需添加两个额外的引号
FOR /F "usebackq=" %%A IN (`""C:\Folder Space\myCmd.exe" "Param space""`) DO (
ECHO %%A
)
答案 2 :(得分:2)
<强>小心:强>
使用&#39;来电&#39; (如Andriy M所示)似乎是最安全的选择。
我发现添加前导和尾随双引号的情况(如jeb的可能解决方案所示)存在问题。
<强>问题:强>
for /f "delims=" %%i in ('""C:\path with spaces\hello.bat" "filename with an & ampersand.jpg""') do ( echo output=%%i )
cmd.exe的输出:& was unexpected at this time.
<强>解决方案:强>
for /f "delims=" %%i in ('call "C:\path with spaces\hello.bat" "filename with an & ampersand.jpg"') do ( echo output=%%i )