batch need to compare %name% against multiple letter

April 2, 2013 at 12:30:15
Specs: Windows 7
hi,
I need to compare a variable, %name%, against many letters of the alphabet at once.
like this

set /p name=
if %name% NEQ a,b,c,d,e,f,g,h... goto :not
if %name% == l,m,n,o,p,q,r... goto :yes

i need to compare %name% against a then b then c, and so on.

is this possible?
if so, how?


See More: batch need to compare %name% against multiple letter

Report •


#1
April 2, 2013 at 13:31:33
Unless someone knows a different way, I believe the only way to do what you want is to list them all out. I did a few quick Google searches and didn't come up with anything, but that doesn't mean there's not a better way to do it. Here's what I’m suggesting:

set /p name=
if %name% NEQ a goto not
if %name% NEQ b goto not
if %name% NEQ c goto not
if %name% NEQ d goto not
etc...
etc...
if %name% == l goto yes
if %name% == m goto yes
if %name% == n goto yes
if %name% == o goto yes
if %name% == p goto yes
etc...
etc...

A couple things to point out: in order for the batch script to go to "yes" the variable entered would have to be the letter specified and the letter specified only. Same with first set of checks, if what the user enters as a variable is "b", they will go to "not" because of the first check (b is not equal to a.)

Does that make sense?


Report •

#2
April 2, 2013 at 17:46:50
Here's a couple of thing's I've tried in the past, assuming I am "clear on the concept" of what you intend:
::exclusion / negative
setlocal enabledelayedexpansion
set test=abcdefgh
set name=
set /p name=ENTER ONE CHARACTER:
set test2=!test:%name%=!
::fyi
echo %test%
echo %test2%
if %test2% equ %test% goto :not
::--------

::inclusion / affirmative
set name=
set /p name=ENTER ONE CHAR:
call %name% 2>nul || goto :notgood
echo back from test, proceeding
::.. proceed with "valid-entry" code
:l
:m
:n
:o
:p
echo good
goto :eof

:notgood
echo invalid entry

I guess the question of the day is, if this is case-sensitive or not, because my code here is NON-case-sensitive (a=A etc.)


Report •

#3
April 3, 2013 at 05:46:34
Pretty easy, here but it will be a loop. You cannot compare simultaneously, that doesn't make any sense to my logic.
@echo off & setlocal enabledelayedexpansion
letter1=a
letter2=b
letter3=c
letter4=d
letter5=e
letter6=f
letter7=g
letter8=h
letter9=i
letter10=j
letter11=k
letter12=l
letter13=m
letter14=n
letter15=o
letter16=p
letter17=q
letter18=r
letter19=s
letter20=t
letter21=u
letter22=v
letter23=w
letter24=x
letter25=y
letter26=z
set cnt=0
:LOOP
set /A cnt+=1
if "%name%" NEQ "!letter%cnt%!" (
Goto :somewhere1
) else (
Goto :somewhere2
)
You can change it to go to the loop if NEQ or if EQUAL aka ==.

Report •

Related Solutions

#4
April 3, 2013 at 07:18:32
My question is, are you planning on having more than one character set into the variable? I only ask because it makes a difference in how you look for it. when using the comparison statements as far as I know it has to be an exact match. Note that nbrane prompts for a single character.

:: mike


Report •

#5
April 11, 2013 at 10:32:59
Another way you could do this is, even though it is lengthy:

if not "%name%"=="a" if not "%name%"=="b" ... goto Yes else goto No


Report •

Ask Question