Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
hi
i was trying to create a batch file but im getting some unwanted results. wen it runs and i select an option the program brings up all the option and does not exit..@ECHO OFF
echo MAIN LUNCH MENU:
echo 1.Staff
echo 2.Student
echo 3.Exitset INPUT=
set /p INPUT=Enter choice....If "%input%"=="1" goto staff
If "%input%"=="2" goto student
If "%input%"=="3" goto exit:STAFF
start c:\Users\Jahildar\staffmenu.bat:STUDENT
start c:\Users\Jahildar\studentmenu.bat:EXIT
start c:\Users\Jahildar> exitcan somebody help me please???????

Correction (no need for EXIT):
@ECHO OFF
echo MAIN LUNCH MENU:
echo 1.Staff
echo 2.Student
echo 3.Exitset INPUT=
set /p INPUT=Enter choice....If "%input%"=="1" goto staff
If "%input%"=="2" goto student
If "%input%"=="3" goto :EOF:STAFF
start c:\Users\Jahildar\staffmenu.bat
Goto :EOF:STUDENT
start c:\Users\Jahildar\studentmenu.bat

No point in clearing a var and setting it on the next line.
==============================
@ECHO OFFecho MAIN LUNCH MENU:
echo 1.Staff
echo 2.Student
echo 3.Exitset /p INPUT=Enter choice....
goto :%INPUT%
:1
start c:\Users\Jahildar\staffmenu.bat
Goto :EOF:2
start c:\Users\Jahildar\studentmenu.bat:3
=====================================
If at first you don't succeed, you're about average.M2

M2: "No point in clearing a var and setting it on the next line." The reason it's done is that the SET /P command does NOT set the variable if the user just presses Enter - it leaves it with its previous value. Suppose you already had a variable named input with value 2 before you run the batch file...
So I would say Jahildar was correct in initialising the variable prior to setting it with SET /P.
Some comments on the code:
1. If the user presses 4 or any other invalid value, the batch file should go back and re-print the menu. It should only proceed once the user has entered 1, 2 or 3.
2. The START commands run the other batch files in new windows. Is that what you want, or do you just want to run the other batch files in the same window?

Fail safe version...
@ECHO OFF
:LOOP
echo MAIN LUNCH MENU:
echo 1.Staff
echo 2.Student
echo 3.Exitset /p INPUT=Enter choice....
for %%j in (1 2 3) do if "%%j"=="%INPUT%" goto :%INPUT%
cls
goto :LOOP:1
start c:\Users\Jahildar\staffmenu.bat
Goto :EOF:2
start c:\Users\Jahildar\studentmenu.bat:3

THIS is what happen after i press 1 from main menu.
- the staff menu comes up..main closes
- press one from staff:
* lunch menu comes up...staff menu stays with C:\users\jahildar> at the endhow do i get stagg menu to close as well?
here is the code:@ECHO OFF
echo Staff LUNCH MENU:
echo 1.Show list of lunch items and cost
echo 2.Buy item
echo 3.return to Mainset INPUT=
set /p INPUT=Enter choice....goto:%input%
:1
start c:\Users\Jahildar\item.bat
goto:EOF:2
start c:\Users\Jahildar\buy1.bat
goto:EOF:3
start c:\Users\Jahildar\menu.bat

You can use the EXIT command instead of GOTO :EOF, but be aware that if you then try to test the batch file by typing its name at the command prompt, it will close your command prompt window when it exits.

Ignore my previous post. Better solution below:
Instead of START batchfilename.bat
have START cmd /c batchfilename.bat

The solution to preventing surprise vales is to SETLOCAL.
=====================================
If at first you don't succeed, you're about average.M2

(For M2):
SETLOCAL doesn't prevent global values from being visible.
To see what I mean, try the following batch file:
setlocal
set /p windir=Please just press Enter:
set windir

Combining all the suggestions from various posters above, here is a "final" version. Please try it.
@ECHO OFF
SETLOCAL
:LOOP
echo MAIN LUNCH MENU:
echo 1.Staff
echo 2.Student
echo 3.Exitset INPUT=
set /p INPUT=Enter choice....
for %%j in (1 2 3) do if "%%j"=="%INPUT%" goto :%INPUT%
cls
goto :LOOP:1
start cmd /c c:\Users\Jahildar\staffmenu.bat
Goto :EOF:2
start cmd /c c:\Users\Jahildar\studentmenu.bat:3

@ECHO OFF
setLocal EnableDelayedExpansionecho MAIN LUNCH MENU:
echo 1.Staff
echo 2.Student
echo 3.Exitset /p INPUT=Enter choice....
goto :!INPUT!
:1
start c:\Users\Jahildar\staffmenu.bat
Goto :EOF:2
start c:\Users\Jahildar\studentmenu.bat:3
=====================================
If at first you don't succeed, you're about average.M2

Usage scenario doesn't look very user-friendly:
C:\>MENU
MAIN LUNCH MENU:
1.Staff
2.Student
3.Exit
Enter choice....4
The system cannot find the batch label specified - 4
C:\>That's why the loop is there.

i got it working...THANK U GUYS FOR HELPING. Ya'll are good at these stuff. what i learn here today is basically my first tutorial class for batch files. THANK U A LOT.

"The system cannot find the batch label specified - 4"
If the user isn't smart enough to pick one of 3 choices offered... not much hope.
=================================
Also, not much point in STARTing another bat if the first one is going to end.=================================
@ECHO OFF
setLocal EnableDelayedExpansionecho MAIN LUNCH MENU:
echo 1.Staff
echo 2.Student
echo 3.Exitset /p INPUT=Enter choice....
goto :!INPUT!
:1
c:\Users\Jahildar\staffmenu.bat:2
c:\Users\Jahildar\studentmenu.bat:3
=====================================
If at first you don't succeed, you're about average.M2

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |