Command line arguments for DOS are passed to:
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:
%n
where when n is, it refers to
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.
Syntax:
%~mn
where:
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)
%~0 expands %0 removing any surrounding quotes (“)
%~f0 expands %0 to a fully qualified path name
echo %~f0
c:\Program Files\demo.bat
%~d0 expands %0 to a drive letter only
echo %~d0
c:
%~p0 expands %0 to a path only
echo %~p0
\Program Files\
%~n0 expands %0 to a file name only
echo %~n0
demo
%~x0 expands %0 to a file extension only
echo %~x0
.bat
%~s0 expanded path contains short names only
echo %~s0
c:\PROGRA~1\demo.bat
%~a0 expands %0 to file attributes
echo %~a0
--a------
%~t0 expands %0 to date/time of file
echo %~t0
2013-10-09 16:55
%~z0 expands %0 to size of file
echo %~z0
22
%~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:
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,…)
shift
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
SET FIRST_ARG=%1
shift
:: Get the last argument
set ARG=""
IF "%1"=="" GOTO Continue
set ARG=%1
shift
:Loop
IF "%~1"=="" GOTO Continue
set ARG=%ARG% %1
SHIFT
GOTO Loop
:Continue