if command with AND/OR/NOT

February 5, 2010 at 17:49:07
Specs: Windows 7 Ultimate x64, 4 GB
set /p choice=(Y/N)?
if '%choice%'=='y' or 'Y' echo Yes

How can I use OR/AND/NOT in if command?

I found my answer, batch files doesn't support what I wanted.
I have to use this:

if (condition1) goto a
if not (condition2) goto b
[lines to execute if test is true]
goto end
[lines to execute if inclusive disjunction is false]

Feel free to lock.

See More: if command with AND/OR/NOT

Report •

February 5, 2010 at 20:00:57
you can't, not really, you have to juggle things around a bit.
in your example, that is easy:
if /i '%choice%' == 'y' echo yes
the /i says to ignore case.
you can construct ANDs by stacking IFs, (since each depends on outcome of preivous IF):
if x=1 if y=2 if z=3 echo 1 2 3
you can build elaborate structures of nested IFs, but that can become cumbersome in complex situations.
One way to construct a "case" structure is to call labels:
set /p test=enter the subroutine (one, two) you want to run:
call :%test% 2>nul
if %errorlevel% gtr 0 (
:: this happens if there's no label...
echo that selection is not an option
goto tryagin
) else (
goto :eof
echo one
goto :eof
echo two

if you need to have multiple instances of similar labels in various "case" constructs, to keep them from conflicting , just add some letters to the label to make it unique, like:
call :aa%choice%
labels are not case sensitive, :Y same as :y

Report •

February 5, 2010 at 20:32:22
Thanks for your help.
Now, I'm able to do what I wanted. :)


Report •

February 7, 2010 at 07:09:49
And use the NOT word (when you have to) for the IF statement, but always as the first parameter of that command.

The usage of complex IF (and ELSE)-statements is very programmer unfriendly in Windows/DOS ... often there are different solutions, one even worse than another, but all pretty bad. Remember that DOS-batching is not the same as Java or C ... there is even not a switch (case/...) command (as Nbrane also states). Using GOTO is - in the world of programming - called spaghetti-coding. It is ideal if you want nobody (including yourself) to ever debug your code, that is assuming you can get it even correct in the first place.

Good luck !

Report •

Related Solutions

Ask Question