How to translate a "OR" statement in BATCH ?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
chogall
Posts: 1
Joined: 16 Aug 2011 07:23

How to translate a "OR" statement in BATCH ?

#1 Post by chogall » 16 Aug 2011 07:31

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!

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

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

#2 Post by dbenham » 16 Aug 2011 08:27

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

taripo
Posts: 228
Joined: 01 Aug 2011 13:48

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

#3 Post by taripo » 27 Aug 2011 22:05

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?

Ocalabob
Posts: 79
Joined: 24 Dec 2010 12:16
Location: Micanopy Florida

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

#4 Post by Ocalabob » 28 Aug 2011 20:24

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.

Post Reply