Home NT NT Syntax

NT Syntax : Parameters

A parameter is a text value passed into a batch script or passed from one part of the script to another.

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

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

The one exception to the above is that FOR parameters use a letter rather than a number.

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

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

Bugs to watch out for: 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.

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


Passing parameters from one batch to another:

      MyBatch.cmd SMITH 100   

      CMD /C 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



Simon Sheppard
SS64.com