| |
With the information covered so far we would be 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 that our files are written in stone, so to speak, and if we want to manipulate a file we have to hard-code it into the batch script.
What if we wanted to perform a particular operation on a different file each time? We can use replaceable parameters to do 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 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 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 in that everywhere you see a replaceable parameter; 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 ECHO %1 SHIFT ECHO %1 :: %1  |
|
|
|
|
|
|
|
|
|
The first 2 lines are remark statements that show the name of the file and a brief explanation of the program. The third line echo's the first replaceable parameter entered on the command line. If there is no replaceable parameter was entered, this command will display the Echo is on message. The fourth line shifts the argument. The line 5 acts like line 3 and echo's second argument. The last line is a remark statement that illustrates the replacement of every occurrence of the %1 parameter with the replaceable parameter entered in the command prompt.
The SHIFT command discards the first parameter %0, moves the remaining parameters down one value, and brings in a new value if one exits. 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. A comma or space is also a requirement to separate each replaceable parameter.
| |
|
| |
|