Page 1 of 1

Passing Variable Length Arguments into Batch File

Posted: 06 Nov 2008 20:05
by jeff00seattle
Within a batch file, I am using the function sqlcmd that can take variable number of argments. For example:

sqlcmd %database% %file% -v value1 -v value2 -v value3 ...

I want to pass into this batch file the values (value1, value2, value3, ...) that would be applied to sqlcmd function. In other words, something like:

Call foo value1 value2 value3 ...

What would be the best way to perform this action?

ANSWER: BATCH files with Variable Length Parameters

Posted: 08 Nov 2008 12:38
by jeff00seattle
If calling FOO v1 v2 v3 ..., and wish to gather all the parameters:

Code: Select all

SET PARAMS=
:SHIFT_LOOP
IF "%1"=="" GOTO SHIFT_OUT
   SET PARAMS=%PARAMS% %1
   SHIFT
GOTO SHIFT_LOOP
:SHIFT_OUT


If calling FOO v1 v2 v3 ..., and wish to gather all the parameters after the second parameter:

Code: Select all

SHIFT
SHIFT
SET PARAMS=
:SHIFT_LOOP
IF "%1"=="" GOTO SHIFT_OUT
   SET PARAMS=%PARAMS% %1
   SHIFT
GOTO SHIFT_LOOP
:SHIFT_OUT