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.
There is no practical limit to the number of parameters you can pass to a batch file, but you can only address parameter 0 (%0 - The batch file name) through parameter 9 (%9).
Command line syntax Commands provide the action, or list of actions separated by semicolons, that should be implemented. If no command is specified, then the command is assumed to be new-tab by default. To display a help message listing the available command line arguments, enter: wt -h , wt --help , wt -? , or wt /? .
A command line argument is simply anything we enter after the executable name, which in the above example is notepad.exe. So for example, if we launched Notepad using the command C:\Windows\System32\notepad.exe /s, then /s would be the command line argument we used.
Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With