Sometimes, you may want to invoke another command, batch file, or program in your batch script with all of the batch script arguments. On the other hand, sometimes, you may wish to invoke the command with only some of the arguments. In this article, we see how to easily extract batch script arguments. Then, we will see how to invoke another command with those arguments.
Extract Batch Script Arguments
We can easily extract batch script arguments since we can use the variable %*.
The variable %*
provides all batch script arguments, the arguments given to a batch script when invoked. In other words, it provides all script arguments: the value of the variable %1
– the first argument, then the value of the variable %2
– the second argument, then the value of the variable %3
– the third argument, and so on.
Note that despite the similarity of %0
to %1
,%2
,%3
variables – %0
is not a script argument and is the name of the batch file name. Therefore it is not included in %*
variable.
Let’s see how can we Invoke another command with all arguments that the batch script was invoked with
a1.bat – Invoking Command With All Batch Script Arguments
@echo off
echo %*
Output
C:>a1.bat arg1 arg2 arg3 "arg4 with space"
arg1 arg2 arg3 "arg4 with space"
Extract Batch Script Arguments From Specific Position
Sometimes you may want to use the first arguments that were given to the batch file and move the rest of them to another batch file or program. How can we extract batch script arguments from specific position?
Shift Does Not Change The Value Of %*
It may seem that shift
is the right answer. shift
changes the position of batch parameters in a batch file . Unfortunately, shift
only changes the values of the batch parameters %0
through %9
by copying each parameter into the previous one (%1
is copied to %0
, %2
is copied to %1
, %2
is copied to %1
, %3
is copied to %2
and so on). However, It does not change the value of %*
as the following script demonstrate:
a2.bat – shift does not change the value of %*
@echo off
echo In start of script
echo %%0,%%1,%%2,%%3,%%4,%%5 : %0,%1,%2,%3,%4,%5
echo %%* = %*
shift
shift
echo After 2 shifts
echo %%0,%%1,%%2,%%3,%%4,%%5 : %0,%1,%2,%3,%4,%5
echo %%* = %*
Output
C:>a2.bat arg1 arg2 arg3 arg4 arg5
In start of script
%0,%1,%2,%3,%4,%5 : C:a2.bat,arg1,arg2,arg3,arg4,arg5
%* = arg1 arg2 arg3 arg4 arg5
After 2 shifts
%0,%1,%2,%3,%4,%5 : arg2,arg3,arg4,arg5,,
%* = arg1 arg2 arg3 arg4 arg5
Extract Batch Script Arguments From Specific Position
As we see, shift
does not change the value of %*. However, we can use the shift command to extract the batch script arguments from specific position. The following script define build_args
which take a position
and list of arguments
. The function will set the args
variable list of arguments
from position
.
Let’s see how can we Invoke another command with all arguments from specific position:
a3.bat – Extract The Batch Script Arguments From Specific Position
@echo off
call :build_args 2 %*
echo args from position 2 : %args%
call :build_args 3 %*
echo args from position 3 : %args%
call :build_args 4 %*
echo args from position 4 : %args%
goto :eof
:build_args
set first=%1
For /L %%k in (1,1,%first%) Do shift
set args=%1
:loop
shift
if [%1] == [] goto :eof
set args=%args% %1
goto :loop
goto :eof
output
C:>a3.bat 1 2 3 4 5
args from position 2 : 2 3 4 5
args from position 3 : 3 4 5
args from position 4 : 4 5
Faq
What is %*
variable in the batch script?
The variable %*
provides all batch script arguments; that is all the arguments given to a batch script when invoked. read more
How to Extract the batch script arguments from specific position?
While shift does not change the value of %*
, we can write a function can use it for this task with few lines of code. read more