我正在尝试将批处理文件参数提取为 3 个字符串。我首先尝试使用一个变量,然后将其拆分为 3(分隔符是一个空格):
代码:
@echo off
set var=\s v4.12 1,2,3,4
for /f "tokens=1,2,3 delims= " %%a in ("%var%") do (
echo a is %%a, b is %%b, c is %%c
)
结果:
a is \s, b is v4.12, c is 1,2,3,4
但是当我尝试将 var 实现为参数时:
新代码:
@echo off
for /f "tokens=1,2,3 delims= " %%a in ("%1") do (
echo a is %%a, b is %%b, c is %%c
)
执行:
test.bat \s v4.12 1,2,3,4
结果:
a is /s, b is , c is
我尝试打开回声,发现参数没有在循环中传递,我该如何解决?
========== 编辑(这是对 Aacini 的回答):
我当前的代码:
for /f "tokens=1-3" %%a in ("%*") do (
echo First is %%a, Second is %%b, Third is %%c
:loopers
for /f "tokens=* delims=," %%d in ("%%c") do (
echo %%d
shift
if not "%%d"=="" (
goto :loopers else exit >nul
)
)
)
获得第三个标记(%%c 等于 1,2,3,4)后,我必须再次创建一个循环以使用逗号作为分隔符来获取每个数字,但是当我尝试时,它会无限显示 %c(不是值但实际的 %c 文本)
这是结果:
First is /s, Second is v1.4, Third is 1,2,3,4
1
%c
..infinite loop of %c..