在windows中,我想循环遍历一组环境变量,例如这个伪代码:
set MYVAR1=test
set MYVAR2=4711
set MYVAR3="a b c"
for /l %%x in (1, 1, 3) do (
echo %MYVAR%s%%
)
我期望以下输出
test
4711
a b c
如何更改此示例代码以使其正常工作?
答案 0 :(得分:4)
@echo off
set MYVAR1=test
set MYVAR2=4711
set MYVAR3="a b c"
setlocal enableDelayedExpansion
for /l %%x in (1, 1, 3) do (
echo !MYVAR%%x!
)
endlocal
答案 1 :(得分:2)
还有一种方法,使用变量前缀
解析set
命令的输出
@echo off
setlocal enableextensions disabledelayedexpansion
set MYVAR1=test
set MYVAR2=4711
set MYVAR3="a b c"
for /f "tokens=1,* delims==" %%a in ('set MYVAR') do echo %%b
答案 2 :(得分:1)
另一种方法:
@echo off
set MYVAR1=test
set MYVAR2=4711
set MYVAR3="a b c"
for /l %%x in (1, 1, 3) do (
call echo %%MYVAR%%x%%
)
pause