Passing Arguments to Batch Files
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.
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.
start "" sample.cmd test
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.
@echo off
setlocal
echo The received argument is %1.
echo The second argument is %2.
endlocal
Running it with an argument prints the following.
Only one argument was passed, so %2 is empty.
With multiple arguments, the output looks like this.

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

60分でわかる! 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.
@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.
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.
@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.
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.
@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.
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.
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.
@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.