The command: "SHIFT" is on Windows 12, 11, 10, .. , MS Server 2025, 2022, 2019, .. available
The examples for the command "SHIFT"
The
`SHIFT` command in Windows Command Prompt is used to move the position of parameters in a batch file. Here are some examples of using the
`SHIFT` command:
Example 1: Iterate through parameters in a batch file:
@ECHO OFF
:LOOP
IF "%1"=="" GOTO END
ECHO parameters: %1
SHIFT
GOTO LOOP
:END
Description: This example uses a loop to iterate through all parameters in a batch file and display them one by one. The
`SHIFT` command is used to shift the position of the parameters.
Example 2: Sum parameters in a batch file:
@ECHO OFF
SET SUM=0
:ADD_LOOP
IF "%1"=="" GOTO END_ADD
SET /A SUM+= %1
SHIFT
GOTO ADD_LOOP
:END_ADD
ECHO sum: %SUM%
Description: In this example the parameters are summed in a loop. The
`SHIFT` command moves the parameter position, and
`SET /A` is used to update the sum.
Example 3: Working with nested loops and SHIFT:
@ECHO OFF
:OUTER_LOOP
IF "%1"=="" GOTO END_OUTER
ECHO Outer Parameters: %1
SHIFT
:INNER_LOOP
IF "%1"=="" GOTO END_INNER
ECHO Inner Parameter: %1
SHIFT
GOTO INNER_LOOP
:END_INNER
GOTO OUTER_LOOP
:END_OUTER
Description: This example shows how
`SHIFT` can be used in nested loops. The outer loop processes outer parameters while the inner loop processes the inner parameters.
Example 4: Skipping parameters with SHIFT:
@ECHO OFF
SHIFT
SHIFT
ECHO Third parameter: %1
Description: Here the first two parameters are skipped with two consecutive
`SHIFT` commands and the third parameter is displayed.
Example 5: Check and handle parameters:
@ECHO OFF
:PARAM_LOOP
IF "%1"=="" GOTO END_PARAM
IF /I "%1"=="/DEBUG" (
ECHO debug mode activated.
SHIFT
GOTO PARAM_LOOP
)
ECHO Process parameters: %1
SHIFT
GOTO PARAM_LOOP
:END_PARAM
Description: This example checks whether the first parameter is
"/DEBUG". If this is the case, a message is issued and the parameter is skipped.
The
`SHIFT` command is particularly useful in batch scripts where a variable number of parameters need to be processed. Note that
`SHIFT` moves the contents of the parameter list and reassigns parameter positions. Therefore, it is important to use
`SHIFT` wisely and according to the logic of your batch script.
"SHIFT" Excerpt from Microsoft Windows Help
Microsoft Windows [Version 10.0.19045.3693]
(c) Copyright 1985-2023 Microsoft Corp.
C:\\WINDOWS>
Changes the position of replaceable parameters in a batch file.
SHIFT [/n]
If Command Extensions are enabled the SHIFT command supports
the /n switch which tells the command to start shifting at the
nth argument, where n may be between zero and eight. For example:
SHIFT /2
would shift %3 to %2, %4 to %3, etc. and leave %0 and %1 unaffected.
Important information, tips for the "SHIFT" command
There are a few important aspects to consider when using the
`SHIFT` command in batch scripts:
1.
Parameter loss: When you call
`SHIFT`, this shifts the position of the parameters to the left in the parameter list. This means that the value of the first parameter (
`%1`) is replaced by that of the second parameter (
`%2`), the second parameter becomes the third (
`%3`), and so on. The value of the last parameter (
`%9`) is lost and the value of
`%0` (the script name) remains unchanged.
2.
Number of shifts: You can only call
`SHIFT` nine times in a batch script because there are only nine numeric parameters (
`%0` to
`%9`). After the ninth shift, the value of the ninth parameter remains lost.
3.
SHIFT in loops: When using
`SHIFT` in a loop, you should ensure that the conditions are checked properly to avoid an infinite loop if there are no other parameters.
@ECHO OFF
:LOOP
IF "%1"=="" GOTO END_LOOP
ECHO parameters: %1
SHIFT
GOTO LOOP
:END_LOOP
4.
SHIFT in nested loops: When using
`SHIFT` in nested loops, note that moving the parameters in a loop also affects the parameter positions in the nested loops.
@ECHO OFF
:OUTER_LOOP
IF "%1"=="" GOTO END_OUTER
ECHO Outer Parameters: %1
SHIFT
:INNER_LOOP
IF "%1"=="" GOTO END_INNER
ECHO Inner Parameter: %1
SHIFT
GOTO INNER_LOOP
:END_INNER
GOTO OUTER_LOOP
:END_OUTER
5.
Delayed expansion and SHIFT: If you use
`SHIFT` in combination with delayed expansion (
`ENABLEDELAYEDEXPANSION`), note that
`!` is used for delayed expansion, not
`%`.
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET COUNT=0
:LOOP
IF "!1"=="" GOTO END_LOOP
SET /A COUNT+=1
SHIFT
GOTO LOOP
:END_LOOP
ECHO Number of parameters: %COUNT%
It is important to carefully plan the order and frequency of
`SHIFT` to ensure that the parameters are shifted as expected and no information is lost. Checking for empty parameters (
`IF "%1"==""`) is also important to avoid infinite loops.