Search code examples
windowsbatch-filecommand-line-arguments

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


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.


Solution

  • Use the shift command if you want to work with more than 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