1.

Solve : goto help...?

Answer»

I am TRYING to make a batch file with a menu, but it doesn't work. No matter what i push it just goes onto the next line or just exits:
Here is an example:
-----------------------------------------------------------------------------------------------------------------------------------------
@echo off
echo PLEASE PRES 1, 2, 3 ,4 OR 5
@echo.
echo 1.
echo 2.
echo 3.
echo 4.
echo 5.
@echo.
pause

if errorlevel 1 goto 1
if errorlevel 2 goto 2
if errorlevel 3 goto 3
if errorlevel 4 goto 4
if errorlevel 5 goto 5

:1
echo YOU PRESSED 1
pause

:2
echo YOU PRESSED 2
pause

:3
echo YOU PRESSED 3
PAUSE

4:
echo YOU PRESSED 4

5:
echo YOU PRESSED 5You're assuming that pressing a key will set errorlevel?

I hoped (and this is the first time i have used the goto COMMAND) !
Any help will be much appreciated !You could do something LIKE this:

@echo off
:choosenum
set /p num="Choose a number (1-5): "
if %num% GTR 5 cls & goto choosenum
if %num% LSS 1 cls & goto choosenum

if %num% EQU 1 cls & goto 1
if %num% EQU 2 cls & goto 2
if %num% EQU 3 cls & goto 3
if %num% EQU 4 cls & goto 4
if %num% EQU 5 cls & goto 5

:1
echo YOU PRESSED 1
pause> nul & goto end

:2
echo YOU PRESSED 2
pause> nul & goto end


:3
echo YOU PRESSED 3
pause> nul & goto end


:4
echo YOU PRESSED 4
pause> nul & goto end


:5
echo YOU PRESSED 5
pause> nul & goto end


:end

That works quite well, and you can always add in extra stuff if you wanted.And the next homework assignment will be Quote from: Dusty on FEBRUARY 19, 2008, 11:59:55 PM

And the next homework assignment will be
They'll never end...incidentally, if you are checking for errorlevel, you need to arrange the test in reverse order, with the highest errorlevel tested for at the TOP, and errorlevel 1 at the bottom of the list, because "if errorlevel 1" means "if errorlevel is equal to or greater than 1" and if you have it at the top, any nonzero errorlevel will trigger it and the rest will be skipped.

Right about the order of the "If errorlevel" commands.

Nobody mentioned the way you want to set your errorlevel. Set it with the choice command. But wait Microsoft dropped the choice command in recent versions.(I don't know why. It seems that they would have kept it for backward compatibility, even if there are better ways to do the same thing.)(yes I read on this forum that there is a choice written by someone else that you can use, but that is extra baggage.)

By the way Dark Blade, Thanks for the set /p var=... method you posted.
I have been away from writing batch files for a few years and was not familiar.
I will be trying this as it is close to something I'm trying to do.
Quote from: llmeyer1000 on February 20, 2008, 08:50:02 AM
...
Nobody mentioned the way you want to set your errorlevel. Set it with the choice command. But wait Microsoft dropped the choice command in recent versions.

Explore here:
http://hp.vector.co.jp/authors/VA007219/dkclonesup/choice.html
to download various versions of choice .

Quote
(I don't know why. It seems that they would have kept it for backward compatibility, even if there are better ways to do the same thing.)(yes I read on this forum that there is a choice written by someone else that you can use, but that is extra baggage.)
...

No moreso than any other external command. Just put a copy of it in a directory that is in your path, same as MS would have done if they had included it in the first place.


Thanks Dark! i had just been looking at the set /p command before i came back to this forum but didn't know exactly how to use it.

And for those that think this is a homework assignment its not!!! I was just making a batch file for fun.


Discussion

No Comment Found