Computer Hope
Microsoft => Microsoft DOS => Topic started by: campione on August 27, 2008, 11:28:46 AM
-
I know I can take parameters at command line like:
file.bat param1 param2 etc
and have them read in file.bat like:
%1 %2 etc
but is there a way to pass parameters using options, i.e.:
file.bat -pOne param1 -pTwo -param2 etc
and have them read in file.bat, i.e. get value of pOne and pTwo?
Any help appreciated
-
if your asking what i think then you can just add
set Pone=%1%
which will then set your first parameter to "Pone".
on second thoughts that doesn't seem to be what your asking but if it is hope it works. If i get you right this time you want to pass a parameter to the batch file that tells the batch file to get a value?
this can be done but it depends where this value is in the first place.
FB
-
fireballs, you had one % sign too many. To set Pone to the value of the first parameter you would use
set Pone=%1
-
No, what I'm thinking of is a way of kinda setting variables as they are passed, for example:
file.bat -user myUserName -password myPassword
would allow me to access the -user value that was passed, i.e. userName and the -password, i.e. myPassword.
I suppose the whole point of the exercise is to be able to swap around the options, such as:
file.bat -password myPassword -user myUserName
but that this command would still mean the same as the previous command.
-
Actually, just thinking there, I could check each parameter, e.g.:
while (input parameters)(
if %1 == -user
(
do something with %2
)
if %1 == -password
(
do something with %2
)
check that %3 is -password if %1 -user
OR
check that %3 is -user if %1 -password
do something with %4
)
Excuse the pseudocode.
Not as clean as I would have liked but still should do the job.
-
fireballs, you had on % sign too many. To set Pone to the value of the first parameter you would use
set Pone=%1
It works either way for me.
you could have:
if %%1=="-password" goto password
if %%1="-username" goto username
exit
:username
if not %%2=="My Username" (exit) else (
if not %%4=="My Password" exit
)
goto code
:password
if not %%2=="My Password" (exit) else (
if not %%4=="My Username" exit
)
goto code
is that what your looking for?
EDIT: i think we had the same idea, you posted as i was writing a response.
FB
-
That's the one, thought there might have been an actual command.
Thanks.
-
While I was posting a reply, I saw the red "While you were typing a new reply has been posted" warning.
If you think about it logically, you only have 2 possibilities for the parameter sequence
(a) %1 -user %2 username %3 -password %4 password
(b) %1 -password %2 password %3 -user %4 username
So if you look at %1 you will find out all you need to know
if "%1"=="-user" (
set username=%2
set password=%4
) else (
set password=%2
set username=%4
)
-
I suppose my thinking was that using %1 - %9 can be limiting, mainly due to the amount of parameters that can be passed.
-
You can pass any number of parameters, you aren't limited to 9, as you will see if you study the SHIFT command.
Also, the %* parameter expands to a string made up of ***all*** of the parameters passed.
-
I see, so the SHIFT command will keep shifting through parameters, so I could have a large amount of them.
Nice one.