Home Windows XP XP Syntax

Syntax : Parameters

A parameter is any value passed into a batch script:

C:> MyScript.cmd January 1234 "Some value"

Parameters may also be passed to a subroutine with CALL:

CALL :my_sub January 1234 "Some value"

Getting the value of a parameter

You can get the value of any parameter using a % followed by it's numerical position on the command line. i.e.The first item passed is always %1 the second item is always %2 and so on

It is often a good idea to set a variable equal to the parameter, if only to give things easy to read names

SET _MonthPassed=%1

%* in a batch script refers to all the arguments (e.g. %1 %2 %3 %4 %5 ...%255)

Filename Parameter Extensions

If a parameter is used to supply a filename then the following extended syntax can be applied:

we are using the variable %1 (but this works for any parameter)

%~f1 - expands %1 to a Fully qualified path name - C:\utils\MyFile.txt

%~d1 - expands %1 to a Drive letter only - C:

%~p1 - expands %1 to a Path only - \utils\

%~n1 - expands %1 to a file Name, or if only a path is present - the last folder in that path

%~x1 - expands %1 to a file eXtension only - .txt

%~s1 - changes the meaning of f, n and x to reference the Short name (see note below)

Additional parameter extensions available in Win2K / XP:

    %~1         - expand %1 removing any surrounding quotes (")
    %~a1        - display the file attributes of %1
    %~t1        - display the date/time of %1
    %~z1        - display the file size of %1
    %~$PATH:1   - search the PATH environment variable
                  and expand %1 to the fully qualified name of
                  the first match found.

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

Note on short file/folder names:
There is a bug involving the ~s option - the displayed output may be wrong if the current directory name is not the same as the 8.3 version of the directory.
A workaround is to run command.com /c rem , which will change the current directory to 8.3

e.g. if the current directory is C:\Program Files\ you will see the bug
if the current directory is C:\progra~1\ it will work fine (but then you wont see the long name)
more here

FOR command parameters

The FOR command creates parameters which are identified with a letter rather than a number.

Caveat: The modifier letters (a, d, f, n, p, s, t, x) are easily confused with FOR parameter letters - so think about your choice of letters when using FOR. Apart from making code hard to follow, this will actually cause problems under NT 4:

For example - this command works and displays a drive letter and filename:

FOR %G in (c:\autoexec.bat) DO echo %~dnG
>echo c:AUTOEXEC

Changing the variable to one of the reserved letters breaks the code:

for %n in (c:\autoexec.bat) DO echo %~dnn
>echo %~dnn

Windows 2000 will make a distinction between upper/lower case parameters and Windows XP does not seem to suffer from this problem at all.

Propagating Null's - if an input is required - test that it is actually there.

%0 - the Batch Script itself

When a CMD script is run from a network share, it may be accessed directly from the UNC share or from a mapped drive.

You can get the pathname of the .CMD script itself with %0

You cannot set the current directory to a UNC drive but you can refer to other files in the same folder as the batch script by using this syntax:

  CALL %0\..\SecondBatch.cmd

This use of %0 is a little different under Windows XP - see Q318689

Examples:

Pass parameters from one batch to another:

      MyBatch.cmd SMITH 100

Or as part of a CALL :

      CALL MyBatch.cmd SMITH 100

Passing from one part of a script to another

   Using CALL to jump to a subroutine
      CALL :s_staff SMITH 100

   As a FOR parameter:
      FOR /F %%G IN ('DIR /b *.*') DO call :s_subroutine %%G

"argument" is another name for parameter

Related commands
:

CALL - Call one batch program from another
CMD - Start a new DOS shell (cmd.exe)
FOR - Conditionally perform a command several times
SHIFT - Shift the position of replaceable parameters in a batch file

Linux BASH equivalent commands:

dirname - Convert a full pathname to just a path



Back to the Top

Simon Sheppard
SS64.com