5

I would like to use first 2 arguments passed to a batch file and then append everything else to the command executed in the batch file.

Is there an easy way to do that?

// batch file
command -Duser=%1 -Dpwd=%2 {? how to pass all remaining arguments on the command line}

The batch will be executed like this

batch.bat USR PWD -Dargument1=1 -Dargument2=2

Effectively, I would like the following to be executed

command -Duser=USR -Dpwd=PWD -Dargument1=1 -Dargument2=2
Leonid
  • 22,360
  • 25
  • 67
  • 91

3 Answers3

3

The for command should give you what you want:

for /f "tokens=1,2*" %%a in ("%*") do echo command -Duser=%%a -Dpwd=%%b %%c

From the help for for (for /?):


FOR /F "eol=; tokens=2,3* delims=, " %i in (myfile.txt) do @echo %i %j %k would parse each line in myfile.txt, ignoring lines that begin with a semicolon, passing the 2nd and 3rd token from each line to the for body, with tokens delimited by commas and/or spaces. Notice the for body statements reference %i to get the 2nd token, %j to get the 3rd token, and %k to get all remaining tokens after the 3rd. For file names that contain spaces, you need to quote the filenames with double quotes. In order to use double quotes in this manner, you also need to use the usebackq option, otherwise the double quotes will be interpreted as defining a literal string to parse.


So in my solution, %%a has the value of the first argument passed to the script, %%b the value of the second argument, and %%c the remaining arguments.

When I run batch.bat USR PWD -Dargument1=1 -Dargument2=2 from a command prompt I get:

command -Duser=USR -Dpwd=PWD -Dargument1=1 -Dargument2=2

Remove the echo after the do to call your command with the arguments.

Patrick Cuff
  • 28,540
  • 12
  • 67
  • 94
  • 1
    This works as long as neither of the first 2 arguments contain quoted spaces. But it will fail if either argument contains quoted spaces - probably not a problem with username or password, put definitely not acceptable as a general solution. – dbenham Sep 07 '12 at 14:00
  • @dbenham; you are correct, pass any kind of quoted strings and all bets are off. – Patrick Cuff Sep 07 '12 at 14:10
  • Actually quoted args are OK as long as the quotes are balanced and the args don't contain spaces or special characters like `&` `|` etc. Another potential problem - arguments can also be delimited by `,` `;` or `=` instead of space. – dbenham Sep 07 '12 at 15:20
2

Patrick Cuff's solution usually works, but it will fail if either of the first 2 arguments contain quoted spaces or special characters. It will also fail if the arguments are delimited with something other than spaces. The FOR /F delimiters could be expanded to include , ; and =, but then it will fail if the argument contains any of those quoted delimiters.

The following improvement supports quoted special characters like & and |, but the argument delimiters are still a problem.

@echo off
setlocal enableDelayedExpansion
set args=%*
for /f "tokens=1,2*" %%a in ("!args!") do (
  endlocal
  echo arg1=%%a  arg2=%%b  remainder=%%c
)

EDIT Here is a simpler modification using USEBACKQ that achieves the same thing

for /f "usebackq tokens=1,2*" %%a in ('%*') do echo arg1=%%a  arg2=%%b  remainder=%%c

End edit

Unfortunately a truly general solution requires a GOTO loop that parses every parameter from 3 on. The solution below should work as long as all quotes are balanced and all special characters are quoted.

@echo off
setlocal
set "args="
:parseArgs
set args=%args% %3
shift /3
if "%~3" neq "" goto :parseArgs
if (%3) equ ("") goto :parseArgs
echo arg1=%1  arg2=%2  remainder=%args%

To eliminate all limitations would require drastic measures - see How to receive even the strangest command line parameters?

Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390
1

This might be what you want

C:>help shift
Changes the position of replaceable parameters in a batch file.

SHIFT [/n]

If Command Extensions are enabled the SHIFT command supports
the /n switch which tells the command to start shifting at the
nth argument, where n may be between zero and eight.  For example:

    SHIFT /2

would shift %3 to %2, %4 to %3, etc. and leave %0 and %1 unaffected.
Andy Morris
  • 3,393
  • 1
  • 21
  • 20
  • But it will not help here, as SHIFT has only an effect for the parameters `%1`..`%9` but NOT for `%*`. So there isn't a simple way for getting all parameters without some prefixed parameters – jeb Sep 07 '12 at 12:44