MAKEMSI quickly and reliably creates MSI files in a non-programmatic way
Have your say! Join the MAKEMSI discussion list or view archive! Suggest improvements. No question too simple or too complex.
[Bottom][Contents][Prev]: Batch File Tips and Tricks[Next]: Convert Long FileNames to Short (8.3)
Have your say! Join the MAKEMSI discussion list or view archive! Suggest improvements. No question too simple or too complex.
\->Tips and Tricks->Batch Files->Batch File Tips and Tricks->Batch File Parameters

Batch File Parameters

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.

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 to similarly process environment variables (or strings or command output) as demonstrated here (for a string) to deteremine the full name of the current directory:

for %%f in (".") do set FullCurrentDir=%%~ff

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:

@echo off
@rem *** Demonstrates main file manipulations ***
setlocal
cls
set    BatchFile=%~f0
set BatchFileDir=%~dp0
for %%x in ("%BatchFile%") do set File83Name=%%~fsx
for %%x in ("%BatchFile%") do set FileDrive=%%~dx
for %%x in ("%BatchFile%") do set FilePath=%%~px
for %%x in ("%BatchFile%") do set FileExtn=%%~xx
for %%x in ("%BatchFile%") do set FileAttr=%%~ax
for %%x in ("%BatchFile%") do set FileTime=%%~tx
for %%x in ("%BatchFile%") do set FileSize=%%~zx
for %%f in ("%BatchFile%") do set FileNoPath=%%~nf%%~xf

echo BatchFile    = %BatchFile%
echo BatchFileDir = %BatchFileDir%
echo File83Name   = %File83Name%
echo FileDrive    = %FileDrive%
echo FilePath     = %FilePath%
echo FileExtn     = %FileExtn%
echo FileAttr     = %FileAttr%
echo FileTime     = %FileTime%
echo FileSize     = %FileSize%
echo ShortName    = %FileNoPath%
%~p1Expands %1 to a path only
%~n1Expands %1 to a file name only

For example:

set FileThis=C:\TMP\SomeFile.Extn
...
for %%f in ("%FileThis%") do set FileThisSN=%%~nf%%~xf
%~x1Expands %1 to a file extension only
%~s1Changes the meaning of n and x options to reference the short name instead
%~a1Expands %1 to file attributes
%~t1Expands %1 to date/time of file
%~z1Expands %1 to size of file

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


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]: Batch File Tips and Tricks[Next]: Convert Long FileNames to Short (8.3)


MAKEMSI© is (C)opyright Dennis Bareis 2003-2008 (All rights reserved).
Saturday May 28 2022 at 3:11pm
Visit MAKEMSI'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.