Batch File Parameter Modifiers

How to Effectively Process Command Line Parameters in Batch Scripts

© Guy Lecky-Thompson

Techniques for using built-in command line parameter modifiers to change the values of parameters passed in the command line for specific programming purposes.

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:

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 OFF
ECHO %~ftza0

The would lead to output such as:

C:\My Commands>whatcmd.bat
--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:

%~$PATH:1

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


The copyright of the article Batch File Parameter Modifiers in Command Line Programming is owned by Guy Lecky-Thompson. Permission to republish Batch File Parameter Modifiers must be granted by the author in writing.




Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo