Jovica
Jovica

Reputation: 444

Batch check if variable is specific value

I can not find solution (or is too complicated for me to understand), I would like to set variable by input

@echo off
set /p var="Choose (AV, RN or I) : " %=%
echo %var%

If value is one of these three then proceed (I will then use that variable to set path, copy,move,start...), else goto exit.

Upvotes: 2

Views: 4124

Answers (2)

Aacini
Aacini

Reputation: 67216

The simplest way to achieve this is not checking the variable at all, but using its value in a call command with the error messages redirected to nul. This way, if the variable have some of the valid values, the appropriate routine is called; otherwise, nothing happens:

@echo off
set /p var="Choose (AV, RN or I) : " %=%
echo %var%
call :%var% 2> NUL
goto :EOF

:AV
echo Selected: AV
exit /B

:RN
echo Selected: RN
exit /B

:I
echo Selected: I
exit /B

Upvotes: 2

Magoo
Magoo

Reputation: 79983

if /i "%var%"=="AV" goto av
if /i "%var%"=="RN" goto rn
if /i "%var%"=="I" goto i

echo illegal input "%var%"

Quite what you want to do from there, you don't reveal.

The /i after the if means - ignore case. If you insist that the case should match, use if "... - that is, remove the /i

Upvotes: 4

Related Questions