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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With