如何在每次运行的for循环中更改不同的变量?
假设:
for %%i in (
VAR:RUN1,RUN2,RUN3,RUN4,RUN5,RUN6,RUN7,RUN8,RUN9
var1:1,2,3,4,5,6,7,8,9
var2:10,20,30,40,50,60,70,80,90
var3:a,b,c,d,e,f,g,h,i
var4:x,y,x,y,x,y,x,y,x
) do (
echo %var1% %var2% %var3% %var4%
)
虽然有例如每个var的9个不同的值?
或以更好的方式订购vars:
for %%i in (
LoopRUN:VAR1,VAR2,VAR3,VAR4
run1:1,10,a,x
run2:2,20,b,y
run3:3,30,c,x
run4:4,40,d,y
run5:5,50,e,x
run6:6,60,f,y
run7:7,70,g,x
run8:8,80,h,y
run9:9,90,i,x
) do (
echo %var1% %var2% %var3% %var4%
)
这应该是这样的:
C:\>1,10,a,x
2,20,b,y
3,30,c,x
4,40,d,y
5,50,e,x
6,60,f,y
7,70,g,x
8,80,h,y
9,90,i,x
编辑:
我无法看到如何在变量和电话之间清楚地获得相应的信息。
这有点困难,而不是“echo”,每次运行/循环中都有一个调用:
"command /S %VAR1% -subswitch %VAR2% /H %VAR3% -o %VAR4%"
说明:
通过选择解决方案,可以使用REM线来保持运行概览:
REM Devicegroup A
run1
run2
run3
REM Devicegroup B
run4
run5
REM
即使忘记设置一个VAR,这是由代码处理的,唯一的问题是如果你第二次使用“run [x] ...”那么请密切关注你的运行编号!
虽然运行是按照自然顺序完成的,但不知道我们是否能抓住这个。见输出:
command /S 10 -subswitch 10 /H a -o x
command /S 11 -subswitch 10 /H a -o x
command /S 12 -subswitch 20 /H b -o y
command /S 13 -subswitch 30 /H c -o x
command /S 14 -subswitch 40 /H d -o y
command /S 15 -subswitch 50 /H e -o x
ERROR: Missing variable in run[16]
command /S 17 -subswitch 70 /H g -o x
command /S 18 -subswitch 80 /H h -o y
command /S 19 -subswitch 90 /H i -o x
command /S 1 -subswitch 10 /H a -o x
command /S 20 -subswitch 20 /H b -o y
command /S 2 -subswitch 20 /H b -o y
command /S 3 -subswitch 30 /H c -o x
command /S 4 -subswitch 40 /H d -o y
command /S 5 -subswitch 50 /H e -o x
ERROR: Missing variable in run[6]
command /S 7 -subswitch 70 /H g -o x
command /S 8 -subswitch 80 /H h -o y
command /S 9 -subswitch 90 /H i -o x
答案 0 :(得分:2)
@ECHO OFF
SETLOCAL
for %%r in ( "run1:1,10,a,x",
"run2:2,20,b,y",
"run3:3,30,c,x",
"run4:4,40,d,y",
"run5:5,50,e,x",
"run6:6,60,f,y",
"run7:7,70,g,x",
"run8:8,80,h,y",
"run9:9,90,i,x"
) DO (
for /f "tokens=2delims=:" %%i in ("%%~r") do (
echo %%i
)
)
GOTO :EOF
应该产生你要求的输出。
答案 1 :(得分:2)
另一个!
@echo off
setlocal EnableDelayedExpansion
set LF=^
rem ** Previous two empty lines required
set "vars=1,10,a,x;2,20,b,y;3,30,c,x;4,40,d,y;5,50,e,x;6,60,f,y;7,70,g,x;8,80,h,y;9,90,i,x"
for /F "tokens=1-4 delims=," %%A in ("%vars:;=!LF!%") do echo command /S %%A -subswitch %%B /H %%C -o %%D
输出:
command /S 1 -subswitch 10 /H a -o x
command /S 2 -subswitch 20 /H b -o y
command /S 3 -subswitch 30 /H c -o x
command /S 4 -subswitch 40 /H d -o y
command /S 5 -subswitch 50 /H e -o x
command /S 6 -subswitch 60 /H f -o y
command /S 7 -subswitch 70 /H g -o x
command /S 8 -subswitch 80 /H h -o y
command /S 9 -subswitch 90 /H i -o x
编辑:添加新方法作为对评论的回复
以前的批处理文件是解决此问题的最简单方法。但是,如果您关心的是“为其他同事保留可读的数组定义”,那么下面的方法可能会更清晰。该计划还解决了您对“包括测试,检查您是否确实有4个变种”的担忧:
@echo off
setlocal
rem Define the *array* of variables per run
set run[1]=1,10,a,x
set run[2]=2,20,b,y
set run[3]=3,30,c,x
set run[4]=4,40,d,y
set run[5]=5,50,e,x
set run[6]=6,60,f
set run[7]=7,70,g,x
set run[8]=8,80,h,y
set run[9]=9,90,i,x
for /F "tokens=1-5 delims==," %%A in ('set run[') do (
if "%%E" neq "" (
ECHO command /S %%B -subswitch %%C /H %%D -o %%E
) else (
echo ERROR: Missing variable in %%A
)
)
输出:
command /S 1 -subswitch 10 /H a -o x
command /S 2 -subswitch 20 /H b -o y
command /S 3 -subswitch 30 /H c -o x
command /S 4 -subswitch 40 /H d -o y
command /S 5 -subswitch 50 /H e -o x
ERROR: Missing variable in run[6]
command /S 7 -subswitch 70 /H g -o x
command /S 8 -subswitch 80 /H h -o y
command /S 9 -subswitch 90 /H i -o x
答案 2 :(得分:0)
@echo off
for %%i in (
"1,10,a,x"
"2,20,b,y"
"3,30,c,x"
"4,40,d,y"
"5,50,e,x"
"6,60,f,y"
"7,70,g,x"
"8,80,h,y"
"9,90,i,x"
) do for /f "delims=, tokens=1-4" %%A in ("%%~i") do (
echo %%A %%B %%C %%D
)
%%A
到%%D
可以与您的%var1%
到%var4%
对应。
如果您需要任何解释,请告诉我。
答案 3 :(得分:0)
@ECHO OFF &SETLOCAL
setlocal EnableDelayedExpansion
set $VAR=RUN1,RUN2,RUN3,RUN4,RUN5,RUN6,RUN7,RUN8,RUN9
set var1=1,2,3,4,5,6,7,8,9
set var2=10,20,30,40,50,60,70,80,90
set var3=a,b,c,d,e,f,g,h,i
set var4=x,y,x,y,x,y,x,y,x
call:Send 4
exit /b
:send
set $c=1
for %%a in (%$var:,= %) do (echo %%a
set $run=
call:final %1
set /a $c+=1)
exit /b
:final
for /l %%b in (1,1,%1) do (set $liste=!var%%b!
for /f "tokens=%$c% delims=," %%x in ('echo !$liste!') do set $run=!$run! %%x)
set $c1=1
set $comm=
for %%a in (!$run!) do (if !$c1! equ 1 set $Comm=Command /s %%a
if !$c1! equ 2 set $comm=!$comm! -subswitch %%a /H
if !$c1! equ 3 set $comm=!$comm! %%a -O
if !$c1! equ 4 set $comm=!$comm! %%a
set /a $c1+=1)
echo !$comm!