1.

Solve : Simple batch menu question?

Answer»

I'm trying to make a batch menu with option from 1 to 3 but if I was to click say option 7 which is not register, it would automatically take me to option 1. Is there a way that it will only force my batch to accept option from 1 to 3 only. If the user enters say option 7 it would display Main_Menu again.

this is what I have so far but it does not work.

:: ---------------------------------------------------------------
: Main_Menu
:: ---------------------------------------------------------------
CLS
TITLE MAIN MENU
ECHO 1) Sub_MenuA
ECHO 2) Sub_MenuB
ECHO 3) Internet
ECHO.
ECHO.

SET /p Option=Choice:
if "%Option%"=="1" GOTO Sub_MenuA
if "%Option%"=="2" GOTO Sub_MenuB
if "%Option%"=="3" GOTO Internet

thanksQuote from: locbtran on March 22, 2009, 09:00:56 AM

:: ---------------------------------------------------------------
: Main_Menu
:: ---------------------------------------------------------------

I strongly suggest you do NOT use labels to start comment LINES. This is not supported. They can CAUSE problems for example such a line in a loop will break it. Use REM instead.


Code: [Select]
:start
CLS
TITLE MAIN MENU
ECHO 1) Sub_MenuA
ECHO 2) Sub_MenuB
ECHO 3) Internet
ECHO.
ECHO.

SET /p Option=Choice:
if "%Option%"=="1" GOTO Sub_MenuA
if "%Option%"=="2" GOTO Sub_MenuB
if "%Option%"=="3" GOTO Internet

echo.
echo Please CHOOSE 1 to 3 only!
echo.
pause
goto start

Loc B Tran,

Code: [Select]
C:\>type menu.bat
@Echo off

:Start
cls
echo TITLE MAIN MENU
ECHO 1) Sub_MenuA
ECHO 2) Sub_MenuB
ECHO 3) Internet
ECHO 4) Quit
ECHO.
ECHO.

SET /p Option=Choice:
if "%Option%"=="1" GOTO Sub_MenuA
if "%Option%"=="2" GOTO Sub_MenuB
if "%Option%"=="3" GOTO Internet
if "%Option%"=="4" GOTO EOF
Goto Start

:Sub_MenuA
echo Sub_MenuA
pause
Goto Start
:Sub_MenuB
echo Sub_MenuB
pause
Goto Start
:Internet
echo Internet
"c:\program files\internet explorer\iexplore.exe" http://www.google.com/
Pause
Goto Start
:EOF

C:\>

Output of menu.bat

Quote
C:\>type outmenu.txt
TITLE MAIN MENU
1) Sub_MenuA
2) Sub_MenuB
3) Internet


Choice: 1
Sub_MenuA
Press any key to continue . . .

TITLE MAIN MENU
1) Sub_MenuA
2) Sub_MenuB
3) Internet


Choice: 2
Sub_MenuB
Press any key to continue . . .

TITLE MAIN MENU
1) Sub_MenuA
2) Sub_MenuB
3) Internet


Choice: 3
Internet
Press any key to continue . . .

TITLE MAIN MENU
1) Sub_MenuA
2) Sub_MenuB
3) Internet


Choice: 7

TITLE MAIN MENU
1) Sub_MenuA
2) Sub_MenuB
3) Internet
C:\>
thank you

the code works perfectly.


Discussion

No Comment Found