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.

2
  • This is possible, take a look at this article. But, implementing the same in some scripting language is much easier like vbs Commented Dec 30, 2014 at 10:04
  • 1
    Not exactly what you asked for, but you should take a look at stackoverflow.com/a/8162578/1012053 Commented Dec 30, 2014 at 21:12

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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..

1 Comment

You could exploid the parameters maybee by doing this: %0 "password" string to identify what is coming next %1 "<the actual password>" %2 "user" string to identify that the next param is the user name %3 "<the actual user name>"
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%
)

1 Comment

substring syntax only works with variables not with parameters like %1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.