Name
shift
Synopsis
Delete the variable
that holds the first command-line argument (%1
)
and shifts over the remaining arguments.
Syntax
shift
Description
Use the shift
command when you want to cycle
through all of the command-line arguments specified when the batch
file was run. When a shift
statement is
encountered, the value stored in %2
is assigned to
%1
, the value stored in %3
is
assigned to %2
, and so on. The value stored in
%1
before the shift
statement
is lost. This is particularly useful when processing loops.
Examples
In the following batch file, shift
is used so that
each of the command-line options becomes option #1
(%1
) for processing within the loop. The beauty is
that this works regardless of the number of arguments entered:
@echo off rem MTYPE.BAT rem example: mtype foo.txt bar.txt *.bat :loop if "%1"=="" exit for %%f in (%1) do type %%f shift pause goto loop
The if
statement tests for an empty argument
(meaning that shift
has exhausted the supply of
arguments) and ends the loop when found.
Notes
Normally, the number of command-line arguments is limited to nine (
%1
through%9
), butshift
makes it possible for a batch file to accommodate more.If you use a wildcard on the command line, it is passed to the batch file as a single argument (e.g., *.*), which can be used as is. It isn’t expanded first, as with Unix shells, so you can’t pick it apart in the script and use each matched filename separately.
Get Windows XP in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.