TExcelTAD
Home › Windows CMD & Batch › Arguments: %1 & %~

Arguments: %1 & %~

When you run backup.bat C:\data "D:\My Backups", the words after the script name are its arguments. Inside the script they're available as %1, %2, ... %9, the whole argument list is %*, and %0 is the script itself. The %~ modifiers take these values apart: strip quotes, get the folder, file name, extension or size.

The playground runs C:\tad\main.bat without arguments, so the examples on this page pass arguments to a subroutine with call :label. Inside a subroutine %1, %* and all the modifiers work exactly as they do for a whole script.

%1 to %9 and %*

cmd · try it yourself
@echo off
call :args report.txt "My Documents" 42
goto :eof

:args
echo 1: %1
echo 2: %2
echo 3: %3
echo 4: [%4]
echo all: %*
goto :eof
1: report.txt
2: "My Documents"
3: 42
4: []
all: report.txt "My Documents" 42

Arguments are separated by spaces (and also commas, semicolons and =). Quotes keep "My Documents" together, but the quotes stay part of %2. A missing argument is simply empty.

At a real prompt the same thing looks like this:

cmd
C:\Users\ada>type show.bat
@echo off
echo 1: %1
echo 2: %2
echo all: %*

C:\Users\ada>show.bat report.txt "My Documents"
1: report.txt
2: "My Documents"
all: report.txt "My Documents"

%~1: removing quotes

%~1 is %1 with surrounding quotes removed. That lets you add your own quotes consistently, whether or not the user quoted the argument:

cmd · try it yourself
@echo off
call :open "C:\Program Files\App\app.ini"
call :open C:\temp\plain.ini
goto :eof

:open
echo raw:   %1
echo clean: %~1
echo safe:  "%~1"
goto :eof
raw:   "C:\Program Files\App\app.ini"
clean: C:\Program Files\App\app.ini
safe:  "C:\Program Files\App\app.ini"
raw:   C:\temp\plain.ini
clean: C:\temp\plain.ini
safe:  "C:\temp\plain.ini"

The rule of thumb: read arguments with %~1 and write them inside quotes, "%~1". Writing "%1" would double the quotes (""C:\Program Files\..."") when the user already quoted the path.

Path modifiers

The ~ can be combined with letters that treat the argument as a file path:

Modifier Meaning
%~1 remove surrounding quotes
%~f1 full path
%~d1 drive letter, e.g. C:
%~p1 path (folders) only, e.g. \tad\
%~n1 file name without extension
%~x1 extension, including the dot
%~s1 short 8.3 name
%~a1 attributes
%~t1 last-modified date and time
%~z1 size in bytes
%~$PATH:1 search the folders in PATH, return the first full match

Letters combine: %~dp1 is drive + path (the folder), %~nx1 is name + extension. The path letters don't check that the file exists; they just work on the text, completed with the current directory where needed.

cmd · try it yourself
@echo off
call :parts "D:\Projects\site\index.final.html"
goto :eof

:parts
echo drive:      %~d1
echo path:       %~p1
echo name:       %~n1
echo extension:  %~x1
echo name+ext:   %~nx1
echo folder:     %~dp1
echo full:       %~f1
goto :eof
drive:      D:
path:       \Projects\site\
name:       index.final
extension:  .html
name+ext:   index.final.html
folder:     D:\Projects\site\
full:       D:\Projects\site\index.final.html

Only the last dot starts the extension, so index.final is the name.

A relative argument is resolved against the current directory, and %~z1 reads the size of a file that exists:

cmd · try it yourself
@echo off
> notes.txt echo twelve chars
call :info notes.txt
goto :eof

:info
echo full path: %~f1
echo size:      %~z1 bytes
goto :eof
full path: C:\Users\learner\notes.txt
size:      14 bytes

The script ran from C:\Users\learner, so that's where notes.txt lives. The 14 bytes are the 12 characters plus the CR LF line ending that echo writes.

%0 and %~dp0: where is the script?

%0 is the script as it was called, and modifiers work on it too. The most useful expression in batch scripting is %~dp0: the folder that contains the running script, always ending with a backslash. Use it to find files that ship next to the script, no matter which directory the user ran it from:

cmd · try it yourself
@echo off
echo script:      %~f0
echo name:        %~nx0
echo script dir:  %~dp0
echo current dir: %CD%
echo config file: %~dp0settings.ini
script:      C:\tad\main.bat
name:        main.bat
script dir:  C:\tad\
current dir: C:\Users\learner
config file: C:\tad\settings.ini

Note that the current directory (C:\Users\learner) and the script's folder (C:\tad\) are different. A script that opens settings.ini without %~dp0 would look in the wrong place. Because %~dp0 already ends in \, write %~dp0settings.ini, not %~dp0\settings.ini. A common first line is cd /d "%~dp0" or pushd "%~dp0".

Inside a subroutine %0 is the label (:info), not the script. Read %~dp0 in the main part and store it in a variable if subroutines need it.

shift: processing any number of arguments

%1 to %9 reach only nine arguments. shift moves every argument one place to the left: %2 becomes %1, and so on. Loop with shift until %1 is empty to handle any number of arguments:

cmd · try it yourself
@echo off
call :each alpha "beta gamma" delta
goto :eof

:each
if "%~1"=="" goto :eof
echo argument: %~1
shift
goto each
argument: alpha
argument: beta gamma
argument: delta

Note goto each (not call) loops inside the same subroutine call, so shift keeps working on the same list. %* is not affected by shift; it always holds the original arguments.

Options and validation

Batch has no argument parser, so scripts check arguments by hand. A typical pattern: show usage when a required argument is missing, and recognise switches:

cmd · try it yourself
@echo off
call :deploy
echo ---
call :deploy /v staging
goto :eof

:deploy
set "verbose="
if /i "%~1"=="/v" (set "verbose=1" & shift)
if "%~1"=="" (
  echo usage: deploy [/v] target
  exit /b 1
)
echo deploying to %~1
if defined verbose echo verbose mode is on
exit /b 0
usage: deploy [/v] target
---
deploying to staging
verbose mode is on

exit /b 1 returns an error code to the caller (see Exit Codes & errorlevel).

Comparison with other shells

Task CMD Bash PowerShell
first argument %1 / %~1 $1 $args[0] or param($Path)
all arguments %* "$@" $args
count count in a loop $# $args.Count
script folder %~dp0 $(dirname "$0") $PSScriptRoot
file name parts %~n1, %~x1 ${f%.*}, ${f##*.} [IO.Path]::GetExtension()

Bash covers the same ideas in Bash Script Arguments; PowerShell replaces positional arguments with named parameters, see PowerShell Scripts & Scopes.

Learn more: call & Subroutines, Batch for Loops (the same modifiers on %%f).

Test yourself

What does %~dp0 give in C:\tools\build.bat?

  1. C:\tools\build.bat
  2. C:\tools\
  3. build.bat
  4. The current directory

The script was called with "My File.txt". What is the safest way to use it?

  1. %1
  2. "%1"
  3. "%~1"
  4. %~n1

What does shift do?

  1. Removes %* entirely
  2. Moves each argument one position left, so %2 becomes %1
  3. Uppercases the arguments
  4. Swaps %1 and %2