Batch File Programming SHIFT Cmd

Accessing Unlimited Numbers of Parameters Given on the Command Line

© Guy Lecky-Thompson

How to use the SHIFT command in batch file programming to access parameters passed in the command line, including those with more than 10 items.

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.

One issue with batch files is knowing what parameters have been passed, as well as how many have been provided. Rather than restricting users to a very closed sequence and number of batch file parameters, script programmers can create batch command files that make use of the SHIFT keyword in creative ways to access the parameter list.

Accessing Command Line Parameters

Parameters are accessed from within a batch file program by substitution. The variables that contains the parameters passed on the command line to the batch file are %0 to %9, with the name of the batch file stored in position %0. For example:

ECHO OFF
ECHO This command was called using the '%0' name.

If the reader copies the above into a batch file (whatcmd.bat) and executes it, they will see output such as:

c:\>whatcmd.bat
c:\>ECHO OFF
This command was called using the 'whatcmd.bat' name.

There are some modifiers that can be applied which change the value of the variable returned. For example, specifying %~f0 will return a fully qualified path name.

The SHIFT Keyword

The SHIFT keyword can only be used in batch file programs. The most basic use simply shifts all the parameters in the command line down by one position. So, the value of %0 becomes equal to %1, and the tenth parameter, if it exists, is placed in variable %9.

This allows the programmer to permit more than 10 parameters to be fed to a batch file program. Considering that batch file programs can be built and executed automatically (i.e. by using another batch file or command line program) this is very useful. Also, because command lines can be built up in a complex manner without user interaction (automatically), having many parameters becomes commonplace, and accessing more than 10 at a time then becomes a necessity.

However, it is irreversible, so once SHIFT has been used, the entire parameter list is irrevocably changed. So, it should be used with care, or in cases where the parameters are stored in variables for later retrieval.

There is one workaround - with command extensions enabled, the SHIFT command can be used with a parameter which allows the programmer to preserve some of the command line parameters. The /n parameter causes SHIFT to start shifting the parameters at the nth item. For example:

SHIFT /2

The above snippet would preserve parameters in positions %0 and %1, shifting everything else down one position. Parameters up to (and including) %8 can be preserved in this way; the highest value for the parameter therefore being /9.

Other Batch File Commands


The copyright of the article Batch File Programming SHIFT Cmd in Command Line Programming is owned by Guy Lecky-Thompson. Permission to republish Batch File Programming SHIFT Cmd 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