How can I pass arguments to a batch file?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Batch files accept arguments through positional parameters %1 through %9. When you run myscript.bat hello world, %1 is hello and %2 is world. The special variable %0 holds the script's own filename, and %* expands to all arguments as a single string. For more than nine arguments, use the SHIFT command to rotate parameters left.
Basic Positional Parameters
Arguments are passed on the command line separated by spaces and accessed inside the batch file using %1, %2, through %9.
Running it:
Output:
If fewer arguments are provided than referenced, the missing parameters expand to empty strings. %4 in the example above would expand to nothing.
The %0 and %* Special Variables
%0 is the name (or full path) of the batch file itself. %* represents all arguments combined.
Running deploy.bat staging us-east-1 verbose produces:
%* is particularly useful when passing all arguments to another command:
Handling Arguments with Spaces
Arguments containing spaces must be wrapped in double quotes on the command line:
Inside the batch file, %1 includes the quotes: "C:\Users\John Smith\report.txt". To strip the quotes, use the ~ modifier:
Output:
Parameter Modifiers
The %~ syntax provides powerful modifiers for extracting parts of file paths:
| Modifier | Expands to | Example (if %1 is "C:\src\app.exe") |
%~1 | Removes surrounding quotes | C:\src\app.exe |
%~f1 | Full path | C:\src\app.exe |
%~d1 | Drive letter only | C: |
%~p1 | Path only (no drive, no filename) | \src\ |
%~n1 | Filename without extension | app |
%~x1 | Extension only | .exe |
%~dp1 | Drive + path | C:\src\ |
%~nx1 | Filename + extension | app.exe |
%~z1 | File size in bytes | 45056 |
%~t1 | Timestamp of file | 06/18/2026 10:30 AM |
These modifiers work for any parameter, not just file paths, though the path-specific modifiers only produce meaningful results when the argument is actually a valid file path.
Validating Arguments
Always check whether required arguments were provided before using them:
Using %~1 (with tilde) inside the if comparison strips quotes, preventing syntax errors when arguments contain spaces.
Using SHIFT for More Than Nine Arguments
The SHIFT command moves all parameters one position to the left: %2 becomes %1, %3 becomes %2, and so on. The original %1 is discarded.
Running process.bat alpha beta gamma delta outputs:
This pattern is essential for batch files that accept a variable number of arguments.
Named Parameters with Flags
Batch files do not have built-in named parameter support, but you can implement it with a parsing loop:
Usage:
Practical Example: Build and Deploy Script
Here is a realistic batch file that uses argument handling for a deployment workflow:
Batch Arguments vs. PowerShell Parameters
| Feature | Batch (%1) | PowerShell (param()) |
| Positional parameters | %1 through %9 | $args[0], $args[1], ... |
| Named parameters | Manual parsing required | Built-in with param() block |
| Type validation | None | Supports [string], [int], etc. |
| Default values | Manual (if "%~1"=="" set ...) | param($Name = "default") |
| More than 9 args | Requires SHIFT | Unlimited via $args array |
| Quote handling | %~1 strips quotes | Automatic |
| Tab completion | None | Built-in for named params |
For new scripts, PowerShell is almost always the better choice. Batch files are relevant when you need compatibility with systems that do not have PowerShell installed or when maintaining legacy scripts.
Common Pitfalls
Not quoting %1 in if comparisons. If %1 is empty, if %1=="" ... becomes if =="" ..., which is a syntax error. Always use if "%~1"=="" with both tilde (to strip outer quotes) and surrounding quotes (to handle empty values).
Forgetting setlocal in scripts that set variables. Without setlocal, variables set inside the batch file leak into the calling shell session. This can cause confusing behavior when running the script multiple times. Always start with setlocal.
Using %VAR% inside a parenthesized block. Variable expansion happens when the block is parsed, not when each line executes. Inside if or for blocks, use !VAR! with setlocal enabledelayedexpansion:
Confusing exit with exit /b. Using exit without /b closes the entire command prompt window. Use exit /b [errorlevel] to return from the batch file while keeping the parent shell open.
Passing arguments with special characters. Characters like &, |, <, >, and ^ have special meaning in CMD. Arguments containing these must be escaped or quoted:
Summary
Batch files receive arguments through %1 to %9 for positional access, %* for all arguments combined, and %0 for the script name. Use %~1 to strip surrounding quotes and %~dp1 and similar modifiers to extract path components. Validate arguments early with if "%~1"=="" checks. For more than nine arguments, loop with SHIFT. For named flag-style parameters, implement a parsing loop with if /i comparisons. Always use setlocal to prevent variable leakage and exit /b instead of bare exit to avoid closing the calling shell.
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.