Thursday, March 29, 2007

A parameter is any value passed into a batch script:

parameters: "A parameter is any value passed into a batch script:"


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 parameterYou 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 ExtensionsIf 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 nameAdditional 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

No comments: