Passing Variable Length Arguments into Batch File

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jeff00seattle
Posts: 7
Joined: 06 Nov 2008 19:55

Passing Variable Length Arguments into Batch File

#1 Post by jeff00seattle » 06 Nov 2008 20:05

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?

jeff00seattle
Posts: 7
Joined: 06 Nov 2008 19:55

ANSWER: BATCH files with Variable Length Parameters

#2 Post by jeff00seattle » 08 Nov 2008 12:38

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

Post Reply