0
set /p FILEINPUT="1, 2 or 3?"
if "%FILEINPUT%"==("1","2") ( do this command
) else ( do this command )

So in this example I want the program to check what the user input, if it is 1 or 2 it does the first command, and if it is something else it does the second command. What do I have to put in the if part to make it check multiple variables? As this didn't seem to work.

4 Answers4

0

Batch files don't have logical operators such as and or or, nor do they have if-else statements. However you can use nested if statements or an "or" variable to achieve the same effect. It just takes a little more work. Take a look at this post:

Logical operators ("and", "or") in DOS batch

Community
  • 1
  • 1
Mir
  • 305
  • 8
  • 24
0

There are several ways (or workaround, call it whatever you want) to do it, here is one:

set _ONEorTWO="12"
set /p FILEINPUT="1, 2 or 3?"

REM Keep only the first character.
set FILEINPUT=%FILEINPUT:~0,1%

CALL set _final=%%_ONEorTWO:%FILEINPUT%=%%
if NOT %_final% == %_ONEorTWO% (
    REM answered 1 or 2
    echo in if
) else ( 
    echo in else
)
Rubik
  • 1,431
  • 1
  • 18
  • 24
0

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] 
JosefZ
  • 28,460
  • 5
  • 44
  • 83
  • And how do I make it check if it is equal to all of the strings, not just a specific one? – Coding Towel Mar 07 '15 at 11:19
  • @CodingTowel - I don't understand what are you asking for. _Nothing_ can be equal to _all of the strings_, if that strings differ each other. – JosefZ Mar 07 '15 at 15:23
  • If I have this "for %%G in (%1% %2% %3% %4%)" Then I want all of those to be the same thing. – Coding Towel Mar 07 '15 at 15:29
  • @CodingTowel Still do not understand. I feel this exceeds topic of your original question an exceeds comment scope (as it's not a chat here). Please, consider asking a new question. – JosefZ Mar 07 '15 at 16:35
0

Here is another way using (the much better) choice command in combination with goto:

choice /C 123 /N /M "1, 2 or 3?"
set input=%errorlevel%
if %input%==0 goto end
goto %errorlevel%
:1
:2
Echo You pressed 1 or 2
goto end
:3
Echo You pressed 3
goto end
.....
:end
Echo End of Program
Pause
Monacraft
  • 6,510
  • 2
  • 17
  • 29