Checking a condition from a list of variables?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Cat
Posts: 32
Joined: 11 Nov 2011 12:04

Checking a condition from a list of variables?

#1 Post by Cat » 11 Nov 2011 12:18

This is part of a code I'm writing.

Code: Select all

choice /c wdsa /n>nul&set A%V1%%V2%=_
if %errorlevel%==1 set /a V1=%V1%-1
if %errorlevel%==2 set /a V2=%V2%+1
if %errorlevel%==3 set /a V1=%V1%+1
if %errorlevel%==4 set /a V2=%V2%-1
set target=A%V2%%V1%
if %target%==%check% echo invalid&goto grid


I want the condition to activate with %check% being set to A12, A34, A21, A09, and A19.
Is there any way to do this without coding several lines?

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

Re: Checking a condition from a list of variables?

#2 Post by dbenham » 11 Nov 2011 14:57

Try this

Code: Select all

setlocal enableDelayedExpansion
choice /c wdsa /n>nul&set A%V1%%V2%=_
if %errorlevel%==1 set /a V1-=1
if %errorlevel%==2 set /a V2+=1
if %errorlevel%==3 set /a V1+=1
if %errorlevel%==4 set /a V2-=1
set target=A%V2%%V1%
set test=,A12,A34,A21,A09,A19,
if !test:,%target%,=! neq !test! echo invalid&goto grid

The code searches test for target and replaces with nothing. If the result does not match test then target was in test.
I also simplified your V1 and V2 math

Dave Benham

Cat
Posts: 32
Joined: 11 Nov 2011 12:04

Re: Checking a condition from a list of variables?

#3 Post by Cat » 11 Nov 2011 18:01

I'm getting "A12 was not expected at this time" and the window closes. :S

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

Re: Checking a condition from a list of variables?

#4 Post by dbenham » 11 Nov 2011 18:20

Oops - I forgot some quotes in the last line:

Code: Select all

setlocal enableDelayedExpansion
choice /c wdsa /n>nul&set A%V1%%V2%=_
if %errorlevel%==1 set /a V1-=1
if %errorlevel%==2 set /a V2+=1
if %errorlevel%==3 set /a V1+=1
if %errorlevel%==4 set /a V2-=1
set target=A%V2%%V1%
set test=,A12,A34,A21,A09,A19,
if "!test:,%target%,=!" neq "!test!" echo invalid&goto grid


Dave Benham

Cat
Posts: 32
Joined: 11 Nov 2011 12:04

Re: Checking a condition from a list of variables?

#5 Post by Cat » 11 Nov 2011 19:04

That works perfectly, thanks :)

Post Reply