Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the rest of arguments in windows batch file?

I know I can get first argument with %0, second with %1, and so on.. And I can also get all arguments with %*.

Can I get all arguments from the second arguments? For example, if I run

foo.bat bar1 bar2 bar3 bar4

How can I get only bar2 bar3 bar4?

like image 607
Louis Rhys Avatar asked May 03 '13 07:05

Louis Rhys


2 Answers

@ECHO OFF
SETLOCAL
SET allargs=%*
IF NOT DEFINED allargs echo no args provided&GOTO :EOF 
SET arg1=%1
CALL SET someargs=%%allargs:*%1=%%
ECHO allargs  %allargs%
ECHO arg1     %arg1%
ECHO someargs %someargs%

This will leave SOMEARGS with at least one leading separator (if it is set)

like image 136
Magoo Avatar answered Oct 16 '22 04:10

Magoo


With SHIFT command. But with every shift you'll lose the first. This will not change the %* but you'll be able to get all argument ,but the first:

@echo off
shift

set "arg_line= "
:parse_args
if "%~1" NEQ "" (
 arg_line=%argline% "%~1"
 goto :parse_args
)

now you'll have all arguments but the first stored in %arg_line%

like image 28
npocmaka Avatar answered Oct 16 '22 06:10

npocmaka