Batch - Cannot find batch label specified

offroadguy56

Honorable
Apr 3, 2013
450
1
10,960
Hello.

I'm writing a batch script for some coworkers and myself to more easily open files and programs. Would also like to use a more customized script at home.

I've pulled a sample script off the web allowing a list to be created with echo and using a "set /p" command to select items from the list to call on a function to be executed.

I'm trying to make it more user friendly but when an incorrect input like XYZ that doesn't match any of the function labels is entered the prompt will quickly display that it couldn't find the specified label. I want the prompt to stop there and to loop back to the beginning of the script.

I've tried puttin a GOTO command below the IF statement. I've tried an ELSE statement which seems to just ignore the first part of the IF statement and executes the ELSE part no matter what input was entered. Do I need another IF statement with an ERRORLEVEL check? I'm not sure how to do that, or what number is even returned when that error occurs.

Here's some sample code that highlights the issue.
@echo off
:start
echo choose 1
echo choose 2 to test wrong input

SET /p x=Choose an Option:
IF '%x%' == '%x%' GOTO Item_%x%

:Item_1
echo option 1 was selected
GOTO start

:Success
echo wrong input works
pause
GOTO start

Thanks. If you need anymore info let me know.
 
Solution
Hi, I searched for a similar batch file and found one here: https://community.spiceworks.com/scripts/show/362-batch-file-menu

I made a few minor modifications to it and simplified it a little bit and came up with the following:

@ECHO OFF
CLS

:MENU
CLS

ECHO ============= MENU NAME =============
ECHO 1. Selection 1
ECHO 2. Selection 2
ECHO 3. Selection 3
ECHO ==========PRESS 'Q' TO QUIT==========
ECHO.

SET INPUT=
SET /P INPUT=Please select a number:

IF /I '%INPUT%'=='1' GOTO Selection1
IF /I '%INPUT%'=='2' GOTO Selection2
IF /I '%INPUT%'=='3' GOTO Selection3
IF /I '%INPUT%'=='Q' GOTO Quit

echo.

ECHO ============INVALID INPUT============
ECHO Please select a number from the Main
echo Menu [1-3] or select 'Q' to quit.
ECHO...
Hi, I searched for a similar batch file and found one here: https://community.spiceworks.com/scripts/show/362-batch-file-menu

I made a few minor modifications to it and simplified it a little bit and came up with the following:

@ECHO OFF
CLS

:MENU
CLS

ECHO ============= MENU NAME =============
ECHO 1. Selection 1
ECHO 2. Selection 2
ECHO 3. Selection 3
ECHO ==========PRESS 'Q' TO QUIT==========
ECHO.

SET INPUT=
SET /P INPUT=Please select a number:

IF /I '%INPUT%'=='1' GOTO Selection1
IF /I '%INPUT%'=='2' GOTO Selection2
IF /I '%INPUT%'=='3' GOTO Selection3
IF /I '%INPUT%'=='Q' GOTO Quit

echo.

ECHO ============INVALID INPUT============
ECHO Please select a number from the Main
echo Menu [1-3] or select 'Q' to quit.
ECHO ======PRESS ANY KEY TO CONTINUE======

PAUSE > NUL
GOTO MENU

:Selection1
echo Run Program 1
pause
goto menu

:Selection2
echo Run Program 2
pause
goto menu

:Selection3
echo Run Program 3
pause
goto menu

:Quit
CLS

ECHO ==============THANKYOU===============
ECHO ======PRESS ANY KEY TO CONTINUE======

PAUSE>NUL
I hope this helps.
 
Solution