Here is a solution with FOR
loop extendable to almost any number of acceptable user's reply:
rem remove variable _match
set "_match="
set /p FILEINPUT="1, 2 or 3?"
rem check whether user's reply is in (1 2) set
for %%G in (1 2) do if "%FILEINPUT%"=="%%~G" set "_match=%%~G"
if defined _match (
echo do this command: "if" branch
) else (
echo do this command: "else" branch
)
The link provided says that "FOR
conditionally perform a command on several files", but in fact the FOR
command treats strings, regardless those strings denote files or don't.
Extension:
- more replies acceptable,
- more than 1 word in an acceptable reply:
Script:
:again
set "FILEINPUT="
rem remove variable _match
set "_match="
set /p "FILEINPUT=one of main menu items at stackoverflow.com: "
rem format user's reply
rem two-words maximally: remove redundant white spaces
for /F "tokens=1,2*" %%G in ("%FILEINPUT%") Do (
rem echo [%%~G] [%%~H] [%%~I]
if "%%~I"=="" if "%%~H"=="" (
set "FILEINPUT=%%~G"
) else (
set "FILEINPUT=%%~G %%~H"
)
)
rem check whether user's reply is right
for %%G in (
Questions Tags Users Badges Unanswered "Ask Question"
) do (
if /I "%FILEINPUT%"=="%%~G" set "_match=%%~G"
)
if defined _match (
echo "if" branch [%FILEINPUT%]==[%_match%]
goto :next
) else (
echo "else" branch [%FILEINPUT%]
if not "%FILEINPUT%"=="" goto :again
)
:next
Output:
==>28886458.bat
one of main menu items at stackoverflow.com: ask questiO N
"else" branch [ ask questiO N ]
one of main menu items at stackoverflow.com: ask questiON
"if" branch [ask questiON]==[Ask Question]
==>28886458.bat
one of main menu items at stackoverflow.com: TAGGS
"else" branch [TAGGS]
one of main menu items at stackoverflow.com: TAGS
"if" branch [TAGS]==[Tags]