views:

552

answers:

2

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?

+3  A: 

%* 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.

Joey
Wow, slick approach. Thanks!
Dave
I've shortened it a bit; there was some redundancy :)
Joey
A: 

This approach fails if you have assignments in the parameters, e.g. "a=b". Then you'll get two separate parameters "a" and "b", losing the =.

Tbee