How to Parse Command-Line Arguments (%1, %~1, %*) in Batch Script
Command-line arguments are the primary way to pass information into a batch script, making it a flexible and reusable tool. Instead of hardcoding a filename or a setting, you can provide it when you run the script. Batch scripting has a built-in system for accessing these arguments using special percent-sign parameters (%1, %2, etc.).
This guide will teach you how to access individual arguments, how to handle all arguments at once, and how to use the powerful tilde (~) modifiers to automatically remove quotes and parse file paths. Mastering these parameters is essential for creating powerful and professional command-line tools.
What are Command-Line Arguments?
Arguments are the pieces of information you type on the command line after the script's name. They are separated by spaces.
MyScript.bat argument1 "argument 2 with spaces" /switch
In the example above, the script receives three distinct arguments.
Accessing Individual Arguments: %1, %2, ... %9
Batch provides a set of special variables to access the first nine arguments directly:
%0: The name of the script itself.%1: The first argument.%2: The second argument.- ...
%9: The ninth argument.
@ECHO OFF
ECHO The script name is: %0
ECHO The first argument is: %1
ECHO The second argument is: %2
ECHO The third argument is: %3
Usage and Output:
C:\> ShowArgs.bat first "second arg" third
The script name is: ShowArgs.bat
The first argument is: first
The second argument is: "second arg"
The third argument is: third
Notice that the quotes around "second arg" are preserved in the %2 variable.
Accessing All Arguments: %*
The %* parameter is a special variable that expands to all arguments that were passed to the script, starting from the first one. It is a single string containing everything the user typed after the script name.
@ECHO OFF
ECHO All arguments passed to the script are: %*
Usage and Output
C:\> MyScript.bat alpha beta gamma /debug
All arguments passed to the script are: alpha beta gamma /debug
The Power of the Tilde (~) Modifier: %~1
The tilde (~) is a modifier that changes how an argument is expanded. Its most common use is to remove any surrounding double quotes from an argument. This is essential for handling file paths that contain spaces.
Problem
If a user drags a file with spaces onto your script, %1 will contain quotes: "C:\My Files\data.txt". If you try to use this to build a new path, you'll get an invalid result like ""C:\My Files\data.txt".log".
Solution
Using %~1 gives you the clean, un-quoted path.
Example of correct script
@ECHO OFF
ECHO Raw argument %%1 is: %1
ECHO Clean argument %%~1 is: %~1
ECHO.
ECHO Recommended usage: "%~1"
Usage and Output:
C:\> CleanArg.bat "C:\Program Files\App\run.exe"
Raw argument %1 is: "C:\Program Files\App\run.exe"
Clean argument %~1 is: C:\Program Files\App\run.exe
Recommended usage: "C:\Program Files\App\run.exe"
The best practice is to use %~1 to get the clean value and then put your own quotes around it to ensure it's handled safely.
Handling More Than 9 Arguments with SHIFT
You can only access the first nine arguments directly with %1 through %9. To access the tenth argument and beyond, you must use the SHIFT command.
The SHIFT command discards the first argument (%1) and shifts all subsequent arguments down by one position. The old %2 becomes the new %1, the old %3 becomes the new %2, and the old %10 becomes the new %9.
@ECHO OFF
ECHO First arg: %1
SHIFT
ECHO After SHIFT, first arg is now: %1
You can use SHIFT in a loop to process an unlimited number of arguments.
The %~ Modifier Cheat Sheet
The tilde modifier can do much more than just remove quotes. It can deconstruct a file path passed as an argument.
Assuming %1 is "C:\My Project\report.docx":
| Modifier | Meaning | Example Output |
|---|---|---|
%~1 | Removes quotes | C:\My Project\report.docx |
%~f1 | full path | C:\My Project\report.docx |
%~d1 | drive letter | C: |
%~p1 | path (folder) | \My Project\ |
%~n1 | filename only | report |
%~x1 | extension only | .docx |
%~s1 | short (8.3) path | C:\MYPROJ~1\REPORT.DOCX |
%~dp1 | drive and path | C:\My Project\ |
%~nx1 | name and extension | report.docx |
Practical Example: A Simple File-Processing Tool
This script demonstrates a real-world use case. It expects a filename as the first argument and an optional /backup switch as the second.
@ECHO OFF
SETLOCAL
REM --- Get the clean file path from the first argument ---
SET "SourceFile=%~1"
SET "BackupSwitch=%2"
ECHO --- File Processor ---
ECHO.
REM --- Validate input ---
IF "%SourceFile%"=="" (
ECHO [ERROR] No filename provided.
ECHO Usage: %~n0 "path\to\file.txt" [/backup]
GOTO :End
)
IF NOT EXIST "%SourceFile%" (
ECHO [ERROR] File not found: "%SourceFile%"
GOTO :End
)
ECHO Processing file: "%SourceFile%"
REM (File processing logic would go here)
ECHO ...done.
ECHO.
REM --- Check for the optional backup switch ---
IF /I "%BackupSwitch%"=="/backup" (
ECHO Backup switch detected. Creating backup...
COPY "%SourceFile%" "%SourceFile%.bak" > NUL
ECHO Backup created.
)
:End
ENDLOCAL
Conclusion
Command-line arguments are the key to transforming a static script into a flexible command-line tool.
- Use
%1,%2, ...` to access individual arguments. - Use
%*to access all arguments as a single string. - The tilde (
~) modifiers are essential for robust scripting.%~1should be your default way to access arguments to safely remove quotes. - Use the other modifiers like
%~dp1(path) and%~nx1(name) to easily parse file paths passed to your script. - Use
SHIFTto process more than nine arguments.
By mastering these parameters, you can write professional-grade batch scripts that are powerful and easy to use.