How can I shift down by one all of a batch file's arguments?

A. You can use the "shift" command to move all the arguments in a batch file down by one. If the batch file calls this command once, then argument %1 would become the second argument instead of the first and argument %0 would become the first argument instead of the name of the program or batch file. For example, when I run test.bat

@rem test.bat
@echo off
:next
if "%0" == "" goto end
        echo %0
        shift
        goto next
:end

by typing

C:\>test a b c d e f g h i j k

the batch file will display the following output onscreen:

test
a
b
c
d
e
f
g
h
i
j
k

You can optionally add /n to the end of the shift command where n is the argument to start from. For example, if you used

shift /2

%3 would become %2, %4 would become %3, but %0 and %1 would be unchanged.

Obviously, you shouldn't use the /n switch in the above example because doing so will cause the list of parameters to never run out, thus causing a never ending loop.

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish