Batch file 使用多个变量(数组)进行for循环以更改每次运行

Batch file 使用多个变量(数组)进行for循环以更改每次运行,batch-file,for-loop,batch-processing,var,Batch File,For Loop,Batch Processing,Var,如何在每次运行时在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,

如何在每次运行时在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个不同的值

或者以更好的方式订购VAR:

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 
编辑:

我看不出如何在VAR和呼叫之间获得清晰的通信

这有点困难,每个运行/循环中都有一个调用,而不是“echo”:

"command /S %VAR1% -subswitch %VAR2% /H %VAR3% -o %VAR4%"
REM Devicegroup A
run1
run2
run3
REM Devicegroup B
run4
run5
REM
说明:

通过choosen解决方案,可以使用REM线来保持跑步概览功能:

"command /S %VAR1% -subswitch %VAR2% /H %VAR3% -o %VAR4%"
REM Devicegroup A
run1
run2
run3
REM Devicegroup B
run4
run5
REM
即使您忘记设置一个由代码处理的变量,唯一的问题是如果您第二次使用“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
%%A
%%D
可以对应于您的
%var1%
%var4%

如果你需要解释什么,请告诉我

@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
%%A
%%D
可以对应于您的
%var1%
%var4%

如果你需要解释什么,请告诉我

@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
应该产生您要求的输出

@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!
应该生成您要求的输出。

另一个

@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!
@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
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
编辑:添加新方法作为对评论的响应

以前的批处理文件是解决此问题的最简单方法。但是,如果您关心的是“为其他同事保留可读的数组定义”,那么下面的方法可能更清晰。该计划还解决了您对“包括测试,谁检查您是否真的有4个变量”的担忧:

输出:

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
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
再来一个

@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
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
编辑:添加新方法作为对评论的响应

以前的批处理文件是解决此问题的最简单方法。但是,如果您关心的是“为其他同事保留可读的数组定义”,那么下面的方法可能更清晰。该计划还解决了您对“包括测试,谁检查您是否真的有4个变量”的担忧:

输出:

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
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


请在您的代码中显示
数组
。您需要明确说明您想要做什么。修复不执行您未指定的操作的代码是一个猜测游戏。@endro准确地说,其中有9个:)@Magoo我需要多次使用4个不同的变量发送命令,每次使用不同的变量值。请注意,
使用4个变量运行9次
使用9个变量运行4次完全不同变量
:“有9个数组…或4个数组,这取决于9x4或4x9变量的排列方式。虽然我更喜欢9乘以4个变量”(?),但请在代码中显示
数组
。您需要明确说明您想要做什么。修复不执行您未指定的操作的代码是一个猜测游戏。@endro准确地说,其中有9个:)@Magoo我需要多次使用4个不同的变量发送命令,每次使用不同的变量值。请注意,
使用4个变量运行9次
使用9个变量运行4次完全不同变量
:“有9个数组…或4个数组,这取决于9x4或4x9变量的排列方式。虽然我更喜欢9乘以4个变量”(?)哈哈,但答案基本相同,只是你没有将四个值提取到单独的变量中。你为什么要把
“run1:”
“run2:”等包含在内?@yes-看到你的帖子并检查了相似性。仍然不清楚数据格式有多灵活,它是否来自文件,是否需要设置4个变量,或者是否将使用逗号分隔的字符串作为参数调用子例程。决定精确地制作出所要求的内容——但将相同的技术写在我的剪贴板上,所以——发布并被诅咒,就像他们说的那样……:)很公平。虽然还不清楚,但我认为他可能是想把它们放在单独的变量中,因为他问题中的一行-
echo%var1%%var2%%var3%%var4%
。我说得对吗,在Unclesmeat的例子中,变量只能按separator“,”排序,而在你的第一行中,然后是“:”@peet给出了你的最新编辑,使用Unclesmeat的版本,将
echo%%A..
替换为
command/s%%A-subswitch%%B/H%%C-o%%D
haha,除了没有将这四个值提取到单独的变量之外,答案几乎相同。你为什么要把
“run1:”
“run2:”等包含在内?@yes-看到你的帖子并检查了相似性。仍然不清楚数据格式有多灵活,它是否来自文件,是否需要设置4个变量,或者是否将使用逗号分隔的字符串作为参数调用子例程。决定精确地制作出所要求的内容——但将相同的技术写在我的剪贴板上,所以——发布并被诅咒,就像他们说的那样……:)很公平。虽然还不清楚,但我认为他可能是想把它们放在单独的变量中,因为他问题中的一行-
echo%var1%%var2%%var3%%var4%
。我说得对吗,在Unclesmeat的例子中,变量只能按separator“,”排序,而在你的第一行中,然后是“:”@peet给出了你的最新编辑,使用Unclesmeat的版本,将
echo%%A..
替换为
command/s%%A-subswitch%%B/H%%C-o%%D
command/s%%A-subswitch%%B/H%%C-o%%D
这在您的循环中也能起作用吗?肯定能起作用。试试看,让我知道。在
echo
之后添加它,或者用您的命令替换echo。虽然
'echo%%~i'
可以有效地替换为
“%%~i”
没错,但这样会更干净一些。干杯。@peet,如果这对你有效,请将其标记为正确答案。如果没有,请告诉我。
command/S%%A-subswitch%%B/H%%C-o%%D