IF ERRORLEVEL exit if variable = none

Microsoft / Dos 6.1
May 28, 2010 at 15:47:03
Specs: Windows 7 Ultimate, 4GB DDR2
Hello, I'm trying to set an errorlevel when none of the variable names above is selected, e.g.:

@ECHO OFF

:help
::help
goto BG

:error1
ECHO "%BG%" is invalid - Type "help" for help.

:BG
SET /P BG=Choose your BG:
IF "%BG%" EQU "" goto error1
IF NOT "%BG: =%"=="%BG%" goto error1
IF %BG%==A SET X=0
IF %BG%==B SET X=1
IF %BG%==C SET X=2
IF %BG%==help goto help

IF ERRORLEVEL 0 goto TT ELSE goto error1
::the command above = if any of the above is entered, go on. if none of the options above is entered goto error1.

goto TT
:TT


See More: IF ERRORLEVEL exit if variable = none

Report •

#1
May 28, 2010 at 17:52:39
here's what i would do:
:BG
:: these 1 lines are required for this method
SET BG=
SET X=
SET /P BG=Choose your BG:
IF %BG%==A SET X=0
IF %BG%==B SET X=1
IF %BG%==C SET X=2
IF /I %BG%==help set X=help
if not defined X goto :error1
:TT

Report •

#2
May 29, 2010 at 02:19:31
Stated the correct solution is the code posted by nbrane, you have to know:

1) IF ERRORLEVEL NN is TRUE if ErrorLevel is greater or equal to NN. So your statement should be coded

IF NOT ERRORLEVEL 1 goto IT ELSE goto error1

2) In the case you reported to set the ErrorLevel is left to your own, i.e. there is no ErrorLevel set automatically by the system.


Report •

#3
May 29, 2010 at 13:25:06
Hmm all I needed to know was the "if not defined" condition.

Thank you guys it works like a charm now =]!


Report •
Related Solutions


Ask Question