tags:

views:

1544

answers:

2

Further to How to Pass Command Line Parameters in batch file how does one get the rest of the parameters with specifying them exactly? I don't want to use SHIFT because I don't know how many parameters there might be and would like to avoid counting them, if I can.

For example, given this batch file:

@echo off
set par1=%1
set par2=%2
set par3=%3
set therest=%???
echo the script is %0
echo Parameter 1 is %par1%
echo Parameter 2 is %par2%
echo Parameter 3 is %par3%
echo and the rest are %therest%

Running mybatch opt1 opt2 opt3 opt4 opt5 ...opt20 would yield:

the script is mybatch
Parameter 1 is opt1
Parameter 2 is opt2
Parameter 3 is opt3
and the rest are opt4 opt5 ...opt20

I know %* gives all the parameters, but I don't wan't the first three (for example).

+2  A: 

But here you do know how many there will be. You know you'll have three. Shift three times, and the remainder of the parameters are all in %*. That is, when you use shift, you change the apparent value of %* to represent only the parameters you haven't yet shifted off.

Rob Kennedy
Code would make this answer much better
Sparr
There's already code in the question, and in the question this question is based on.
Rob Kennedy
This is wrong, shift doesn't affect the value of %*.
Dave
Gee, you're right, Dave. Thanks.
Rob Kennedy
+3  A: 

Here's how you can do it without using SHIFT:

@echo off

for /f "tokens=1-3*" %%a in ("%*") do (
    set par1=%%a
    set par2=%%b
    set par3=%%c
    set therest=%%d
)

echo the script is %0
echo Parameter 1 is %par1%
echo Parameter 2 is %par2%
echo Parameter 3 is %par3%
echo and the rest are %therest%
Patrick Cuff
This probably won't handle parameters enclosed in quotation marks correctly.
Joey
Johannes left a solid answer on this question: http://stackoverflow.com/questions/761615/is-there-a-way-to-indicate-the-last-n-parameters-in-a-batch-file
Dave
@Dave; what's not solid abut this answer?
Patrick Cuff