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

John Savill

June 2, 2003

1 Min Read
ITPro Today logo in a gray background | ITPro Today

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:nextif "%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:

testabcdefghijk

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.

About the Author

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like