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?
Passing Variable Length Arguments into Batch File
Moderator: DosItHelp
-
- Posts: 7
- Joined: 06 Nov 2008 19:55
-
- Posts: 7
- Joined: 06 Nov 2008 19:55
ANSWER: BATCH files with Variable Length Parameters
If calling FOO v1 v2 v3 ..., and wish to gather all the parameters:
If calling FOO v1 v2 v3 ..., and wish to gather all the parameters after the second parameter:
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