You are not logged in.

#1 02 Jul 2014 10:22

moconnell
Member
Registered: 13 Dec 2006
Posts: 8

IF statement to do something if variable is not equal to 2 variables

What would be a better way to do a command when the test variable is not equal to 2 variables

The way I do it currently is by nesting another IF:
IF /I %test % NEQ 1 (IF %test e% NEQ 2 (command) ELSE exit)

Obviously I would like to do a single statement with a IF (%test% NEQ 1 && %test% NEQ 2), but IF doesn't seem to like that.

(I might be being thick here.)

Offline

#2 02 Jul 2014 12:12

foxidrive
Member
Registered: 04 Apr 2013
Posts: 339

Re: IF statement to do something if variable is not equal to 2 variables

This is a simple AND statement.

IF %test% NEQ 1 IF %test% NEQ 2 command

Offline

#3 02 Jul 2014 16:11

moconnell
Member
Registered: 13 Dec 2006
Posts: 8

Re: IF statement to do something if variable is not equal to 2 variables

foxidrive wrote:

This is a simple AND statement.

IF %test% NEQ 1 IF %test% NEQ 2 command

OK That's pretty much what I was doing without the brackets. So they're no more conventional way to do it you have to nest it?

Offline

#4 02 Jul 2014 17:09

foxidrive
Member
Registered: 04 Apr 2013
Posts: 339

Re: IF statement to do something if variable is not equal to 2 variables

If that is the exact task then it works using that technique.

In most cases Batch files are written around an exact task and when the task changes then the code techniques can change too.
For example a small change in the task can often mean a large change in the code...

Offline

#5 02 Jul 2014 23:27

Aacini
Member
Registered: 05 Dec 2012
Posts: 149

Re: IF statement to do something if variable is not equal to 2 variables

setlocal EnableDelayedExpansion

set "unwantedValues= 1 2 "

if "!unwantedValues: %test% =!" equ "%unwantedValues%" (
    echo %test% is not anyone of 1 or 2
) else (
   echo %test% is 1 or 2
)

You may place any number of values in "unwantedValues" variable; just be sure to separate the values with spaces and begin and end the values with space.

Offline

#6 03 Jul 2014 20:45

moconnell
Member
Registered: 13 Dec 2006
Posts: 8

Re: IF statement to do something if variable is not equal to 2 variables

foxidrive wrote:

...For example a small change in the task can often mean a large change in the code...

Ie it's terrible lol

Thanks for confirming guys

Offline

#7 04 Jul 2014 08:42

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: IF statement to do something if variable is not equal to 2 variables

Yeah it sucks a bit.  There are a few methods you could employ which are more extensible than nesting lots of "if" statements.  In addition to Aacini's example:

@echo off
set "validValues=1 2 3"
set /p "testMe=>: "

echo:"%validValues%" | findstr "\<%testMe\>" >nul

if not errorlevel 1 (echo:Valid) ELSE (echo:Not Valid)

Or even...

@echo off
set "validValues=1 2 3"
set /p "testMe=>: "
set "flag="

for %%a in (%validValues%) do if "%%~a" EQU "%testMe%" set flag=1

if defined flag (echo:Valid) ELSE (echo:Not Valid)

The "findstr" method being the more robust of the two (but there's a small time penalty for using the external EXE).

Both Untested

Last edited by bluesxman (04 Jul 2014 08:44)


cmd | *sh | ruby | chef

Offline

Board footer

Powered by