创建一个接受参数的Windows批处理文件 [英] Creating a Windows batch file that accepts parameters
问题描述
我想创建一个Windows批处理文件,该文件接受参数并在内部使用它.例如,当我在命令行中键入"sample.bat-用户名admin -password pass123"时,它将在sample.bat内部的变量中存储"admin"和"pass123"
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
这是我在Linux中执行的操作:
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
我不习惯在Windows中编写脚本,但是我需要为此编写Windows脚本.请帮助我.非常感谢!
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!
推荐答案
@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%
注意:请勿使用%username%作为变量名,因为它是系统变量.
Note: don't use %username% as a variablename, because it's a systemvariable.
这篇关于创建一个接受参数的Windows批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!