SHIFT: Infinite Parameters
Sometimes your bath file
program may need to use more than nine parameters at a time. )Actually, you
would never need to , but at least you are sure you can handle if you need to.) To see how the SHIFT
command works, Look at the following code
@EACHO OFF
ECHO The first parameters is % 1
ECHO
SHIFT
ECHO The Second parameter is %1
ECHO
SHIFT
ECHO The Third Parameter is %1
Now, execute this batch file from DOS and see what
happens.
C:\ windows>botch_
file_name abc def ghi
The first parameter is
abc
The second parameter is def
The third parameter is ghi
How does it work? Well,
each SHEFT command shuffles the parameters down one position. This means
that after the first SHEFT %1 becomes def, %2 becomes ghi and abc is completely
removed by DOS. All parameters change and move one position down.
Both normal parameters (%1, %2, etc.) and the SHIFT
command can be made efficient by grouping them with the IF conditional statement
to check the parameters passed by the user.
The For Loop
For % % PARAMETER IN ( set ) DO command
Most people change their mind
about learning bath programming when they come across the syntax to the for
command. Let’s analyses the carious parts of the For command. Before we do that,
one should look at the following example.
@ECHO OFF
CLS
FOR % % A IN (abc, def, xyz) DO ECHO % % A
Basically, a For Loop declares a variable (%%A) and assigns it different values as it goes through the predefined set of values (abc, def , xyz) and each time, the variable is assigned a new value, the FOR loop performs a command. (ECHO%%A)
The %% A is the variable, which is assigned different values as the loop goes through the predefined set of values in the brackets. You can use any single letter character after the Two % sign except 0 through 9. We use two %’s as DOS deletes each occurrence of single % sign in a batch file program.
The IN ( abc, def, xyz ) is the list through which the FOR loop goes.The variable %% A is assignee the various values whithin the bracket, as the loop moves, The items in the set (the technical term for the set of values within the brackets) can be separated with commas, colons or simply spaces.
For each item in the set (the IN Thing), the FOR loop performs whatever command is given after the Do keyword. (In this example the loop will ECHO %%A)
SO, basically, when we execute the above batch file, the output wile be:
abc
def
xyz
The For loop becomes very powerful if used along with replaceable parameters. Take the following batch file, for example.