Why is this Choice command not working (batch-file) - batch-file

I have this
#echo off
:1
cls
echo Navigation: W-Up, S-Down, E-Enter
echo _________________________________
echo -(Option 1)
echo Option 2
echo Option 3
choice /c WSE /n
if ERRORLEVEL 1 goto 3
if ERRORLEVEL 2 goto 2
if ERRORLEVEL 3 goto opt1
:2
cls
echo Navigation: W-Up, S-Down, E-Enter
echo _________________________________
echo Option 1
echo -(Option 2)
echo Option 3
choice /c WSE /n
if ERRORLEVEL 1 goto 1
if ERRORLEVEL 2 goto 2
if ERRORLEVEL 3 goto opt2
:3
cls
echo Navigation: W-Up, S-Down, E-Enter
echo _________________________________
echo Option 1
echo Option 2
echo -(Option 3)
choice /c WSE /n
if ERRORLEVEL 1 goto 2
if ERRORLEVEL 2 goto 1
if ERRORLEVEL 3 goto opt3
:opt1
cls
echo You chose Option 1
pause >nul
exit
:opt2
cls
echo You chose Option 2
pause >nul
exit
:opt3
cls
echo You chose Option 3
pause >nul
exit
What it is supposed to do is look like a selection menu, but for some reason it just constantly loops through ":1" from lines 2 to 9 it just loops over and over again, why is it doing this? How do I make it not do this?

Read choice /?.
According to that help page, errorlevel(s) are supposed to be checked in reversed order.
choice /c WSE /n
if ERRORLEVEL 3 goto opt3
if ERRORLEVEL 2 goto 2
if ERRORLEVEL 1 goto 1
Why? Because if ERRORLEVEL 1 means if %errorlevel% geq 1. Alternatively, you can do:
if %errorlevel% equ 1 goto 1
if %errorlevel% equ 2 goto 2
if %errorlevel% equ 3 goto opt3
Or even crazier(but safer) with findstr.
set errlvl=%errorlevel:~-1%
echo "%errlvl%"| findstr /l /x /i """1""" && goto :1
echo "%errlvl%"| findstr /l /x /i """2""" && goto :2
echo "%errlvl%"| findstr /l /x /i """3""" && goto :opt3
We do not really need that set statement, but a precaution
Just another reminder, if the user pressed key(s) out of the "WSE", an annoyingly loud beep will occur. You may want to check out my poorly title question to find out methods of silencing them.

Related

What am i doing wrong using the Choice command with batch?

