Hopefully "ScriptingTipsAndTricks" helps you with your batch file or vbscript scripting :-)
[Bottom][Contents][Prev]: Parameters[Next]: PATH Environment Variable (locations to search)
\->Introduction->Parameter Replacement

Parameter Replacement

You should by now know what a "parameter" is, this section discussed the use (reading and manipulation) of those values. Batch files take parameters which are referred to by numbers (1-9) and "0" is used to represent the batch file itself. The first parameter is referred to as "%1", the second "%2" etc.

To handle an unknown number of parameters you can use the "SHIFT" command.

In the table below "1" is used (for the first parameter). As the syntax only supports a single "character" for the parameter "number" there is a trick (using the ""FOR" command) to similarly process environment variables (or strings or command output) as demonstrated here (for a string) to determine the full name of the current directory:

set FileName=SomeFile.txt
for %%X in ("%FileName%") do set FullFileName=%%~fX

The Syntax of Parameter Replacement

Code Description
%*Refers to all the arguments (e.g. %1 %2 %3 %4 %5 ...). In Windows NT 4 a leading space is added to %*. In Windows 2000+ all leading spaces are removed from %*
%~1Expands %1 removing any surrounding quotes (")

For example:

set FileNameQuotesRemoved=%~1
%~f1Expands %1 to a fully qualified path name

For example:

set FileNameIsParm1FullyQualified=%~f1

for %%F in (".") do set FullCurrentDir=%%~fF
%~$PATH:1Searches the directories listed in the PATH environment variable and expands %1 to the fully qualified name of the first one found. It does not look in the current directory and returns "" if not found.

For example:

@echo off
set FullNameOfCtextExe=
for %%F in ("ctext.exe") do set FullNameOfCtextExe=%%~$PATH:F
if     "%FullNameOfCtextExe%" == "" echo "CTEXT.EXE" not found in PATH (weird)!
if not "%FullNameOfCtextExe%" == "" echo "CTEXT.EXE" found at: "%FullNameOfCtextExe%"
%~d1Expands %1 to a drive letter only

For example:

for %%X in ("%BatchFile%") do set FileDrive=%%~dX
echo FileDrive = %FileDrive%        ("\\" for UNC filename)
%~p1Expands %1 to a path only

For example:

for %%X in ("%BatchFile%") do set   FilePath=%%~pX
echo FilePath = %FilePath%   (won't have drive letter attached)
%~n1Expands %1 to a file name only

For example:

set FileThis=C:\TMP\SomeFile.Extn
...
for %%F in ("%FileThis%") do set FileThisSN=%%~nF%%~xF
echo File without path or drive (shortname) = %FileThisSN%
%~x1Expands %1 to a file extension only

For example:

for %%X in ("%BatchFile%") do set FileExtn=%%~xX
echo FileExtn = %FileExtn%
%~s1Changes the meaning of n and x options to reference the 8.3 file name instead
%~a1Expands %1 to file attributes

For example:

for %%X in ("%BatchFile%") do set FileAttr=%%~aX
echo The files attributes = %FileAttr%
%~t1Expands %1 to date/time of file

For example:

for %%X in ("%BatchFile%") do set   FileTime=%%~tX
echo File is timestamped with a modification time of: %FileTime%
%~z1Expands %1 to size of file

For example:

for %%X in ("%BatchFile%") do set   FileSize=%%~zX
echo File has %FileSize% bytes

The modifiers can be combined to get compound results:

Code Description
%~dp1Expands %1 to a drive letter and path only
%~nxx1Expands %1 to a file name and extension only
%~dp$PATH:1Searches the directories listed in the PATH environment variable for %1 and expands to the drive letter and path of the first one found.
%~ftza1Expands %1 to a DIR like output line

Many of the file manipulations above are demonstrated in the file name manipulation example code.


Microsoft awarded me an MVP (Most Valuable Professional award) in 2004, 2005, 2006, 2007, 2008 & 2009 for the Windows SDK (Windows Installer) area.Please email me any feedback, additional information or corrections.
See this page online (look for updates)

[Top][Contents][Prev]: Parameters[Next]: PATH Environment Variable (locations to search)


ScriptingTipsAndTricks© is (C)opyright Dennis Bareis 2003-2008 (All rights reserved).
Sunday September 07 2014 at 12:50pm
Visit ScriptingTipsAndTricks's Home Page
Microsoft awarded me an MVP (Most Valuable Professional award) in 2004, 2005, 2006, 2007, 2008 & 2009 for the Windows SDK (Windows Installer) area.