【发布时间】:2013-12-12 22:38:01
【问题描述】:
如何在每次运行时在 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
编辑:
我看不到如何清楚地获得变量和调用之间的对应关系。
这有点困难,而不是“回声”,同样在每个运行/循环中都有一个调用:
"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
【问题讨论】:
-
请在您的代码中显示
array。 -
你需要说出你到底想做什么。修复没有做你没有指定的事情的代码是一个猜谜游戏。
-
@Endoro 准确地说有 9 个 :)
-
@Magoo 我需要多次发送带有 4 个不同变量的命令,并且每次都为每个变量发送不同的值。
-
请注意,
run 9 times with 4 variables与run 4 times with 9 variables完全不同:“有 9 个数组...或 4 个,具体取决于排列 9x4 或 4x9 变量的方式。而我更愿意去 9 次 4 vars" (?)
标签: batch-file for-loop batch-processing var