Passing Arguments to Batch Files

Maintained on

To pass arguments to a batch file, list the values after the batch file name, separated by spaces. The batch file receives them as %1, %2, and so on.

This article shows how to pass and receive arguments, which you need when calling another batch file to share common processing, with sample code.

How to Pass Arguments to a Batch File

Simply write the arguments after the batch file name when running it with the call or start command.

The following is an example using the call command.

Passing
call sample.cmd test

With start, the first quoted string is interpreted as the window title. If the executable path may be quoted, put an empty title "" first to be safe.

Passing
start "" sample.cmd test
A common pitfall with start

Writing start "C:\My Scripts\job.cmd" makes the first quoted string the title rather than the file path. Put an empty title first, as in start "" "C:\My Scripts\job.cmd".

For details on call and start, see the following articles.

Using the Received Arguments

Arguments passed to a batch file are stored in order in %1, %2, %3, and so on.

Example with call

Create the following batch file sample.cmd.

sample.cmd
@echo off
setlocal

echo The received argument is %1.

echo The second argument is %2.

endlocal

Running it with an argument prints the following.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 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>

Only one argument was passed, so %2 is empty.

With multiple arguments, the output looks like this.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 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>
おすすめ書籍※ 広告を含む場合があります
中小企業経営者のためのRPA入門 RPA導入を成功させる方法

中小企業経営者のためのRPA入門 RPA導入を成功させる方法

60分でわかる! AIエージェント 超入門

60分でわかる! AIエージェント 超入門

コマンドラインの黒い画面が怖いんです。

コマンドラインの黒い画面が怖いんです。

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門

Example with start

As noted above, start needs a title, so write the title, the path of the file to run, and then the arguments, in that order.

Create the following batch file sample.cmd.

sample.cmd
@echo off
setlocal

echo The received argument is %1.

echo The second argument is %2.

pause

endlocal

Running it with an argument opens a new Command Prompt window that prints the following.

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

Stripping Quotes and Special Forms (%~1, %0)

To pass a value that contains spaces, wrap it in " (for example, call sample.cmd "My File.txt"). %1 then includes the quotes, while %~1 returns the value without them.

%0 holds the name of the batch file itself, and %~dp0 returns the path of the folder where the batch file is located.

Checking the Number of Arguments

%* holds all the received arguments, but to handle values that contain spaces strictly, it is safer to count them one by one with shift.

Create the following batch file sample.cmd.

sample.cmd
@echo off
setlocal

set count=0

:count_loop
if "%~1"=="" goto count_end
set /A count+=1
shift
goto count_loop

:count_end

echo The number of arguments is %count%.

endlocal

Running it with arguments prints the following.

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

Removing Arguments with shift

The shift command discards the first argument and moves the remaining ones down by one.

Create the following batch file sample.cmd.

sample.cmd
@echo off
setlocal

echo The received argument is %1.
shift
echo The second argument is %1.
shift
echo The third argument is %1.

endlocal

Running it with arguments prints the following.

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

Every echo refers to %1, yet the output correctly shows the first, second, and third arguments, because shift moves the remaining arguments down each time.

shift works for more than 9 arguments

Only %1 to %9 can be referenced directly, but repeating shift lets you process arguments beyond %9 in order. Note also that %* does not change when shift is run.

Processing Each Received Argument

Applying the same idea, you can process each argument in turn.

Create the following batch file sample.cmd.

sample.cmd
@echo off
setlocal

:loop
if "%~1"=="" goto end
echo The received argument is %~1.
shift
goto loop

:end

endlocal

Running it with arguments prints the following.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 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>

Practice Questions

Question 1

Select the appropriate way to pass arguments when calling a batch file from Command Prompt.

回答がサーバーに送信されることはありません
Question 2

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

回答がサーバーに送信されることはありません
#Command Prompt #Batch Files #Arguments #Command Line #Commands