Jump to content

Batch file choice issue

WkdPaul

So, I have a long and complicated script that I can't post in here, but I'll get to the issue ; choice command.

 

I have a few menus where you have to confirm things one after another, but along the way, one of the result stops working, after the second menu, any options after the second one doesn't work and the second option is automatically taken.

 

I have different iterations of this code and  they all have the same issue, here is the last iteration (previous iteration had the "if errorlevel x GOTO y" style, that didn't work either and that's why I tried to put that into a variable).

Basically, when I get to :USURE, if I select option 3, it selects option 2 instead (option 1 and 2 work fine) ... ;

 

echo.
echo selection 1
echo 1 - YES
echo 2 - NO
echo 3 - EXIT
echo.
echo.
choice /c:123
if errorlevel 1 SET M=1
if errorlevel 2 SET M=2
if errorlevel 2 SET M=3
IF %M%==1 GOTO ONE
IF %M%==2 GOTO TWO
IF %M%==3 GOTO EOF

:ONE
cls
echo.
echo U SURE
echo 1 - YES
echo 2 - NO
echo 3 - EXIT
echo.
echo.
choice /n /c 123
if errorlevel 1 SET M=1
if errorlevel 2 SET M=2
if errorlevel 3 SET M=3
IF %M%==1 GOTO USURE
IF %M%==2 GOTO TWO
IF %M%==3 GOTO EOF



:USURE
CLS
ECHO.
ECHO REALLY REALLY SURE ?
ECHO 1 - YES
ECHO 2 - NO
ECHO 3 - EXIT
ECHO.
ECHO.
CHOICE /n /c 123
if errorlevel 1 SET M=1
if errorlevel 2 SET M=2
if errorlevel 3 SET M=3
IF %M%==1 GOTO DONE
IF %M%==2 GOTO TWO
IF %M%==3 GOTO EOF

 

If you need help with your forum account, please use the Forum Support form !

Link to comment
Share on other sites

Link to post
Share on other sites

damn ... so I extracted that to it's own batch file (yeah I know, haven't done that until now ! 😞 ) and it works ... so it might be and issue somewhere else in the batch file ! 😞

If you need help with your forum account, please use the Forum Support form !

Link to comment
Share on other sites

Link to post
Share on other sites

Found a few typos in the batch file (again, work related, so I can't post the whole thing).

 

Should've done that before creating this thread ! lol

 

Hint : make sure your variables are all ok and that you're not missing an ECHO or REM somewhere 😛

If you need help with your forum account, please use the Forum Support form !

Link to comment
Share on other sites

Link to post
Share on other sites

Put a labrel at start then say goto label, don't repeat echo commands so much

 

also @ in front of a command hides it so you only get output of that command  ex @echo.

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, mariushm said:

Put a labrel at start then say goto label, don't repeat echo commands so much

 

also @ in front of a command hides it so you only get output of that command  ex @echo.

 

 

The code above is just a small section of the batch file, so it's missing labels.

 

The echo. are only to add empty lines so that the "menu" is easier to read, else, it's all cramped at the top without any breaks between lines and sometimes makes it harder to read, the rest are prompt for the choices (in the code it's not just yes/no/exit, that's just place holders that I put here for the purpose of showing the section that was problematic).

 

Thanks for the advice.

If you need help with your forum account, please use the Forum Support form !

Link to comment
Share on other sites

Link to post
Share on other sites

Its been a while but IIRC you should do the error level checks in reverse in a batch file, so start at 3, then 2 and finally 1. Can't remember the reason but I'm sure not doing it this way can cause weird issues like you described.

 

Also, unless you have a specific reason to there's no need to use variables in your error checks, errorlevel is a predefined variable anyway so instead of

if errorlevel 1 SET M=1
if errorlevel 2 SET M=2
if errorlevel 3 SET M=3
IF %M%==1 GOTO DONE
IF %M%==2 GOTO TWO
IF %M%==3 GOTO EOF

you can do

IF %ERRORLEVEL% EQU 3 GOTO EOF
if %ERRORLEVEL% EQU 2 GOTO TWO
if %ERRORLEVEL% EQU 1 GOTO DONE

 

Main Rig:-

Ryzen 7 3800X | Asus ROG Strix X570-F Gaming | 16GB Team Group Dark Pro 3600Mhz | Corsair MP600 1TB PCIe Gen 4 | Sapphire 5700 XT Pulse | Corsair H115i Platinum | WD Black 1TB | WD Green 4TB | EVGA SuperNOVA G3 650W | Asus TUF GT501 | Samsung C27HG70 1440p 144hz HDR FreeSync 2 | Ubuntu 20.04.2 LTS |

 

Server:-

Intel NUC running Server 2019 + Synology DSM218+ with 2 x 4TB Toshiba NAS Ready HDDs (RAID0)

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Master Disaster said:

you can do


IF %ERRORLEVEL% EQU 3 GOTO EOF
if %ERRORLEVEL% EQU 2 GOTO TWO
if %ERRORLEVEL% EQU 1 GOTO DONE

 

That's what I originally had, but I thought it had something to do with the way I wrote it (though I never had issues with that before), that's why I mentioned I had multiple iterations of those choices.

 

3 minutes ago, Master Disaster said:

Its been a while but IIRC you should do the error level checks in reverse in a batch file, so start at 3, then 2 and finally 1. Can't remember the reason but I'm sure not doing it this way can cause weird issues like you described.

Wasn't aware of that and never had issues previously, might be a good idea to switch ! Thanks to that info !

If you need help with your forum account, please use the Forum Support form !

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×