Dos - Command line (Argument|Parameter)


Command line arguments for DOS are passed to the batch script through the cmd or call command:

They are not variables. You can't test them for instance with the If Defined Variable statement.

The content of argument is interpreted. If you pass a character that has a meaning for DOS, it will be interpreted.

Example if you pass the ampersand character escaped “^&” and that you use it in your dos script, it will still be interpreted as a control operator.

The preprocessing before they are passed to the target program are:

  • ^ escape chars. outside of double-quoted strings are removed
  • the quote characters are removed.

Ref1, Ref2 over Parsing C Command-Line Arguments




where when n is, it refers to

  • 0 : the name of the batch file itself,
  • 1 : the first parameter,
  • 2 : the second and so on until the 9th
  • * : all the arguments (e.g. %1 %2 %3 %4 %5 …)

To use more parameters, you have to use the shift command. It will shift the arguments variable to the left (%2 becomes %1)


With the use of modifiers, the batch parameters (%n) can been enhanced.




  • m is a one letter modifier
  • n is the argument number <note important>And not *</note>

0 is the first parameter and can be changed by any other argument

All the below examples have been made with the file (c:\Program Files\demo.bat)

Without: remove any surrounding quotes

%~0 expands %0 removing any surrounding quotes (“)

f: fully qualified path name

%~f0 expands %0 to a fully qualified path name

echo %~f0
c:\Program Files\demo.bat

d: drive letter only

%~d0 expands %0 to a drive letter only

echo %~d0

p: path only

%~p0 expands %0 to a path only

echo %~p0
\Program Files\

n: file name only

%~n0 expands %0 to a file name only

echo %~n0

x: file extension only

%~x0 expands %0 to a file extension only

echo %~x0

x: file extension only

%~s0 expanded path contains short names only

echo %~s0

a: file attributes

%~a0 expands %0 to file attributes

echo %~a0

t: date/time of file

%~t0 expands %0 to date/time of file

echo %~t0
2013-10-09 16:55

z: size of file

%~z0 expands %0 to size of file

echo %~z0

z: size of file

%~$PATH:0 searches %0 in the directories listed in the PATH environment variable and expands %0 to the fully qualified name of the first one found. If the file defined by %0 is not found, an empty string is returned.

c:\Program Files\ must be in the path

echo %~$PATH:0
c:\Program Files\demo.bat

PATH can be replaced by other valid values.


The modifiers can be combined to get compound results:

  • %~dp1 - expands %1 to a drive letter and path only
  • %~nx1 - expands %1 to a file name and extension only
  • %~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


test if a command line argument is set

if .%1 == . ( 
	echo %1 is not set
	) ELSE (
	echo %%1 is set with the value: %1


Will shift the command line argument (1 becomes 0, 2 becomes 1,…)



A snippet on how to get a slice on the argument. The snippet takes the first argument in FIRST_ARG and put the others in ARG

:: Take the host pattern

:: Get the last argument
set ARG=""
IF "%1"=="" GOTO Continue
set ARG=%1

IF "%~1"=="" GOTO Continue
set ARG=%ARG% %1

Powered by ComboStrap