Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: How do you add several requirments to one "if" command?  (Read 2429 times)

0 Members and 1 Guest are viewing this topic.

bbman12

  • Guest
How do you add several requirments to one "if" command?
« on: July 24, 2011, 07:17:38 PM »
I couldn't find the right way to say this, but:

if %var%==(several variables here)

so like:

if %var%==a,b,c,d,e,f,g

I just don't know how you would put it.

Thanks.

Geek-9pm


    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: How do you add several requirments to one "if" command?
« Reply #1 on: July 24, 2011, 08:06:41 PM »
You can nest the if statements.
1. In Batch nested nIFs are used often.
2. You and use the AND operator.
3. You can aggregate results.
(pseudo code)
set got to zero
if var is a bump got
if var is b bump got
if var is c bump got
if got is 3 say 'got all'
If got is 0 say  'got nada'

But I don't think that is what you want.

Do you want a CASE block? It is not native to batch files.
(pseudo code)
CASE BEGIN
  var = a goto isa
  var = b goto isb
  var = c goto isc
END CASE

You would have more tools in a script language like Vb script.
Code: [Select]
  Select Case testexpression
    [Case expressionlist-n
      [statements-n]] . . .
    [Case Else
      [elsestatements-n]]
  End Select

bbman12

  • Guest
Re: How do you add several requirments to one "if" command?
« Reply #2 on: July 24, 2011, 08:15:24 PM »
I'm sort of new, but really I just want to be able to have several possibilities on one "if" command.

I'm sorry if that doesn't make sense.

So like:

on "if %var%==1,2,3,4,5"

if either 1 through 5 is entered, it will execute a command

but is that the correct format for the command, or do I have to use an "and" command?

if so, Can I have some examples?




Thanks a bunch.

Salmon Trout

  • Guest
Re: How do you add several requirments to one "if" command?
« Reply #3 on: July 25, 2011, 12:10:06 AM »
AS has been said, batch does not have a multiple - If command, nor a CASE statement. You have to get crafty.

If a,b,c,d,e are consecutive numbers e.g. 1 to 5 then you can do this

if %var% GTR 0 (
   if %var% LSS 6 (
      goto pass
   )
)
echo Bad value!
goto end

:pass
echo Good value!
:end

Otherwise you must test for the values individually
   

if "%var%"=="a" goto pass
if "%var%"=="b" goto pass
if "%var%"=="c" goto pass
if "%var%"=="d" goto pass
if "%var%"=="e" goto pass
echo Bad value!
goto end

:pass
echo Good value!
:end

Or you could use some FOR loop trickery but I won't put that here. Bill's around!