通过批处理接受基于通配符变量的用户输入

时间:2015-12-31 07:33:07

标签: batch-file compare user-input

我不知道怎么说出这个问题让你们明白,但请耐心等待。以下是我的代码:

set var1=yes no ok get out of here exit quit

echo Do you want to start?

set /p options=Your Input:

if "%Options%" (This is what I need help with)

将用户输入与var1进行比较,如果为true,则转到nextquestion 即使用户输入' Go start',它也会匹配Go或Start to var1。由于Go不在列表中,它会尝试将Start与var1匹配。如果找到,请继续下一个问题。希望有意义

2 个答案:

答案 0 :(得分:1)

for %%a in (%options%) do for %%b in (%var1%) do if /i "%%a"=="%%b" goto nextq
:not_found
response "%options%" is not implemented
pause

代码证明:

c:\106x>q34542866
Do you want to start?
Your Input:go
User input was "go"
response "go" is not implemented

c:\106x>q34542866
Do you want to start?
Your Input:no
User input was "no"
response "no" found

c:\106x>q34542866
Do you want to start?
Your Input:exit
User input was "exit"
response "exit" found

c:\106x>q34542866
Do you want to start?
Your Input:go start
User input was "go start"
response "go start" is not implemented

c:\106x>q34542866
Do you want to start?
Your Input:I do want to start, yes please
User input was "I do want to start, yes please"
response "I do want to start, yes please" found

代码:

set var1=yes no ok get out of here exit quit
echo Do you want to start?
set /p options=Your Input:

ECHO User input was "%options%"

for %%a in (%options%) do for %%b in (%var1%) do if /i "%%a"=="%%b" goto nextq
:not_found
ECHO response "%options%" is not implemented

GOTO :EOF

:nextq
ECHO response "%options%" found

GOTO :EOF

答案 1 :(得分:0)

@echo off
setlocal EnableDelayedExpansion

set "var1= yes no ok get out of here exit quit "
echo Do you want to start?
set /p options=Your Input:

for %%a in (%options%) do if "!var1: %%a =!" neq "%var1%" goto nextquestion
echo Word not found...
goto :EOF

:nextquestion
echo Word found