我是批处理脚本的新手,需要弄清楚如何使用用户输入的值来创建变量。
@echo off
set /p userMonth = "Enter Month(mm): "
set /p userDay = "Enter Day (dd): "
set /p userYear = "Enter Year (yyyy): "
echo month: %userMonth%
echo day: %userDay%
echo year: %userYear%
set today= %userMonth%.%userDay%.%userYear%
set month= %userMonth%-%userYear%
运行脚本时,输出仅显示
month:
day:
year:
输入后(例如)
01
25
2018
答案 0 :(得分:0)
您的set命令格式错误。使用引号时,它用"
string=
data
"
代替。请记住,您也不想在string
和=
之间留空格。使用设置/?查找更多信息。
set /p "userMonth=Enter Month(mm): "
set /p "userDay=Enter Day (dd): "
set /p "userYear=Enter Year (yyyy): "
echo month: %userMonth%
echo day: %userDay%
echo year: %userYear%
此外,要减去或增加字符串,您需要使用set /a
命令。 / A开关指定等号右边的字符串
是要计算的数值表达式。
set "today=%userMonth%.%userDay%.%userYear%"
set /a "month=%userMonth%-%userYear%"
可以使用其他运算符:
+ Add set /a "_num=_num+5"
+= Add variable set /a "_num+=5"
- Subtract (or unary)set /a "_num=_num-5"
-= Subtract variable set /a "_num-=5"
* Multiply set /a "_num=_num*5"
*= Multiply variable set /a "_num*=5"
/ Divide set /a "_num=_num/5"
/= Divide variable set /a "_num/=5"