嵌套for循环批处理 [英] Nested For Loop in Batch
问题描述
伊夫过目许多线程,似乎无法的解决方案放在一起我的问题。
Ive look over many threads and cannot seem to put together a solution to my problem.
我想什么做的是使用两个列表创建一个输出。
What i would like to do is use two lists to create one output.
set Client_ID=BJCH,BORG,FGMS,SVIN,JEFF,NWIL
set PartNo=1,2,9,10,12,20
for %%A in (%Client_ID%) do (
for %%B in (%PartNo%) do (
echo %%A %%B
)
)
但输出我得到的是:
But the output I get is:
BJCH 1
BJCH 2
BJCH 9
BJCH 10
BJCH 12
BJCH 20
BORG 1
BORG 2
BORG 9
BORG 10
BORG 12
BORG 20
FGMS 1
FGMS 2
FGMS 9
etc........
我需要的是
BJCH 1
BORG 2
FGMS 9
SVIN 10
JEFF 12
NWIL 20
任何想法,我做错了什么?任何帮助深表AP preciated。
Any idea what I'm doing wrong? Any help is much appreciated.
推荐答案
您两份名单是的分离的人的:如果你窝有一个为
进入另外一个,你的乘法的结果的数量。有没有办法处理的两个列表的在同一个为
,除非你转换成列出两个<一个href=\"http://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990\">arrays然后处理在同一个两个数组为
,也就是过程中的两个数组有相同索引的元素。例如:
Your two lists are separated ones: if you nest one for
into the other one, you are multiplying the number of results. There is no way to process both lists in the same for
, unless you convert the lists into two arrays and then process both arrays in the same for
, that is, process the elements of the two arrays with same index. For example:
setlocal EnableDelayedExpansion
set Client_ID[1]=BJCH
set Client_ID[2]=BORG
etc...
set PartNo[1]=1
set PartNo[2]=2
etc...
for /L %%i in (1,1,6) do echo !Client_ID[%%i]! !PartNo[%%i]!
您还可以模拟previous处理(与指数相同的两个元素)是这样的:
You may also simulate previous processing ("two elements with same index") this way:
@echo off
setlocal EnableDelayedExpansion
set Client_ID=BJCH,BORG,FGMS,SVIN,JEFF,NWIL
set PartNo=1,2,9,10,12,20
set i=0
for %%A in (%Client_ID%) do (
set /A i+=1, j=0
for %%B in (%PartNo%) do (
set /A j+=1
if !i! equ !j! echo %%A %%B
)
)
修改:补充输出实例的
BJCH 1
BORG 2
FGMS 9
SVIN 10
JEFF 12
NWIL 20
这篇关于嵌套for循环批处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!