Command Prompt for Windows

Replaceable Parameters

Batch files can be passed parameters on the command line. These parameters can be referenced by using replaceable parameters. There are a maximum of 10 replaceable parameters allowed, specified by %0 through to %9 inside a batch file and the special cases %# which returns the number of parameters passed to the batch file, %* which returns the batch files original command line tail and %~ which is a modifier. The ~ modifiers have the following syntax :-

~remove any surrounding double quotes (ie. ").
~areturn the file attributes.
~dreturn the drive letter only.
~freturn the fully qualified path name.
~nreturn the file name only.
~preturn the path only.
~sthe path returned contains short names only.
~treturn the date/time of file.
~xreturn the file extension only.
~zreturn the size of file.
~$env:search the directories listed in the environment variable and return the fully qualified name of the first one found. If the environment variable name is not defined or the file is not found by the search, then an empty string is returned.
numthe replaceable batch parameter 0 to 9.

Modifiers can be combined to get compound results, for example :-

%~dp2return the drive letter and path only for parameter %2.
%~nx1return the file name and extension only for parameter %1.

%~dp$PATH:3searches the directories listed in the PATH environment variable and return the drive letter and path of the first one found for parameter %3.

%~ftza0return the DIR like output line for parameter %0.

There is no particular order the modifiers need to be specified. env can be any valid environment variable, such as the PATH environment variable. The ~ modifiers can not be used with %*.

For example, consider the batch file below called PARAMS.BAT. This batch file displays the total number of parameters, along with the first 10 parameters passed to the batch file with the double quotes removed from each parameter :-

ECHO %%* is %*
ECHO %%# is %#
LET num=0
FOR %%i IN (%~0,%~1,%~2,%~3,%~4,%~5,%~6,%~7,%~8,%~9) DO {
    ECHO %%%num% is %%i
    LET num=%num% + 1

Running the above batch file from the command prompt :-

PARAMS """abc""" 123

will display the following :-

%* is """abc""" 123
%# is 2
%0 is PARAMS
%1 is abc
%2 is 123