user2996905
user2996905

Reputation: 125

Passing argument in batch file

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.

Upvotes: 3

Views: 295

Answers (3)

Randy Rakestraw
Randy Rakestraw

Reputation: 329

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%
)

Upvotes: 0

Magoo
Magoo

Reputation: 80203

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.

Upvotes: 2

DTH
DTH

Reputation: 1133

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

Upvotes: 0

Related Questions