views:

319

answers:

3

Duplicate:

Clarification: I knew of the looping approach - this worked even before Command Extensions; I was hoping for something fun and undocumented like %~*1 or whatever - just like those documented at http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/percent.mspx?mfr=true.


In a Windows batch file (with the so called "Command Extensions" on), %1 is the first argument, %2 is the second, etc. %* is all arguments concatenated.

My question: is there a way to get everything AFTER %2, for example?

I couldn't find such a thing, and it would be helpful for something I'm working on.

+4  A: 

You can use SHIFT for this. It removes %1 and shifts all other arguments one lower. This script outputs all the arguments after %2 (so it outputs %3, %4...) until one of them is empty (so it's the last one):

@echo off

SHIFT
SHIFT

:loop
if "%1" == "" goto end
echo %1
SHIFT
goto loop

:end

EDIT: Removed example using %* as this doesn't work - %* always outputs all of the parameters

schnaader
Just like in sh? Great! +1 . Do you happen to know what's the powershell option?
OscarRyz
Sorry, haven't used powershell yet.
schnaader
+1  A: 

Building on schnaader's answer, I think this does it if you want everything after %1 concatenated.

@echo off

SHIFT

set after1=

:loop
if "%1" == "" goto end
set after1=%after1% %1
SHIFT
goto loop


:end

echo %after1%
Scott Langham
+2  A: 

I am not sure if there is a direct command but you can always use a simple loop and shift to get the result in a variable. Something like:

@echo off
set RESTVAR=
shift
:loop1
if "%1"=="" goto after_loop
set RESTVAR=%RESTVAR% %1
shift
goto loop1

:after_loop
echo %RESTVAR%

Let me know if it helps!

Samuel
ok didn't notice that other people have answered it already
Samuel
Yes, but your answer is the most complete and is what I did eventually.
noamtm