Change the position of command line arguments passed to a batch file.

      SHIFT [/n]

   /n    Start at the nth argument, where n is between zero and eight. 

You can get the value of any argument using a % followed by it's numerical position on the command line. The first item passed is always %1 the second item is always %2 and so on.

Only parameters %1 to %9 can be referenced by number, but it is possibly to pass more than 9 arguments. The SHIFT command provides a way of accessing these additional argument values.

In addition to allowing a large number of parameters to be read, the SHIFT command is also useful for manipulating optional parameters which may or may not be present. See this StackOverflow answer for an example.

SHIFT will not affect the value of %* which holds all the original arguments %1 %2 %3...

The argument %0 will initially refer to the path that was used to execute the batch, e.g. MyBatch.cmd if in the current directory or a full path like C:\apps\myBatch.cmd

The SHIFT command will not work within parentheses/brackets, so place all your command line arguments in variables before running any FOR commands or other bracketed expressions. Or use the CALL syntax as explained in this forum thread.

Drag and Drop

If you drag and drop one or more files or folders onto a batch file, the batch will be executed and the filenames (or folder names) will be passed to the batch file as arguments:
%1 = "first file", %2 = "second file" etc
Like dragging a file into the CMD window, quotes are automatically added to long names.

Warning: SHIFT can change the value of %0

If SHIFT is used to move a text argument into %0 then any references to %0 will refer instead to the current working directory, unless the new argument value happens to contain a valid path.

For example:
%0\..\MyExecutable.exe will run MyExecutable.exe from the same directory as the Batch file.

If the following argument is passed to myBatch.cmd:
myBatch.cmd D:\utils\

Then SHIFT followed by the same command as before will now run MyExecutable.exe from the directory D:\utils\

To avoid this behaviour use SHIFT /1


If the command line arguments are successfully reassigned %ERRORLEVEL% = unchanged, typically this will be 0 but if a previous command set an errorlevel, that will be preserved (this is a bug).

If a bad switch is given %ERRORLEVEL% = 1


Demobatch.cmd The Quick Brown

As given %1=The, %2=Quick, %3=Brown
will result in %1=Quick, %2=Brown
A second
will result in %1=Brown

Given %1=the, %2=quick, %3=brown, %4=fox
will result in %1=the, %2=brown, %3=fox

SHIFT is an internal command. If Command Extensions are disabled, the SHIFT command will not support the /n switch.

“A small key opens big doors" ~ Turkish Proverb

Related commands

CALL - Call one batch program from another.
SET - Display or edit environment variables.
powershell: param( $var1, $var2,... )
Equivalent bash command (Linux): shift - Shift positional arguments.

Copyright © 1999-2024
Some rights reserved