Passing Arguments to Batch Files

Maintained on

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.

sample.cmd
@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:

×
Command Prompt icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2024 Ribbit App Development All rights reserved.
 
C:\users\user>call sample.cmd test
The received argument is test.
The second argument is .
C:\users\user>

Since only one argument is passed, nothing is stored in %2.

If you pass multiple arguments, the output will be as follows:

×
Command Prompt icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2024 Ribbit App Development All rights reserved.
 
C:\users\user>call sample.cmd test1 test2
The received argument is test1.
The second argument is test2.
C:\users\user>

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.

sample.cmd
@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:

×
Command Prompt icon
sample
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2024 Ribbit App Development All rights reserved.
 
C:\users\user>
The received argument is test.
The second argument is .
Press any key to continue …

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.

sample.cmd
@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:

×
Command Prompt icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2024 Ribbit App Development All rights reserved.
 
C:\users\user>call sample.cmd test1 test2 test3
The number of arguments is 3.
C:\users\user>

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.

sample.cmd
@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:

×
Command Prompt icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2024 Ribbit App Development All rights reserved.
 
C:\users\user>call sample.cmd test1 test2 test3
The received argument is test1.
The received argument is test2.
The received argument is test3.
C:\users\user>

Exercise

Exercise 1

Select the appropriate method for passing arguments when calling a batch file from the command prompt.

Exercise 2

Which of the following is the correct way to pass multiple numeric arguments?

#Command Prompt #Batch Files #Arguments #Command Line #Commands