Batch file programming is a very useful way to automate small, repetitive, tasks. For those who are new to creating batch files, the Windows Command Line Programming tutorial is a good starting place to read about the basics of batch file programming.
Batch file parameters are arguments that are passed to batch files just like passing any parameters to a command accessed in the command line (be it MS-DOS, Windows XP, or a MS-DOS compatible command line interface). Rather than just taking in the parameters as the user (or batch file command script) specified them, the Windows command line allows a number of modifiers to be applied which change the expansion of the parameters.
Parameter expansion is the term used to specify what the command processor (Windows or MS-DOS command line environment) does when it retrieves the parameter accessed by one of the built-in variables %0-%9.
List of Modifiers
The most commonly used modifiers are:
- %~ - takes the parameter and removes any quotation marks
- %~f - takes the parameter and makes a filename out of it, assuming the file exists
- %~$PATH: - searches the PATH variable, to locate the file (as in %~f$PATH:1)
- %~s - use short file names only
- %~a - retrieves the attributes
- %~t - retrieves the date and time
- %~z - retrieves the size
- %~d - expands to a drive letter
These can all be combined together in a variety of ways.
Using Batch Parameter Expansion Modifiers
The modifiers above can also be combined to generate specific variations of the parameters passed on the command line, which include complex information. One of the most useful is to retrieve a directory listing style output line with the file name, date and time, attributes and size. For example:
ECHO %~ftza0
The would lead to output such as:
--a------ 12/20/2007 12:16 PM 26 C:\My Commands\whatcmd.bat
The information can then be passed to the FOR command to be tokenized. This uses a modified version of the technique explained in the article discussing Using FOR Command to Parse Files.
Other combinations include building a complete fully qualified pathname from a parameter, by searching the PATH variable, which may have been set to a local environment (as explained in the Batch Programming Local Commands article. For example:
The above returns either the completely qualified pathname, or a blank value, making it easy to test for the existence of a specific command, executable application or batch script, using the IF command.
Other Batch File Commands
- Batch File Programming IF Commands
- Batch File Programming FOR Command
- Using Batch Labels, Goto and Call in Batch Programming
- Batch Programming Echo Command
- Batch Programming Local Commands
- Batch File Programming Shift Command
Join the Conversation