AdamTheCarpenter
AdamTheCarpenter

Reputation: 13

set vars in bat files

ok, heres my question straight to the point: ive used set /p in my bat script:

SET x= 
SET /P x= 
IF /I '%x%' == '1' GOTO 1
IF /I '%x%' == '2' GOTO 2
IF /I '%x%' == '3' GOTO 3
IF /I '%x%' == '4' GOTO 4
IF /I '%x%' == '5' GOTO 5

and my issue is, if someone types something out of ordinary (eg. 6) I want the command to be goto a (so they get back to the menu) so is there a way to do it ? like using %errorlevel% or "else" ? thanks!

Upvotes: 0

Views: 165

Answers (1)

npocmaka
npocmaka

Reputation: 57252

:repeat
SET "x=" 
SET /P "x=enter x:" 
IF /I "%x%" == "1" GOTO 1
IF /I "%x%" == "2" GOTO 2
IF /I "%x%" == "3" GOTO 3
IF /I "%x%" == "4" GOTO 4
IF /I "%x%" == "5" GOTO 5
echo invalid input
goto :repeat
:1
...
:2
....

if you set goto :repeat before all other labels it will repeat the prompting if the input is invalid and will be skipped if it is.

Upvotes: 2

Related Questions