在批处理文件中,如何在其中包含空格的字符串上循环?
例如,我有:
for %%P in (Test 1,Test 2,Test 3) do (
echo %%P
)
我得到的输出是
Test
1
Test
2
Test
3
而不是我希望的输出:
Test 1
Test 2
Test 3
如果我添加引号,我会
"Test 1"
"Test 2"
"Test 3"
我也不想要。有什么想法吗?
答案 0 :(得分:4)
for /?
有你的答案。
%~I - expands %I removing any surrounding quotes (")
所以你要像这样实现它:
FOR %%P IN ("Test 1","Test 2","Test 3") DO (echo %%~P)
答案 1 :(得分:1)
假设只有3个字符串需要拆分;
for /f "tokens=1,2,3 delims=," %%G in ("Test 1,Test 2,Test 3") do (
echo %%~G
echo %%~H
echo %%~I
)
或者我最喜欢的;
set "string=Test 1,Test 2,Test 3"
set "chVar=%string%"
:reIter
for /f "tokens=1* delims=," %%G in ("%chVar%") do (echo %%G & set "chVar=%%H")
if defined chVar (goto :reIter)
奇怪但可选;
set "string=Test 1,Test 2,Test 3"
set string=%string: =_%
for %%G in (%string%) do call :replace "%%~G"
pause
exit
:replace
set "chVar=%~1"
echo %chVar:_= %
goto :eof