Dos - Command line (Argument|Parameter)

Card Puncher Data Processing

About

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:

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

Ref1, Ref2 over Parsing C Command-Line Arguments

Syntax

Default

%n

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)

Modifiers

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

Syntax:

%~mn

where:

  • m is a one letter modifier
  • n is the argument number And not *

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
c:

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
demo

x: file extension only

%~x0 expands %0 to a file extension only

echo %~x0
.bat

x: file extension only

%~s0 expanded path contains short names only

echo %~s0
c:\PROGRA~1\demo.bat

a: file attributes

%~a0 expands %0 to file attributes

echo %~a0
--a------

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
22

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.

Combinations

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
  • %~dpPATH: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

Management

test if a command line argument is set

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

Shift

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

shift

Slice

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





Discover More
Process States
Command Argument

This page is argument of a command. When the command is started via a shell script (manual or automatic), the arguments: are separated by one or more blank space should be quoted if the value...
Card Puncher Data Processing
DOS - PATH

PATH in dos may refer to: the or to the path of a file. The PATH command displays or sets a search path for executable files. How to transform a relative path to a qualified path By...
Card Puncher Data Processing
DOS - Special Characters

This article tries to list all special characters and their meanings. Character Description Articles | pipeline > stdout < stdin 2> stderr >> append & duplicate handle or control...
Card Puncher Data Processing
Dos - (Batch) Script

The batch script is a text file with the extension .bat or .cmd that is interpreted by a batch interpreter. You can call a batch script by : writing its name in the source script: The script execution...
Card Puncher Data Processing
Dos - Call (Subroutine, function)

The command “call” calls: one batch script passing the (variable|execution) context (the environment variable) a label (ie function) START where: argument The CALL command now accepts...
Card Puncher Data Processing
Dos - DOSKEY (command history)

Doskey is an utility that: edits command lines, recalls Windows commands, and creates macros. Switches Description /REINSTALL Installs a new copy of Doskey. /LISTSIZE=size Sets size...
Card Puncher Data Processing
Dos - Start Command

Starts a (separate|independent) window to run a specified batch script, program or command. It ensures that the process detaches from the console window, which allows the command to run concurrently....
Card Puncher Data Processing
How to create a DOS function ? (known also as a subroutine)

functions in dos are: created and named with a label called with the CALL command creating a subroutine exited with the goto command where: arguments are the argument of the subroutine goto...



Share this page:
Follow us:
Task Runner