Using the SHIFT Statement

by Barry Dysert
(last updated August 18, 2014)

4

You may find the SHIFT statement useful in some of your batch files. What SHIFT does is shift the parameters passed to the batch file from right to left. This gives you one way, for example, to have your batch file accept a variable number of parameters.

Internally, the command interpreter numbers elements of the command line. For instance, consider the following command that executes a batch file name DOIT.BAT:

doit first second third fourth fifth

In this instance, the command provides five parameters to the DOIT.BAT file. The command interpreter, internally, numbers these parameters %1 through %5. In reality, though, there is a "parameter" numbered %0: the batch file name itself. Create your own DOIT.BAT file that contains the following:

@ECHO OFF
CLS
:TOP
IF [%0]==[] GOTO :EOF
ECHO %0 %1 %2 %3 %4 %5
SHIFT
GOTO TOP

All the batch file does is to echo, to the screen, the elements of the command line and then shifts (through the SHIFT command) all the elements to the left. After the shift, element %0 is discarded, element %1 becomes %0, element %2 becomes %1, and so on. When you run the batch file, you should see the following output:

doit first second third fourth fifth
first second third fourth fifth
second third fourth fifth
third fourth fifth
fourth fifth
fifth

When you use SHIFT in your batch file, you can invoke the file with a variable number of parameters and then shift through them to do your processing.

 This tip (13198) applies to Windows 7 and 8.

Author Bio

Barry Dysert

Barry has been a computer professional for over 30 years, working in different positions such as technical team leader, project manager, and software developer.  He is currently a senior software engineer with an emphasis on developing custom applications under Microsoft Windows. ...

MORE FROM BARRY

Copying Files to a CD or DVD

You can have virtually unlimited disk space if you're willing to swap discs once in a while. By copying files to a CD or DVD ...

Discover More

Moving the Videos Library

The Videos folder is one of several system libraries created in Windows by default. This library is specifically optimized to ...

Discover More

Using Powercfg to Change the Active Power Scheme

You can easily change which power scheme is active by using the SetActive switch. This tip shows you how.

Discover More
More WindowsTips

Using Batch Files, Part 2

This tip is part of a series that talks about Windows batch files. It introduces a few more commands and special characters ...

Discover More

Running a Batch File at a Scheduled Time

Once you become comfortable with batch files, chances are that you'll want to use them to perform various system maintenance ...

Discover More

Using Batch Files, Part 4: The CALL Statement

This tip is part of a series that talks about Windows batch files. It introduces a few more commands you can use in your ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WindowsTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 7 + 5?

2015-12-07 08:10:09

Thomas Redd

Thanks for the help you give! I am so excited about what I can now do with Batch Files!


2014-08-23 08:56:44

Barry

Sorry I wasn't as coherent as I should have been. Let me try simplifying it.

The SHIFT command causes the batch procedure to shift the command-line parameters one position to the left. So if you invoked a batch procedure called Barry.bat and specified parameters "one two three", i.e., you'd type (without quotes) "Barry.bat one two three", then inside Barry.bat the symbol %1 would take on the value of "one", the symbol %2 would take on the value of "two", and the symbol %3 would take on the value of "three". Each time the procedure does a SHIFT, the command-line parameters are shifted to the left. So after one SHIFT, the symbol %1 would take on the value of "two", and the symbol %2 would take on the value of "three". After another SHIFT, %1 would be "three"; and after another SHIFT, %1 would be empty.

Here's Barry.bat...
@echo off
echo %1 %2 %3
shift
echo %1 %2
shift
echo %1
shift
echo %1

Here's the output when I invoke it with C:Bat>Barry.bat one two three
one two three
two three
three
ECHO is off.

I hope this makes more sense.


2014-08-22 17:02:35

Jibran Ahmed

Although, I appreciate your effort here Barry but indeed, I must have to agree with Angela that your article is far too technical to understand. Kindly, if it can be made a little easy for us the beginners? Thank you!


2014-08-18 19:10:29

Angela

Sorry but I didn't understand a word of this. far too technical.


Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in WindowsTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.