Creating a Windows batch file that accepts parameters

大兔子大兔子 提交于 2019-12-11 15:11:39

问题


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!


回答1:


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



来源:https://stackoverflow.com/questions/21582886/creating-a-windows-batch-file-that-accepts-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!