3fingersalute
3fingersalute

Reputation: 1

Batch file help using IF with variables

Ok, it's been a little while since I've worked with batch files so I'll admit I'm a little rusty. I'm familiar with getting and input and using it as variable and I know how to use the IF command, just having a little trouble figuring (remembering) the best way to use it. Basically what I want to do is have the user input a number and if that number is one of about 18 different numbers continue to another part of the file and if it is not one of those then continue to a different part.

My question is, do I need to make 18 different IF statements or is there a way to compare all of them with one statement?

Upvotes: 0

Views: 48

Answers (3)

Aacini
Aacini

Reputation: 67216

@echo off
setlocal EnableDelayedExpansion

set numberList=2 4 6 8 10 33 212 467
set /P "input=Enter your number: "
if "!numberList:%input%=!" neq "%numberList%" (
   echo Input matches one of the numbers in list
) else (
   echo Input is no one of the list
)

Upvotes: 1

foxidrive
foxidrive

Reputation: 41234

Test this:

@echo off
set /p "input=Enter your number: "
set "yes="
for %%a in (2 4 6 8 10 212 33 467) do if "%input%"=="%%a" set yes=1
if defined yes (
   echo input matches
) else ( 
   echo no match
)
pause

Upvotes: 0

Ir Relevant
Ir Relevant

Reputation: 1033

@echo off

set/p var=What's your choice? 

           set OR=(goto :lable) else if "%var%" ==

if "%var%" == "18" %OR% "296" %OR% "7239" %OR% "whatever" goto :lable
exit/b

:lable
echo worked
pause

Upvotes: 0

Related Questions