Reputation: 117
I want to create a Windows batch file that accepts parameters and uses it inside. For instance, when I type "sample.bat -username admin -password pass123" in the command line, it will store "admin" and "pass123" in the variables inside sample.bat
This is how I do it in Linux:
USERNAME=
PASSWORD=
while [ $# -ne 0 ]
do
case $1 in
-username*)
USERNAME=$2
;;
-password*)
PASSWORD=$2
;;
*)
;;
esac
shift 1
done
I'm not used to making scripts in Windows but I need to do a Windows counterpart for this one. Kindly help me. Thank you so much!
Upvotes: 1
Views: 9303
Reputation: 56228
@echo off
set user=
set pass=
:loop
if "%1" == "" goto done
if /i "%1" == "-username" set "user=%2"
if /i "%1" == "-password" set "pass=%2"
shift
goto :loop
:done
echo Username: %user%
echo Passoword: %pass%
Note: don't use %username% as a variablename, because it's a systemvariable.
Upvotes: 3