This is not the full code, since its about 1000 lines long, but here's the problem, when i come to this section of the game
choice /c abc1 /n
when i press "a" it's supposed to "medicalbag" and instead it acts like if i were to press "1" and goes back to start
when i press "b,c,1" they all go to "medicalbag".
i can't find a solution to this, i read about the command and apparently it supports these letters and numbers, when i change them out with just numbers they work just fine, but im really not sure what im doing wrong here.
:bag
cls
echo *****************************
echo a) Medical supplies
echo b) Consumables
echo c) Weaponry
echo -----------------------------
echo 1) back
echo -----------------------------
choice /c abc1 /n
if %errorlevel% == a goto medicalbag
if %errorlevel% == b goto consumablebag
if %errorlevel% == c goto weaponrybag
if %errorlevel% == 1 goto start
:medicalbag
cls
echo *****************************
echo Bandages: %bandagecount%
echo -----------------------------
echo 1) back
echo -----------------------------
choice /c 1 /n
if %errorlevel% == 1 goto bag
:consumablebag
cls
echo *****************************
echo Canned food: %cannedfoodcount%
echo Purified water: %purifiedwatercount%
echo Dirty water: %dirtywatercount%
echo -----------------------------
echo 1) back
echo -----------------------------
choice /c 1 /n
if %errorlevel% == 1 goto bag
:weaponrybag
cls
echo *****************************
echo a) combatknife: %combatknifecount%
echo -----------------------------
echo 1) back
echo -----------------------------
choice /c a1 /n
if %errorlevel% == a goto combatknifecheck
if %errorlevel% == 1 goto bag
the errorlevel of the choice keys defined using the /c switch is returned according to the keys position in the list.
with /c abc1:
keypress: errorlevels reuturned:
a 1
b 2
c 3
1 4
To get the literal keypress, you need to use a for /f loop to capture the keypress:
For /f "delims=" %%G in ('choice /n /c abc1')Do Echo(You pressed: %%G
Note - the /n switch MUST be used when using a for loop to capture the pressed key.
Here is an example of a dynamic menu system that will return the user selected value and the description of that value.
#echo off
setlocal EnableDelayedExpansion
CALL :MENU "Medical supplies" "Consumables" "Weaponry" "Back"
CALL :CHOSE
pause
goto :eof
REM ONLY FUNCTIONS BELOW HERE
:MENU
set /a "OptionNo=0"
set "choices="
FOR %%I IN (%*) do (
set /a OptionNo+=1
call :ResolveChar !OptionNo!
set "Option!retval!=%%~I"
set "choices=!choices!!retval!"
echo [!retval!] %%~I
)
GOTO :EOF
:ResolveChar
SETLOCAL
set "keys=_ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set "_startchar=%1"
CALL SET "char=%%keys:~%_startchar%,1%%"
ENDLOCAL & SET "retval=%char%"
goto :eof
:CHOSE
choice /c !choices! /cs /m "Select Your Option: " /n
call :ResolveChar %errorlevel%
set "Option=!Option%retval%!"
echo You Selected: %retval% - %Option%
GOTO :EOF

Batch go to if statement

My if statement is:
If %choice%== 1 (goto 1)
If %choice%== 2 (goto 2)
:1
Echo 1
Pause
:2
Echo 2
Pause
It works if I choose 2 but if I choose 1 goes to 1 then 2.
How do I fix this?
If %choice% equ 1 goto opt1
If %choice% equ 2 goto opt2
:opt1
Echo option 1
Pause
goto :eof
:opt2
Echo option 2
Pause
goto :eof
or maybe:
goto opt%choice%
:opt1
Echo option 1
Pause
goto :eof
:opt2
Echo option 2
Pause
goto :eof
a better way of doing the above would however be:
If %choice% equ 1 echo opt 1
If %choice% equ 2 echo opt 2
if more conditions needs to occur:
If %choice% equ 1 (
echo opt 1
echo do other opt 1 things
)
If %choice% equ 2 (
echo opt 2
echo do something else for opt 2
)
if only 2 options exists, use if else:
If %choice% equ 1 (
echo opt 1
) else (
echo opt 2
)
Then the BEST choice option:
choice /c 12 /m "Enter Choice: "
goto :opt%errorlevel%
:opt1
echo choice 1
echo do more for opt 1
goto :eof
:opt1
echo choice 2
echo do more for opt 2
goto :eof
By adding a third goto location that bypasses 2.
If "%choice%" == "1" goto 1
If "%choice%" == "2" goto 2
:1 Echo 1
Pause
goto 3
:2 Echo 2
Pause
:3

How to validate input in batch

So i made a menu in batch and you have options to select what you would want to do, but if u enter a option thats not on the menu it selects the first option. how would i make it echo invalid input please try again.
menu:
echo Please select what you would like to do!
echo =========================================
echo 1)Example 1
echo 2)Example 2
echo 3)Example 3
echo 4)Example 4
echo 5)Example 5
echo 6)Example 6
echo =========================================
set /p ans="Please enter your selection:"
if %ans%==1 (
goto a
)
if %ans%==2 (
goto b
)
if %ans%==3 (
goto c
)
if %ans%==4 (
goto f
)
if %ans%==5 (
goto g
)
if %ans%==6 (
goto h
)
Use choice. Much simpler. Here is an example
#echo off
Choice /c 123456 /m "select choice"
If not %errorlevel% equ 0 Echo you chose %errorlevel%
You can then use it to goto by creating easy manageable labels.
#echo off
Choice /c 123 /m "select choice"
Goto opt%errorlevel%
:opt3
Echo do something here for option 3
Goto :eof
:opt2
Echo do something here for option 2
Goto :eof
:opt1
Echo do something for oprion 1
:opt0
Exho You pressed ctrl+c and selected N
....
You get the idea..

