Saturday, December 24, 2011

Passing more than 10 arugumets to Windows Batch script

Last week came across an interesting scenario while working a batch script with my friend.
That is, we need to pass more than 10 arguments to a batch script. There will be no problem to pass 9 arguments. I will just try to explain the normal way we use to get up to 9 arguments and way to get more than 10 arguments.

Up to 9 arguments

Consider a script test.bat being called with arguments, a b c  as given below.

c:\> test.bat a b c
Inside test.bat we will getting the arguments as given below.
@echo off
           set arg_1=%1
set arg_2=%2
set arg_3=%3
For more than 10 arguments

We can not use %10 to get the 10th argument. Well, here is the solution we have found.

---------------------------------------------------------------------------------------
@echo ON

setlocal EnableDelayedExpansion

set arg_1=
set arg_2=
set arg_3=
set arg_4=
set arg_5=
set arg_6=
set arg_7=
set arg_8=
set arg_9=
set arg_10=
 //Tricky part
set /a m=0
FOR  %%i IN (%*) DO ( 
   set /a m+=1 
set arg_!m!=%%i
)

---------------------------------------------------------------------------------------

-Krishna








No comments:

Post a Comment