Is there a way to indicate the last n parameters in a batch file?

In the following example, I want to call a child batch file from a parent batch file and pass all of the remaining parameters to the child.

C:> parent.cmd child1 foo bar
C:> parent.cmd child2 baz zoop
C:> parent.cmd child3 a b c d e f g h i j k l m n o p q r s t u v w x y z

Inside parent.cmd, I need to strip %1 off the list of parameters and only pass the remaining parameters to the child script.

set CMD=%1
%CMD% <WHAT DO I PUT HERE>

I've investigated using SHIFT with %*, but that doesn't work. While SHIFT will move the positional parameters down by 1, %* still refers to the original parameters.

Anyone have any ideas? Should I just give up and install Linux?


%* will always expand to all original parameters, sadly. But you can use the following snippet of code to build a variable containing all but the first parameter:

rem throw the first parameter away
shift
set params=%1
:loop
shift
if [%1]==[] goto afterloop
set params=%params% %1
goto loop
:afterloop

I think it can be done shorter, though ... I don't write these sort of things very often :)

Should work, though.


Here's a one-line approach using the "for" command...

for /f "usebackq tokens=1*" %%i in (`echo %*`) DO @ set params=%%j

This command assigns the 1st parameter to "i" and the rest (denoted by '*') are assigned to "j", which is then used to set the "params" variable.


the line

%CMD% <WHAT DO I PUT HERE>

shall be changed to:

(
SETLOCAL ENABLEDELAYEDEXPANSION
SET Skip=1

FOR %%I IN (%*) DO IF !Skip! LEQ 0 ( 
        SET params=!params! %%I
    ) ELSE SET /A Skip-=1
)
(
ENDLOCAL
SET params=%params%
)
%CMD% %params%

of course, you may set Skip to any number of arguments.

链接地址: http://www.djcxy.com/p/30278.html

上一篇: 如何在Windows的命令行创建一个空文件?

下一篇: 有没有办法指出批处理文件中的最后n个参数?