Passing Arguments to Batch Files
As the scale of processing using batch files becomes relatively large, similar processes may appear in multiple places.
In such cases, to streamline the process, using functions or calling other batch files can improve the maintainability of batch files and make them easier to understand.
To do this, it is necessary to pass arguments to batch files.
This article introduces how to pass arguments to batch files and provides specific sample code.
How to pass arguments to batch files
Passing arguments to batch files is very simple. Just execute the call
command or the start
command followed by the arguments.
The following code is an example using the call
command.
call sample.cmd test
For the start
command, you need to specify a title, the path of the file to execute, and the arguments in that order.
start "sample" sample.cmd test
For more information on the call
and start
commands, please refer to the following articles.
Using the Received Arguments
Once you have passed arguments to a batch file, the next step is to learn how to use the received arguments.
When you pass arguments to a batch file, the arguments are stored in variables in order, such as %1
, %2
, %3
, and so on.
Example using the call
command
As an example, create a batch file called sample.cmd
as follows.
@echo off
setlocal
echo The received argument is %1.
echo The second argument is %2.
endlocal
When you execute this batch file by passing arguments, the following output will be displayed:
Since only one argument is passed, nothing is stored in %2
.
If you pass multiple arguments, the output will be as follows:
Example of the start
command
As mentioned earlier, for the start
command, you need to specify a title, the path of the file to execute, and the arguments in that order.
As an example, create a batch file called sample.cmd
as follows.
@echo off
setlocal
echo The received argument is %1.
echo The second argument is %2.
pause
endlocal
When you pass arguments to this batch file and execute it, a new command prompt will open and the following output will be displayed:
Checking the Number of Arguments
Once you are able to pass arguments to a batch file, the next step is to learn how to check the number of received arguments.
When you pass arguments to a batch file, all the passed arguments are stored in %*
.
Therefore, you can check the number of arguments by splitting the value of %*
using the for
command.
As an example, create a batch file called sample.cmd
as follows.
@echo off
setlocal
set count=0
for %%i in (%*) do (
set /a count+=1
)
echo The number of arguments is %count%.
endlocal
When you pass arguments to this batch file and execute it, the following output will be displayed:
Processing Each Received Argument
By applying the method of counting the number of arguments mentioned earlier, you can process each received argument individually.
As an example, create a batch file called sample.cmd
as follows.
@echo off
setlocal
for %%i in (%*) do (
echo The received argument is %%i.
)
endlocal
When you pass arguments to this batch file and execute it, the following output will be displayed: