July 21, 2011

HOWTO: Pass more than 10 arguments to a DOS batch file

Batch files can only handle parameters %0 to %9
%0 is the program name as it was called,
%1 is the first command line parameter,
%2 is the second command line parameter,
and so on till %9.

The batch file's limitation to handle parameters up to %9 only can be overcome by using SHIFT.

After NT 4, we do not need to use SHIFT...
FOR %%A IN (%*) DO (
      rem Now your batch file handles %%A instead of %1
)
%* will always expand to all original parameters, sadly. But you can use the following snippet of code to build a variable containing all but the first parameter:

:my_function
rem skip the first arag
shift
set args=%1
:my_loop
shift
if [%1]==[] goto my_end_loop
set args=%args% %1
goto :my_loop
:my_end_loop
rem Do somthing with the args 
goto :EOF

You can call this inside your batch file:
call :my_function %*

No comments: