In windows batch file, if I have two sets, how do I loop them simultaneously(not nested loop)?
SET A=(1,2,3) SET B=(A,B,C)
loop (1,A), (2,B), (3,C)
pairs?
In windows batch file, if I have two sets, how do I loop them simultaneously(not nested loop)?
SET A=(1,2,3) SET B=(A,B,C)
loop (1,A), (2,B), (3,C)
pairs?
This method may be used with any number of simultaneous sets.
@echo off
setlocal EnableDelayedExpansion
set "A=1,2,3"
set "B=A,B,C"
rem Separate A set into individual array elements
set i=0
for %%a in (%A%) do (
set /A i+=1
set "A[!i!]=%%a"
)
rem Separate B set into individual array elements
set j=0
for %%b in (%B%) do (
set /A j+=1
set "B[!j!]=%%b"
)
if %i% neq %j% (
echo A and B have not the same number of elements
goto :EOF
)
for /L %%i in (1,1,%i%) do echo (!A[%%i]!,!B[%%i]!)
You may read further details on array management in Batch files at this post.
Another, perhaps simpler method:
@echo off
setlocal EnableDelayedExpansion
set "A=1,2,3"
set "B=A,B,C"
(for %%a in (%A%) do echo %%a) > A.txt
< A.txt (
for %%b in (%B%) do (
set /P "A="
echo [!A!,%%b]
)
)
@echo off
set "A=1,2,3"
set "B=A,B,C"
setlocal enableDelayedExpansion
set f1=0
set f2=0
for %%Z in (%A%) do (
set /a f1=f1+1
for %%Y in (%B%) do (
set /a f2=f2+1
if !f1! == !f2! (
echo [%%Z,%%Y]
)
)
set f2=0
)
Cannot be done without some nesting. I preferred rectangular brackets to avoid escaping with the normal ones.