How to pass more than 9 parameters to batch file

Here's an even easier solution that assumes you merely want to pass the values to another program. This example allows passing up to 27 parameters to zip...

@echo OFF
set ZipArgs=%1 %2 %3 %4 %5 %6 %7 %8 %9
for /L %%i in (0,1,8) do @shift
set ZipArgs=%ZipArgs% %1 %2 %3 %4 %5 %6 %7 %8 %9
for /L %%i in (0,1,8) do @shift
set ZipArgs=%ZipArgs% %1 %2 %3 %4 %5 %6 %7 %8 %9
"C:\Program Files\WinZip\wzzip.exe" %ZipArgs%

The first line merely turns off echoing each line to the screen. The lines that start with set ZipArgs build the environment variable ZipArgs with the next 9 parameters from the command line. The "for" lines use the SHIFT command to discard the first 9 parameters bringing the next 9 "up front". The last line executes zip passing to it the assembled parameter values.

You could complicate things by adding code to check for the number of parameters on the command line and act appropriately, but this does the trick just fine for me.


save the first nine args in a variable. THEN call shift multiple times and only then use the rest:

set "v=http://example.com?firstName=%1&middleName=%2&lastName=%3&country=%4&address=%5&address2=%6&address3=%7&mobileNo=%8&landlineNo=%9"
shift
shift
shift
shift
shift
shift
shift
shift
shift
start iexplore %v%&emailAddress=%1&hobby1=%2&hobby2=%3&hobby3=%4&hobby4=%5&hobby5=%6

Here is a simple answer: shift is the key and then loop values and assign them into variables. setup counter to indicate which variable is each parameter.

Example gets alphabets into batch file and sets them into arg1, arg2 etc variables.

batchfile test.bat example:

@echo off

set /a counter=1
:ARGLBEG
if "%1"=="" goto ARGLEND
set arg%counter%=%1
goto ARGNEXT 

:ARGNEXT
shift
set /a counter=%counter%+1
goto ARGLBEG

:ARGLEND
echo "List values."
set arg

echo "Just one value, arg9."
echo %arg9%
goto end

:end

How to use test.bat and how results looks:

test.bat A B c d e F G H i J k L M N O P Q R S t u v x y z

Results.

"List values."
arg1=A
arg10=J
arg11=k
arg12=L
arg13=M
arg14=N
arg15=O
arg16=P
arg17=Q
arg18=R
arg19=S
arg2=B
arg20=t
arg21=u
arg22=v
arg23=x
arg24=y
arg25=z
arg3=c
arg4=d
arg5=e
arg6=F
arg7=G
arg8=H
arg9=i
"Just one value, arg9."
i

Tags:

Batch File