1

I have a loop that enumerates all arguments.

From this loop I want to loop thought all up until the current argument.

@ECHO OFF
setlocal enabledelayedexpansion

SET /a Counter=0
FOR %%a IN (%*) DO (
    SET /a Counter=!Counter!+1
    FOR /L %%A IN (1, 1, !Counter!) DO (
        ECHO %%%%A
    )
)

I have tried %%!A! and various combinations of % and !. I have also tried to first add a variable to a inner loop and tried using that.

The closest I get is to get literal values for example %1 %2 %3

Sample execution for arguments TEST1 TEST2 TEST3 should be

TEST1 (from outer loop)

    TEST1 (from inner loop)

TEST2 (from outer loop)

    TEST1 (from inner loop)

    TEST2 (from inner loop)

TEST3 (from outer loop)

    TEST1 (from inner loop)

    TEST2 (from inner loop)

    TEST3 (from inner loop)

I see multiple approaches when using a single loop but not any with nested loops

4
  • you can use shift to contextually change the value of %1 to another arguments value incrementally. ss64.com/nt/shift.html see this SO thread for an example: stackoverflow.com/questions/935609/… Commented Sep 2, 2015 at 12:02
  • Shift will affect the outer loop Commented Sep 2, 2015 at 12:14
  • shift does not affect %*, so your outter loop should not be affected by the operation, as I'm reading your script. Per the ss64.com link above "SHIFT will not affect the value of %* which holds all the original arguments %1 %2 %3..." Commented Sep 2, 2015 at 12:22
  • shift moves only forward Commented Sep 2, 2015 at 12:43

2 Answers 2

1

Sample execution for arguments TEST1 TEST2 TEST3 should be

TEST1 (from outer loop)
    TEST1 (from inner loop) 
TEST2 (from outer loop)
    TEST1 (from inner loop)
    TEST2 (from inner loop) 
TEST3 (from outer loop)
    TEST1 (from inner loop)
    TEST2 (from inner loop)
    TEST3 (from inner loop)

Use the following batch file (test.cmd):

@echo off
setlocal
setlocal EnableDelayedExpansion
set /a _count=0
for %%a in (%*) do (
  echo %%a ^(from outer loop^)
  set /a _count=!_count!+1
  set /a _stop=0
  call :InnerLoop %*
  )
goto :eof

:InnerLoop
  for %%b IN (%*) do (
    set /a _stop=!_stop!+1
    echo     %%b ^(from inner loop^)
    if [!_stop!]==[!_count!] goto :next
    )

:next
goto :eof
endlocal

Example output:

F:\test>test TEST1 TEST2 TEST3
TEST1 (from outer loop)
    TEST1 (from inner loop)
TEST2 (from outer loop)
    TEST1 (from inner loop)
    TEST2 (from inner loop)
TEST3 (from outer loop)
    TEST1 (from inner loop)
    TEST2 (from inner loop)
    TEST3 (from inner loop)

Further Reading

0
1

Since I'd already started playing, another option. I simplified the formatting, you can adjust that. Instead of trying to use %1,2,3... directly, copies them to X1,2,3...

@echo off
setlocal
setlocal enabledelayedexpansion
set N=0
for %%a in (%*) do ( echo %%a outer 
  set /A N=N+1 & set X!N!=%%a
  for /L %%I in (1,1,!N!) do echo   !X%%I! inner
)
endlocal

run with args: alpha bravo charlie 
alpha outer
  alpha inner
bravo outer
  alpha inner
  bravo inner
charlie outer
  alpha inner
  bravo inner
  charlie inner
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .