Replaceable parameters in batch file

This article is from our Febooti archive, it was relevant then, and I think that it is still relevant today (a few details changed).
Previous article: Stopping a DOS batch file.


With the information covered so far we are able to use batch files to make our lives easier, and help our day to day computing operations. However, we could make our batch scripts more sophisticated by avoiding the big limitation that we have been faced with so far. That is – our files are hard-coded (written in stone, so to speak) and if we want to manipulate a file, we have to change the batch script again and again.

What if we wanted to perform a particular operation on a different file each time? We can use replaceable parameters to help us with this task. The two important factors in using replaceable parameters are – how to code them in the batch file, and second – how to give the values for the replaceable parameters to the batch file. The latter is rather quite easy. Replaceable parameters are to be entered after the batch file name and before pressing the Enter key. For example:

C:\>batch-params.bat Par1 Par2 Par3 Par4 Par5

Either a space or a comma character separates each parameter. Both are treated the same. Now, how many replaceable parameters does the above line have? The obvious answer would be five. This would be wrong. DOS counts the name of the batch file itself as a parameter, so there are 6 replaceable parameters on this line. Much like other programming languages DOS names them %0, %1, %2, %3, %4, %5 rather than %1 through %5.

DOS uses replaceable parameters as if they do not exist – for example, everywhere you see a replaceable parameter, the DOS sees the value of that parameter. When a batch script executes with ECHO ON, the DOS precedes each command in the batch file prompt. DOS treats the commands in the batch file exactly like it would if you entered them from the DOS prompt.

:: batch-params.bat
:: Example with Replaceable Parameters.

The first 2 lines are remark statements that show the name of the file and a brief description of the program. The third line echo’s the first replaceable parameter entered on the command line. If replaceable parameter was not entered, the command will display the Echo is on message (just like with simple ECHO command). The fourth line shows the 2nd parameter. The fifth line shifts the argument. The line 6 acts like line 4, but because of SHIFT, it and echo’s third argument. SHIFT command allows to use more than 10 parameters / arguments in the batch file:

SHIFT changes the values of the batch parameters %0 through %9 by copying each parameter into the previous one. In other words, the value of %1 is copied to %0, the value of %2 is copied to %1, and so on. More in MS Help site.

The SHIFT command discards the first parameter %0, moves the remaining parameters down one value, and brings in a new value if one exists. Therefore after the SHIFT command gets read, the value in %1 is moved into %0, and the value in %2 moves into %1 and so on and so forth.

The SHIFT command really has two major purposes. First, by moving the parameters down into a lower replaceable parameter, a single replaceable parameter may be used for all coding by forcing the batch script to loop through the code. Second, it allows the batch file to handle more than nine replaceable parameters. The total number of replaceable parameters is constrained by the 127-character command line limitation (for old OSes), and starting Windows XP or later you can use longer command line – up to 8191 characters. A space, comma or semicolon is also a requirement to separate each replaceable parameter.

If you start our example batch file with the parameters: text1, text2, text3 and text4, you will see the following output:
C:\>batch-params.bat text1 text2 text3 text4
C:\>ECHO text1
C:\>ECHO text2
C:\>ECHO text3

And, note: we do not access fourth parameter (%4), so it is not shown in the console. Also, see the screenshot from Windows 8.1 CMD.exe:

Showing output example from the batch file with parameters
Showing output example from the batch file with parameters

Next article: Loops in the Batch files with examples.

This article is from our Febooti archive, it was relevant then, and I think that it is still relevant today (a few details changed).

Update Feb 21, 2015. Added link to the next article.