Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 687
Dave Avatar asked Apr 17 '09 18:04

Dave


2 Answers

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

like image 114
Joey Avatar answered Sep 22 '22 19:09

Joey


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.

like image 45
max_diff Avatar answered Sep 22 '22 19:09

max_diff