How to get other arguments in a Windows batch file?

I know that I can get the first argument with% 0, the second with% 1, etc. And I can also get all arguments with% *.

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

foo.bat bar1 bar2 bar3 bar4 

How can I get only bar2 bar3 bar4 ?

+6
source share
3 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 delimiter (if one is installed)

+6
source

With the SHIFT team. But with every shift you lose the first. This will not change %* , but you can get all the arguments, but the first:

 @echo off shift set "arg_line= " :parse_args if "%~1" NEQ "" ( arg_line=%argline% "%~1" goto :parse_args ) 

you will now have all the arguments, but the first is stored in %arg_line%

+3
source

You need to use SHIFT . It moves the visible position of the parameters, then %* will receive all parameters from the shifted position. Before using SHIFT you should get the first parameters.

Additional information on SHIFT .

0
source

Source: https://habr.com/ru/post/944183/


All Articles