Quincy Center for Technical Education
Computer Technology Department

% (Replaceable parameter)

% (Replaceable parameter)

The replaceable parameters %0 and %1 to %9 can be placed anywhere within a batch file. When the batch file is run, %0 is replaced by the name of the batch file, and the argument variables %1 to %9 are replaced by the corresponding parameters entered on the command line.

For example, to copy the contents of one folder to another, you would add the following statement in your batch file:

xcopy %1\*.* %2

When you run the file, you would type the following:

mybatch.bat C:\afolder D:\bfolder.

The effect is the same as if you had written xcopy C:\afolder \*.* D:\bfolder in the batch file.

The % parameter expands the batch script argument variables (%0, %1, ..., %9) as follows:

%* in a batch script is a wildcard reference to all the arguments. For individual argument variables, the expansion options are explained in the following tables.

Variable

Description

%~1

expands %1 and removes any surrounding quotes (")

%~f1

expands %1 to a fully qualified path name

%~d1

expands %1 to a drive letter

%~p1

expands %1 to a path

%~n1

expands %1 to a file name

%~x1

expands %1 to a file extension

%~s1

expanded path contains short names only

%~a1

expands %1 to file attributes

%~t1

expands %1 to date/time of file

%~z1

expands %1 to size of file

%~$PATH:1

searches the directories listed in the PATH environment variable and expands %1 to 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 this modifier expands to the empty string.

The modifiers can be combined to get compound results:

Variable

Description

%~dp1

expands %1 to a drive letter and path

%~nx1

expands %1 to a file name and extension

%~dp$PATH:1

searches the directories listed in the PATH environment variable for %1 and expands to the drive letter and path of the first one found

%~ftza1

expands %1 to a dir-like output line

In the above examples %1 and PATH can be replaced by other valid values. The %~ syntax must be terminated by a valid argument number. The %~ modifiers may not be used with %*.