Batch-file wrong input fix?

#echo off
:start
cls
color e
echo YOU HAVE WON $1,000,000! WHAT WILL YOU DO?
echo.
echo =================
echo -Take it (1)
echo -Leave it (2)
echo -Double it (3)
echo =================
echo.
set /p INPUT=Please specify your answer:
If /i "%INPUT%" == "1" goto 1
If /i "%INPUT%" == "2" goto 2
If /i "%INPUT%" == "3" goto 3
If /i "%INPUT%" == "Jonah" goto Jonah
If /i "%INPUT%" == "" goto Wrong
I have a strange problem. Above is a code and for some reason, when I type in "INPUT" something like 'Aiden' it would think it meant '1'. Is there a way to make every wrong answer goto wrong. (Wrong answers like Aiden which isn't even specified there. But not only Aiden, any other thing).
After your if statements you need to put a catch-all GOTO statement so if none of the others work, it will go to wrong instead of just continuing into the block right below
#echo off
:start
cls
color e
echo YOU HAVE WON $1,000,000! WHAT WILL YOU DO?
echo.
echo =================
echo -Take it (1)
echo -Leave it (2)
echo -Double it (3)
echo =================
echo.
set /p INPUT=Please specify your answer:
If /i "%INPUT%" == "1" goto 1
If /i "%INPUT%" == "2" goto 2
If /i "%INPUT%" == "3" goto 3
If /i "%INPUT%" == "Jonah" goto Jonah
goto Wrong

Using the goto command as a fallback in a batch file

How can I make it so that when the folder can't be found, the code will use the goto command?
Here's my code:
:T
Echo Folder is Already Unlocked
:CODE
if attrib -h -s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309B}" goto T
ren "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309B}" Codes
call :c 09 " Codes Unlocked successfully
goto UNLOCK
Here's some examples - given as "answers" since the formatting is easier to use
#ECHO OFF
SETLOCAL
:: This one forces errorlevel 0
echo x|find "x" >nul
if errorlevel 1 (echo errorlevel is 1 or greater) else (echo errorlevel is 0)
:: This one forces errorlevel 1
echo y|find "x" >nul
if errorlevel 1 (echo errorlevel is 1 or greater) else (echo errorlevel is 0)
:: Now some multi-line responses
:: This one forces errorlevel 0
echo x|find "x" >nul
if errorlevel 1 (
echo errorlevel is 1 or greater
echo I say again - errorlevel 1 1 or greater
echo I say a third time - errorlevel 1 1 or greater
) else (
echo errorlevel is 0
echo again errorlevel is 0
echo once again errorlevel is 0
)
:: This one forces errorlevel 1
echo y|find "x" >nul
if errorlevel 1 (
echo Y-errorlevel is 1 or greater
echo I say again - errorlevel 1 1 or greater for Y
echo I say a third time - errorlevel 1 1 or greater - Y
) else (
echo Y-errorlevel is 0
echo Y-again errorlevel is 0
echo Y-once again errorlevel is 0
)
:: conditional modification of variable
set /a var=22
echo x|find "x" >nul
if errorlevel 1 (set /a var=%var%+10) else (set /a var=%var%-10)
echo after testing x, var is %var%
set /a var=22
echo y|find "x" >nul
if errorlevel 1 (set /a var=%var%+10) else (set /a var=%var%-10)
echo after testing y, var is %var%
:: You'll need to change these lines to files/directories that exist or don't exist
set "fileexists=c:\pdoxusrs.net"
set "direxists=c:\users"
set "filemissing=c:\missing.net"
set "dirmissing=c:\nothere"
for %%a in ("%fileexists%" "%direxists%" "%filemissing%" "%dirmissing%") do (
if exist "%%~a" (echo "%%~a" exists) else (echo "%%a" missing)
)
goto :eof
Now if errorlevel isn't being set to 0/non-zero as you appear to be expecting, then try using if exist instead. Probably easier that way, actually. Unfortunately "doesn't work" simply means nothing - we'd need to know exactly what happened, and we'd need you to edit your original posting to add-in your current code (or at least, a representative portion.)

Resources