You are not logged in.
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
This is a simple AND statement.
IF %test% NEQ 1 IF %test% NEQ 2 command
Offline
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
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
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
...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
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