具有预定义变量的用户输入 - 批处理脚本

User Input with predefined variable - batch scripting

我正在尝试批量制作一个脚本,用户将在其中输入他们的姓名,然后必须再次输入。但是,我希望用户能够输入 "my name is [name]" 并将其也视为 "correct"。

目前我的代码在有用户输入时无法识别 %name% 变量,而是转到 :incorrect 标签。

代码:

@ CLS
@ set /P name="Type your name: "
@ set /P userinput="Type your name again: "
@ if /I "userinput"=="%name%" goto correct
@ if /I "userinput"=="my name is %name%" goto correct
@ goto :incorrect
@ :correct
@ CLS & echo Correct! & pause
@ :incorrect
@ CLS & echo Incorrect! & pause 

但是,我不介意考虑它 "correct" 只要用户在他们的输入中某处输入他们的名字,因此我认为它需要以某种方式解析 ? 输入以查找变量 %name% 是否存在于其中。

在 if 语句中测试时,您没有将 userinput 换行在 %% 中,因此程序将其解释为纯文本而不是变量。尝试:

@ CLS
@ set /P name="Type your name: "
@ set /P userinput="Type your name again: "
@ if /I "%userinput%"=="%name%" goto correct
@ if /I "%userinput%"=="my name is %name%" goto correct
@ goto :incorrect
@ :correct
@ CLS & echo Correct! & pause
@ :incorrect
@ CLS & echo Incorrect! & pause