3

Have seen passing argument to a batch file something like

filename.bat argument1 argument2 ..

But i want to pass something like filename.bat username=argument1 password=argument2

As i dont want to depend on any order , user can pass password first and then username.

user2996905
  • 125
  • 1
  • 6
  • This is possible, take a look at this [article](http://www.codeproject.com/Questions/176497/DOS-Batch-how-to-parse-command-line-in-DOS-batch-f). But, implementing the same in some scripting language is much easier like vbs – Thangadurai Dec 30 '14 at 10:04
  • 1
    Not exactly what you asked for, but you should take a look at http://stackoverflow.com/a/8162578/1012053 – dbenham Dec 30 '14 at 21:12

3 Answers3

2

Look here : processing switches

Although this is oriented toward using the format /username argument1 it's relatively easy to adapt to username=argument1 but there is a problem with = when passed within "a" parameter - it's seen as a separator, so the receiving routine would see two parameters, but they'd be paired (username and argument1.)

Really depends on quite how you want to process the data. You can, if you so desire, pass the parameter "quoted" to get over the = is a separator problem, then use

 for /f "tokens=1,*delims==" %%a in ("%~1") do set "%%a=%%b"

but remembering to use the quoting may be a stumbling block.

Note: using the procedure I've pointed to is not restricted by parameter-count.

Community
  • 1
  • 1
Magoo
  • 77,302
  • 8
  • 62
  • 84
0

I dont think you are able to pass parameters in a random order, as they are not identified by a parameter name, but by %0 - %9

see http://www.robvanderwoude.com/parameters.php

As mentioned here, you can use up tp 9 parameters when calling a batch file..

DTH
  • 1,133
  • 3
  • 16
  • 36
  • You could exploid the parameters maybee by doing this: %0 "password" string to identify what is coming next %1 "" %2 "user" string to identify that the next param is the user name %3 "" – DTH Dec 30 '14 at 10:05
0

You could achieve this by using a variable substring since username= and password= are both 9 character long.

For example

set temp=%0
set temp=%temp:~0,9% 
if %temp%=="username=" (
  set tmpUser=%0
  set username=%tmpUser:~9%
  set tmpPass=%1
  set password=%tmpPass:~9%
)
if %temp%=="password=" (
  set tmpPass=%0
  set password=%tmpPass:~9%
  set tmpUser=%1
  set username=%tmpUser:~9%
)
Randy Rakestraw
  • 331
  • 1
  • 10