Computer and Software Support


Shift

This page is from Microsoft

Updated: January 21, 2005

Changes the position of batch parameters in a batch file.
 
 
Syntax
shift
Parameters
none
Remarks
  • Using the shift command-line option with command extensions. When command extensions are enabled (that is, the default), the shift command supports the /n command-line option, which tells the command to start shifting at the Nth argument, where N can be a value from zero to eight. For example, SHIFT /2 would shift %3 to %2, %4 to %3, and so on, and leave %0 and %1 unaffected.
  • How the shift command works. The shift command 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. This is useful for writing a batch file that performs the same operation on any number of parameters.
  • Working with more than 10 batch parameters. You can also use the shift command to create a batch file that can accept more than 10 batch parameters. If you specify more than 10 parameters on the command line, those that appear after the tenth (%9) will be shifted one at a time into %9.
  • Using %* with shift. Shift has no affect on the %* batch parameter.
  • Shifting parameters back. There is no backward shift command. After you carry out the shift command, you cannot recover the first batch parameter (%0) that existed before the shift.
Examples
The following batch file, Mycopy.bat, shows how to use shift with any number of batch parameters. It copies a list of files to a specific directory. The batch parameters are represented by the directory and file name arguments.
@echo off 
rem MYCOPY.BAT copies any number of files
rem to a directory.
rem The command uses the following syntax:
rem mycopy dir file1 file2 ... 
set todir=%1
:getfile
shift
if "%1"=="" goto end
copy %1 %todir%
goto getfile
:end
set todir=
echo All done

Formatting legend


 
Format Meaning
Italic Information that the user must supply
Bold Elements that the user must type exactly as shown
Ellipsis (...) Parameter that can be repeated several times in a command line
Between brackets ([]) Optional items
Between braces ({}); choices separated by pipe (|). Example: {even|odd} Set of choices from which the user must choose only one
Courier font Code or program output