user1742664
user1742664

Reputation: 21

Implement multiple For loop in batch

I am trying to implement multiple for loop in batch like:

for x=1:10 
for y=x+1:10
//my code
end
end

My code is:

@echo off
for /l %%x in (1,1,10) do (
 for /l %%y in (%%y+1,1,10) do (
  //my code
 )
)

However, it doesn't work. Can anyone help me? Thanks.

Upvotes: 1

Views: 143

Answers (3)

npocmaka
npocmaka

Reputation: 57252

@echo off
setlocal enableDelayedExpansion
for /l %%x in (1;,;1step;,;10#=101) do (
 set /a inner=%%x+1

 @@echo ###%%x is from outer loop###

 for /l %%y in (,,,!inner!==1@==10times,9) do ( 

  @@echo -----%%y is from inner loop

 )
)
endlocal

Simplified

@echo off
setlocal enableDelayedExpansion
for /l %%x in (1;1;10) do (
 set /a inner=%%x+1

 echo - %%x is produced from outer loop

 for /l %%y in (!inner!;1;10) do ( 

 echo --- %%y is produced from inner loop

 )
)

Upvotes: 0

foxidrive
foxidrive

Reputation: 41234

I'm reposting a solution but in a simpler script.

@echo off
setlocal enableDelayedExpansion
for /l %%x in (1,1,10) do (
 set /a inner=%%x+1
    for /l %%y in (!inner!,1,10) do ( 
     echo %%x, %%y
    )
)
endlocal
pause

Upvotes: 2

SachaDee
SachaDee

Reputation: 9545

@echo off
for /l %%x in (1,1,10) do (
 echo #LOOP [1] ITERATION [%%x]
 for /l %%y in (1,1,10) do (
 echo  ##LOOP [2] ITERATION [%%y]
 )
echo.
)

Upvotes: 0

Related Questions