Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass additional extra arguments to a batch file?

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
like image 898
Leonid Avatar asked Sep 07 '12 11:09

Leonid


3 Answers

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.

like image 112
Patrick Cuff Avatar answered Nov 04 '22 23:11

Patrick Cuff


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?

like image 27
dbenham Avatar answered Nov 04 '22 23:11

dbenham


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.
like image 1
Andy Morris Avatar answered Nov 05 '22 00:11

Andy Morris