shido 641
shido 641

Reputation: 11

How do you set a range of number as an errorlevel in batch?

I have this script in batch and got a range of questions 1 - 12.
All my code works, but I just can't seem to find a code to throw into my batch to throw out an error message for invalid ranges selected

Example: If I choose 13, which is not part of the options, it automatically goes to 1.
I want all number from 13 up, to show "invalid option" and then default back to the choice menu.

I am not using the shift command in my batch as I just used the following code instead

set /p choice=Type the number that corresponds to the Step you want to take.
if not '%choice%'=='' set choice=%choice:~0%

Notice the ~0 allows me to use numbers passed 9 instead of using the shift command.
Nevertheless, I need code that will say if the user type 13 or up and error appears.
Any ideas? In other words if choices are in ranges 13 - 999 show error.

Update

ok so, I looked at my code and realized I missed one line of code. which is

@echo "%choice%" is not a valid selection/option

It seems that my coding was right where I gave '%choice%'=='' I just didnt leave an echo to say the selection was wrong. Turns out I didnt need any errorlevels in there.

Hope this helps someone

Upvotes: 1

Views: 739

Answers (1)

Jeff K
Jeff K

Reputation: 243

Just to clarify: The following code will output an error statement if the choice is greater than 13, and set the inputted value to the first character entered

@echo off

SET /P choice=Type the number that corresponds to the Step you want to take.

IF %choice% GTR 12 GOTO Error

ECHO You entered %choice%

GOTO End

:Error

ECHO %choice% is not a valid selection/option

set choice=%choice:~0,1%

ECHO %choice%

:End

Upvotes: 0

Related Questions