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.
我可以找到很多例子,但似乎都没有处理这个确切的情况。
2 个解决方案
#1
Here is a demo example using shift command as suggested by Scott C:
以下是Scott C建议的使用shift命令的演示示例:
@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
.
否则,可以使用“%~1”或只需%1或任何需要的任何操作来完成当前参数的任何操作,请参阅执行调用/命令提示符窗口中的帮助输出。或者帮忙打电话。
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.
命令移位将所有参数向左移动1,从而可以再次使用%1访问下一个参数。可以通过shift /?读取命令转换的帮助。在命令提示符窗口中执行。
The two examples above fail to process all parameters correct if the batch file is called with 1 or more empty parameters.
如果使用1个或多个空参数调用批处理文件,则上述两个示例无法正确处理所有参数。
For example calling minimum example with Arg1 "argument 2" "" "arg 4"
results in the output:
例如,使用Arg1“参数2”“”“arg 4”调用最小示例会导致输出:
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:
使用Arg1“参数2”“”“arg 4”调用的第三个批处理文件输出正确:
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:
使用Arg1“参数2”“”“arg 4”调用的最后一个批处理文件输出:
Processing parameter: "Arg1"
Processing parameter: "argument 2"
Skipping empty parameter.
Processing parameter: "arg 4"
But batch files are usually not called with empty parameters.
但批处理文件通常不会使用空参数调用。
#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:
如果参数可能包含通配符(*或?),则必须通过经典的shift循环将参数加载到数组中:
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.
有关批处理文件中阵列管理的进一步说明,请参阅此文章。
#1
Here is a demo example using shift command as suggested by Scott C:
以下是Scott C建议的使用shift命令的演示示例:
@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
.
否则,可以使用“%~1”或只需%1或任何需要的任何操作来完成当前参数的任何操作,请参阅执行调用/命令提示符窗口中的帮助输出。或者帮忙打电话。
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.
命令移位将所有参数向左移动1,从而可以再次使用%1访问下一个参数。可以通过shift /?读取命令转换的帮助。在命令提示符窗口中执行。
The two examples above fail to process all parameters correct if the batch file is called with 1 or more empty parameters.
如果使用1个或多个空参数调用批处理文件,则上述两个示例无法正确处理所有参数。
For example calling minimum example with Arg1 "argument 2" "" "arg 4"
results in the output:
例如,使用Arg1“参数2”“”“arg 4”调用最小示例会导致输出:
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:
使用Arg1“参数2”“”“arg 4”调用的第三个批处理文件输出正确:
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:
使用Arg1“参数2”“”“arg 4”调用的最后一个批处理文件输出:
Processing parameter: "Arg1"
Processing parameter: "argument 2"
Skipping empty parameter.
Processing parameter: "arg 4"
But batch files are usually not called with empty parameters.
但批处理文件通常不会使用空参数调用。
#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:
如果参数可能包含通配符(*或?),则必须通过经典的shift循环将参数加载到数组中:
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.
有关批处理文件中阵列管理的进一步说明,请参阅此文章。