|
![]() |
| 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 %* |
| %~1 | Expands %1 removing any surrounding quotes (") For example: set FileNameQuotesRemoved=%~1 |
| %~f1 | Expands %1 to a fully qualified path name For example:
set FileNameIsParm1FullyQualified=%~f1
for %%f in (".") do set FullCurrentDir=%%~ff |
| %~$PATH:1 | Searches 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%" |
| %~d1 | Expands %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% |
| %~p1 | Expands %1 to a path only |
| %~n1 | Expands %1 to a file name only For example:
set FileThis=C:\TMP\SomeFile.Extn
...
for %%f in ("%FileThis%") do set FileThisSN=%%~nf%%~xf |
| %~x1 | Expands %1 to a file extension only |
| %~s1 | Changes the meaning of n and x options to reference the short name instead |
| %~a1 | Expands %1 to file attributes |
| %~t1 | Expands %1 to date/time of file |
| %~z1 | Expands %1 to size of file |
The modifiers can be combined to get compound results:
| Code | Description |
|---|---|
| %~dp1 | Expands %1 to a drive letter and path only |
| %~nxx1 | Expands %1 to a file name and extension only |
| %~dp$PATH:1 | Searches the directories listed in the PATH environment variable for %1 and expands to the drive letter and path of the first one found. |
| %~ftza1 | Expands %1 to a DIR like output line |
![]() | ![]() |