如何处理for循环中的路径名中的空格?
问题描述:
尝试使用当前脚本的路径,并且路径中包含空格。我似乎无法得到它的工作虽然:如何处理for循环中的路径名中的空格?
C:\Test Directory>dir
Volume in drive C has no label.
Volume Serial Number is 7486-CEE6
Directory of C:\Test Directory
08/31/2010 07:28 PM <DIR> .
08/31/2010 07:28 PM <DIR> ..
08/31/2010 07:28 PM 20 echoit.cmd
08/31/2010 07:28 PM 94 test.cmd
2 File(s) 114 bytes
2 Dir(s) 344,141,197,312 bytes free
C:\Test Directory>type echoit.cmd
@echo off
echo %*
C:\Test Directory>type test.cmd
@echo off
for /f "tokens=*" %%a in ('%~dp0\echoit.cmd Hello World') do (
echo %%a
)
C:\Test Directory>test
'C:\Test' is not recognized as an internal or external command,
operable program or batch file.
C:\Test Directory>
答
更改test.cmd
以下几点:
@echo off
for /f "tokens=*" %%a in ('"%~dp0\echoit.cmd" Hello World') do (
echo %%a
)
您需要设置完整的命令,减去参数,在引号。当整个集合被引用时,Windows命令提示符将单词集合视为单个命令,这就是为什么您必须排除Hello World
参数。如果你想把它包含在引号中,Windows会将它作为命令的一部分而不是作为参数。
答
您是否尝试过添加引号?
for /f "tokens=*" %%a in ('"%~dp0\echoit.cmd" Hello World') do (
echo %%a
)
答
如何使用〜FS0,即
C:\Test Directory>type test.cmd
@echo off
for /f "tokens=*" %%a in ('%~fs0\echoit.cmd Hello World') do (
echo %%a
)
其中%〜FSI - 我扩展%的全路径名与短名
很大,现在把它为“工作c:\ program files(x86)\'而不是'c:\ Test Directory' – Spongman 2012-08-27 21:45:37
它应该以'C:\ Program Files(x86)'的方式工作。你在看什么? – linuxuser27 2012-08-30 16:22:01
我没有添加令牌= *,并在使用时加入了引号,它修复了我的间距问题。 – DoodleKana 2014-08-26 18:01:11