How do you utilize more than 9 arguments when calling a label in a CMD batch-script?

28,293

Solution 1

Use the shift command if you want to work with more 9 parameters.
(actually more than 10 parameters if you count the %0 parameter)

You can [...] use the shift command to create a batch file that can accept more than 10 batch parameters. If you specify more than 10 parameters on the command line, those that appear after the tenth (%9) will be shifted one at a time into %9.

You can either use a loop, store the variables before shifting, or do it quick like this:

@echo off
CALL:LABEL "one" "two" "three" "four" "five" "six" "seven" "eight" "nine" "ten" "eleven" "twelve"
PAUSE
GOTO:EOF

:LABEL
:: print arguments 1-9
echo %1
echo %2
echo %3
echo %4
echo %5
echo %6
echo %7
echo %8
echo %9

:: print arguments 10-11
shift
shift 
echo %8
echo %9

:: print argument 13
shift
echo %9

You can replace the shift commands with a loop in case you have many arguments. The following for loop executes shift nine times, so that %1 will be the tenth argument.

@for /L %%i in (0,1,8) do shift

Solution 2

Assuming we're using a recent-ish version of CMD here, I'm pretty shocked to find no one has posted the following, which allows an arbitrary number of arguments to be processed easily without ever using the clunky shift command:

rem test.bat
call :subr %*
exit /b

:subr
for %%A in (%*) do (
    echo %%A
)
exit /b

You can also do this same technique right in "main" as well.

This way, you don't eat up your arguments as you process them, and there is no need for shift, meaning you can consistently loop through the argument-list more than once if you have complicated calling options and it happens to make your script's logic simpler.

Obviously, if do loop through more than once, it increases computational complexity in exchange for legibility, but neither Bash nor CMD were really built for great speed (as far as I've ever heard) so there's little point in trying to optimize by doing setup all-in-one-go assuming n is any less than 100 or so.

Some sample output:

C:\tmp>test.bat one two three four five six seven eight nine ten eleven
one
two
three
four
five
six
seven
eight
nine
ten
eleven

edit - On the off chance anyone is processing arguments against another list and part of the work is nested, it's important that an intermediary CALL be used to allow the current argument being processed to be transferred into the inner loop; simply doing set intermediary=%%OUTER appears to simply set intermediary to a an empty string, so it's next simplest to do this:

setlocal enabledelayedexpansion
rem ^ in my experience, it's always a good idea to set this if for-loops are involved
for /f %%L in (file.txt) do (
    call :inner %%L
)
exit /b

:inner
for %%A in (%*) do (
    if %%A EQU %~1 call dosomething.bat %~1        
)
exit /b

edit 2 - Also, if for whatever reason you want to add the shift approach into the mix here – perhaps by trying to pass all arguments other than the 1st to a subroutine by using a shift and then using call :subroutine %* – note that it won't work because %* actually gives you all of the original arguments in order, ignorant of any shifting you have done.
It's a shame because there's no native syntax (that I know of) that can group all arguments after a certain one, as one might expect say %3* to do.

Solution 3

This is another way to use the shift command.

Note in this case you can use a variable number of parameters.

@ECHO OFF
CALL:LABEL "one" "two" "three" "four" "five" "six" "seven" "eight" "nine" "ten" "eleven" "twelve"
PAUSE
GOTO:EOF

:LABEL
echo %1
shift
if not [%1]==[] GOTO LABEL

Solution 4

Just throwing my hat in the ring if it helps anyone

Set a=%1 & Set b=%2 & Set c=%3 & Set d=%4 & Set e=%5 & Set f=%6 & Set g=%7 & Set h=%8 & Set i=%9
Shift & Shift & Shift & Shift & Shift & Shift & Shift & Shift & Shift
Set j=%1 & Set k=%2 & Set l=%3 & Set m=%4 & Set n=%5 & Set o=%6 & Set p=%7 & Set q=%8 & Set r=%9
Shift & Shift & Shift & Shift & Shift & Shift & Shift & Shift & Shift
Set s=%1 & Set t=%2 & Set u=%3 & Set v=%4 & Set w=%5 & Set x=%6 & Set y=%7 & Set z=%8

I just put this at the top of the .bat or subroutine that will use >9 args, then you can use %a% through %z% instead of %1 to the non-existent %26.

It's pretty ugly, originally I had it all one one line à la

Set a=%1 & Shift & Set b=%1 & Shift &...

That way it's only on one line and gets hidden behind the edge of the editor pretty swiftly, but apparently Shift won't take effect until the next true line; all the variables were set to the first parameter.

If you're wondering why do this instead of the loops, unless someone shows me you can create a sort of map or array, I needed to use the variables in a one-line command, not echo (or do whatever) with them one at a time:

::example; concatenate a bunch of files
Set output=%1
Shift
<insert above behemoth>
type %a% %b% %c% %d% %e% %f% 2>NUL > %output%

Damn this was so much easier in bash

cat ${@:2} > "$1"

Solution 5

You cannot have more than 10 (0 through 9) accessible arguments (%0 being the batchfile itself) in a batch file. However, using the shift command will allow you to "left-shift" your arguments, and access those arguments beyond the 9th one. If you do it three times, you should end up with %7, %8 and %9 containing "ten", "eleven" and "twelve".

Share:
28,293
Anthony Miller
Author by

Anthony Miller

"Your competition is a hungry immigrant with a digital handheld assistant. America is made up of immigrants... if your children are to be successful they must act like immigrants in their own country. Just by being born here doesn't give you the ability to be successful... it is the work ethic... the pioneering ethic... the service ethic that will win. Your competition is always a hungry immigrant with a digital assistant: hungry in the belly for food, hungry in the mind for knowledge, and the hunger is something that should never leave you." ~Dr. Dennis Waitley

Updated on March 30, 2020

Comments

  • Anthony Miller
    Anthony Miller about 4 years

    I would like to know how to call more than 9 argument within a batch script when calling a label. For example, the following shows that I have 12 arguments assigned along with attempting to echo all of them.

    CALL:LABEL "one" "two" "three" "four" "five" "six" "seven" "eight" "nine" "ten" "eleven" "twelve"
    PAUSE
    GOTO:EOF
    
    
    :LABEL
    echo %1
    echo %2
    echo %3
    echo %4
    echo %5
    echo %6
    echo %7
    echo %8
    echo %9
    echo %10
    echo %11
    echo %12
    

    The output for %10 %11 and %12 ends up being one0 one1 one2. I've tried using curly brackets, brackets, quotations, single quotes around the numbers without any luck.