Page 1 of 1

How to translate a "OR" statement in BATCH ?

Posted: 16 Aug 2011 07:31
by chogall
Hi!

I need to write something like :

Code: Select all

if %choice% == q goto QUIT


but I want that it matches 'Q' also. So I thought about writing something like :

Code: Select all

if %choice% == q|Q goto QUIT


But it doesn't work.
Any idea how I can translate 'q' OR 'Q' ?

Thank you!

Re: How to translate a "OR" statement in BATCH ?

Posted: 16 Aug 2011 08:27
by dbenham
For this application you don't need an OR - you just need to do a case insensitive comparison:

Code: Select all

if /i %choice% == q goto QUIT


To emulate OR between any possible conditional tests it is easiest to use a temporary variable: (pseudo code)

Code: Select all

set "TEST="
if <condition 1> set TEST=1
if <condition 2> set TEST=1
if defined TEST <condition 1 OR condition 2 is true so do whatever>


Dave Benham

Re: How to translate a "OR" statement in BATCH ?

Posted: 27 Aug 2011 22:05
by taripo
dbenham wrote:To emulate OR between any possible conditional tests it is easiest to use a temporary variable: (pseudo code)...
Dave Benham


You say that's the easiest way. What's the hard way then?

Re: How to translate a "OR" statement in BATCH ?

Posted: 28 Aug 2011 20:24
by Ocalabob
Greetings chogall

Try this (untested)

Code: Select all

@echo off
set /p choice="Enter q or Q " 
if %choice%==q goto quit
if %choice%==Q goto quit
:quit
echo You picked %choice%
pause
set choice=


Best wishes.