找到你要的答案

Q:How do you iterate through a subset of the parameters passed to a batch file?

Q:你怎么遍历通过一个批处理文件的一个子集参数?

Inside a batch file, how do you iterate through a subset of the command line arguments passed to the batch file?

I can find lots of examples, but none seems to deal with this exact scenario.

在一个批处理文件,你怎么遍历命令行参数传递到批处理文件的子集?

我可以找到很多例子,但似乎没有一个处理这个确切的情况。

answer1: 回答1:

Here is a demo example using shift command as suggested by Scott C:

@echo off
set "Count=0"

:NextParameter
if "%~1" == "" goto Done
set /A Count+=1
echo Parameter %Count% is: "%~1"
shift
goto NextParameter

:Done
echo %Count% parameters processed.
set "Count="

The absolute minimum loop would be:

@echo off
:NextParameter
if "%~1" == "" goto :EOF
echo Processing parameter: "%~1"
shift
goto NextParameter

In the loop always first parameter is evaluated for being an empty string. The loop exits if this condition becomes true.

Otherwise it can be done whatever should be done with the current parameter using "%~1" or just %1 or whatever is needed, see help output in a command prompt window on executing there call /? or help call.

Command shift shifts all arguments to left by 1 making it possible to access the next parameter again with %1. Help of command shift can be read with shift /? executed in a command prompt window.

The two examples above fail to process all parameters correct if the batch file is called with 1 or more empty parameters.

For example calling minimum example with Arg1 "argument 2" "" "arg 4" results in the output:

Processing parameter: "Arg1"
Processing parameter: "argument 2"

This can be solved with following batch code:

@echo off
:NextParameter
if "%~1" == "" (
    if not [%1] == [""] goto :EOF
)
echo Processing parameter: "%~1"
shift
goto NextParameter

This third batch file called with Arg1 "argument 2" "" "arg 4" outputs correct:

Processing parameter: "Arg1"
Processing parameter: "argument 2"
Processing parameter: ""
Processing parameter: "arg 4"

It is of course also possible to skip empty parameter in list, for example with:

@echo off
:NextParameter
if "%~1" == "" (
    if [%1] == [""] (
        echo Skipping empty parameter.
        shift
        goto NextParameter
    )
    goto :EOF
)
echo Processing parameter: "%~1"
shift
goto NextParameter

This last batch file called with Arg1 "argument 2" "" "arg 4" outputs:

Processing parameter: "Arg1"
Processing parameter: "argument 2"
Skipping empty parameter.
Processing parameter: "arg 4"

But batch files are usually not called with empty parameters.

这里是一个演示例子,使用移位命令所建议的史葛C:

@echo off
set "Count=0"

:NextParameter
if "%~1" == "" goto Done
set /A Count+=1
echo Parameter %Count% is: "%~1"
shift
goto NextParameter

:Done
echo %Count% parameters processed.
set "Count="

绝对最小循环:

@echo off
:NextParameter
if "%~1" == "" goto :EOF
echo Processing parameter: "%~1"
shift
goto NextParameter

在循环中总是第一个参数被求值为空字符串。如果此条件为真,则循环退出。

否则,它可以做任何应该用当前参数使用“% 1”或只需% 1或任何需要,看到帮助输出在命令提示符窗口上执行调用/?或帮助呼叫。

命令移位将所有参数由1移到左,以便用%1 1再次访问下一个参数。指令移位的帮助可以用Shift /?在命令提示符窗口中执行。

以上两个例子未能处理所有参数,如果批处理文件被调用有1个或更多空参数。

例如调用最小例子arg1”的说法2”“”“精4”结果的输出:

Processing parameter: "Arg1"
Processing parameter: "argument 2"

这可以用以下批处理代码解决:

@echo off
:NextParameter
if "%~1" == "" (
    if not [%1] == [""] goto :EOF
)
echo Processing parameter: "%~1"
shift
goto NextParameter

这第三批文件称为“参数2”arg1”“精4”输出正确:

Processing parameter: "Arg1"
Processing parameter: "argument 2"
Processing parameter: ""
Processing parameter: "arg 4"

当然,也可以跳过列表中的空参数,例如:

@echo off
:NextParameter
if "%~1" == "" (
    if [%1] == [""] (
        echo Skipping empty parameter.
        shift
        goto NextParameter
    )
    goto :EOF
)
echo Processing parameter: "%~1"
shift
goto NextParameter

这最后一批文件称为“参数2”arg1”“精4”输出:

Processing parameter: "Arg1"
Processing parameter: "argument 2"
Skipping empty parameter.
Processing parameter: "arg 4"

但批处理文件通常没有空参数调用。

answer2: 回答2:

I think the simplest way is to first load the parameters in an array, and then process array elements in any way you wish:

@echo off
setlocal EnableDelayedExpansion

rem Load parameters in "parV" array and "parC" counter
set parC=0
for %%a in (%*) do (
   set /A parC+=1
   set parV[!parC!]=%%a
)

rem Show parameters from 3 to N
for /L %%i in (3,1,%parC%) do echo %%i- !parV[%%i]!

If the parameters may contain wild-card characters (* or ?), then the parameters must be loaded in the array via the classical shift loop:

rem Load parameters in parV/parC even if they have wild-cards
set parC=0
:nextPar
   if "%~1" equ "" goto endPars
   set /A parC+=1
   set "parV[!parC!]=%~1"
   shift
goto nextPar
:endPars

For a further description on array management in Batch files, see this post.

我认为最简单的方法是先加载数组中的参数,然后按照你希望的任何方式处理数组元素:

@echo off
setlocal EnableDelayedExpansion

rem Load parameters in "parV" array and "parC" counter
set parC=0
for %%a in (%*) do (
   set /A parC+=1
   set parV[!parC!]=%%a
)

rem Show parameters from 3 to N
for /L %%i in (3,1,%parC%) do echo %%i- !parV[%%i]!

如果参数可能包含通配符字符(*或?),参数必须通过传统的移位循环载入数组:

rem Load parameters in parV/parC even if they have wild-cards
set parC=0
:nextPar
   if "%~1" equ "" goto endPars
   set /A parC+=1
   set "parV[!parC!]=%~1"
   shift
goto nextPar
:endPars

有关批处理文件中数组管理的进一步说明,请参见此职位。

batch-file