通过包含数字的变量获取批量参数

通过包含数字的变量获取批量参数

我有一个枚举所有参数的循环。

从这个循环开始我想循环思考直到当前论点。

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

我尝试过 %%!A! 以及 % 和 ! 的各种组合。我还尝试过先将变量添加到内部循环,然后尝试使用该变量。

我最接近的是获取文字值,例如 %1 %2 %3

参数 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)

我看到使用单个循环时有多种方法,但没有看到使用嵌套循环的方法

答案1

参数 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)

使用以下批处理文件(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

示例输出:

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)

进一步阅读

答案2

因为我已经开始玩了,所以还有另一种选择。我简化了格式,你可以调整它。不要直接尝试使用 %1,2,3...,而是将它们复制到 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

相关内容