我有一个无限的参数列表a b c d e f g h .... 我想在批处理文件中运行命令,如下所示:
mybatchfile a b c d e f g h I j k l m n ...
我有一个程序,我想从d到最后一个使用params。我可以这样做吗? 我知道%*将采用所有参数。我可以通过这种方式排除一些参数吗?
答案 0 :(得分:3)
你的批次:
@echo off
rem remove 3 first args
SHIFT
SHIFT
SHIFT
:start
if "%1"=="" (goto :exit)
:: Do whatever with token %1
Echo [%1]
:: Shift %2 into %1
SHIFT
goto :start
:exit
::pause
将输出:
C:\temp>shift.bat a b c d e f g h i j k l m n
[d]
[e]
[f]
[g]
[h]
[i]
[j]
[k]
[l]
[m]
[n]
答案 1 :(得分:1)
@echo off
setlocal
:: Preserve first 3 arguments so they may be used elsewhere in the script, if needed.
set arg1=%1
set arg2=%2
set arg3=%3
:: Now build the argument list for the remaining arguments
shift /1
shift /1
shift /1
set "args="
:getArgs
if "%~1" neq "" (
set args=%args% %1
shift
goto :getArgs
)
:: Call your program
yourProgram %args%
:: Carry on with the rest of your script, as needed
如果您的程序是另一个批处理脚本,请不要忘记使用call yourProgram %args%
,否则它将不会返回。
答案 2 :(得分:0)
@echo off
for /f "tokens=1-3,* delims= " %%a in ("%*") do set "params=%%d"
echo here are the parameters 4...n:
echo %params%
start myprogram %params%