Pass parameters separated by space, and assign each argument to variable as shown below. Use %* to mean “all”. For example,
echo off
set arg1=%1 // First parameter
set arg2=%2
shift
shift
some-command /u %arg1% /p %arg2% %*
When you run:
test-command admin password foo bar
the above batch file will run:
fake-command /u admin /p password foo bar
If you want to handle missing parameters you can do something like. Dot/period is used in equality operations, because if %1
is empty, this will end up being IF . == . and so the GOTO will happen.
IF %1.==. GOTO No1
... do stuff...
GOTO End1
:No1
ECHO No param 1
GOTO End1
:End1
Argument %0
will initially refer to the path that was used to execute the batch, e.g. MyBatch.cmd if in the current directory or a full path like C:\apps\myBatch.cmd
SHIFT, change the position of command line arguments passed to a batch file. Only parameters %1 to %9 can be referenced by number, but it is possibly to pass more than 9 arguments. The SHIFT command provides a way of accessing these additional argument values.
Examples:
Demobatch.cmd The Quick Brown
As given %1=The, %2=Quick, %3=Brown
SHIFT will result in %1=Quick, %2=Brown
A second SHIFT, will result in %1=Brown
Given %1=the, %2=quick, %3=brown, %4=fox
SHIFT /2, will result in %1=the, %2=brown, %3=fox
References :- SHIFT