Change the position of command line parameters in a batch file.

      SHIFT [/n]

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


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

Parse Command Line Arguments

if "%1"=="" (goto :main)
:: Do whatever with token %1
Echo [%1]
:: Shift %2 into %1
goto :start


The parameter %0 will initially refer to the path that was used to execute the batch - this could be MyBatch.cmd if in the current directory or a full path like C:\apps\myBatch.cmd

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

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

If the following parameter is passed to myBatch.cmd

myBatch.cmd D:\utils\

Then the following commands in myBatch will run MyExecutable.exe from the directory D:\utils\


The SHIFT command will not work within brackets, so place all your command line arguments in variables before running any FOR commands or other bracketed expressions. (Alternatively use call syntax as explained in this forum thread.)

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


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

© Copyright 1999-2013
Some rights reserved