Saturday 9 March 2013

SHIFT: Infinite Parameters

Sometimes your batch 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
it if you need to.)To see how the SHIFT command works, look at the
following
snippet of code:
@ECHO OFF
ECHO The first Parameter is %1
ECHO.
SHIFT
ECHO The Second Parameter is %1
ECHO.
SHIFT
ECHO The Second Parameter is %1
Now execute this batch file from DOS and see what happens.
C:\windows>batch_file_name abc def ghi 

The first Parameter is abc
The Second Parameter is def
The Second Parameter is ghi
How does it work? Well, each SHIFT command shuffles the parameters
down one position. This means that after the first SHIFT %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 more
efficient by grouping them with the IF conditional statement to check
the
parameters passed by the User.
THE FOR LOOP
The syntax of the FOR LOOP is:
FOR %%PARAMETER IN(set) DO command
Most people change their mind about learning Batch Programming when
they come
across the syntax of the For Command. I do agree that it does seem a
bit weird,
but it is not as difficult as it appears to be. Let's analyze the various
parts
of the For command. Before we do that 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 a 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 assigned the various values within the brackets, 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 will be:
abc
def
xyz
The FOR loop becomes very powerful if used along with replaceable
parameters. Take
the following batch file, for example,
@ECHO OFF
ECHO.
ECHO I am going to delete the following files:
ECHO %1 %2
ECHO.
ECHO Press Ctrl+C to Abort process
PAUSE
FOR %%a IN (%1 %2 ) DO DEL %%a
ECHO Killed Files. Mission Accomplished.
At execution time, the process would be something like:
C:\WINDOWS>batchfilename *.tmp *.bak
I am going to delete the following files:
*.tmp *.bak
Press Ctrl+C to Abort process
Press any key to continue . . .
Killed Files. Mission Accomplished.

No comments:

Post a Comment