Batch go to if statement - batch-file

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

Related

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..

Why is this Choice command not working (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.

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

Batch file RPG game error

#echo off
TITLE Zombie Warrior
setlocal enabledelayerdexpansion
:new
set playerdmg= 23
set zombiedmg= 24
set coin= 0
set rewards= 10
set level= 0
goto refresh
:refresh
set health=100
set zombiehealth=200
set zombie2health=400
goto menu
:menu
cls
echo.
echo Zombie Warrior
echo Coins = %coin%
echo.
echo 1) Play!
echo 2) Exit.
echo 3) Shop
echo.
set /p c=C:\
if "%c%" == "1" goto home
if "%c%" == "2" exit
if "%c%" == "3" goto shop
goto menu
set health=100
set zombiehealth=200
:home
cls
echo Welcome to the game!
echo -+-+-+-+-+-+-+-+-+-+-+-+-
echo Coins: %coin%
echo Level: %level%
echo.
echo 1) FIGHT!
echo 2) Quit
set /p c=C:
if "%level%" == "1" (
if "%c%" == "1" goto encounter2
)
if "%level%" == "0" (
if "%c%" == "1" goto encounter1
)
if "%c%" == "2" goto menu
goto home
set health=100
set zombiehealth=200
:encounter1
cls
echo You: %health%
echo Zombies Level 1: %zombiehealth%
echo Your damage: %playerdmg%
echo.
echo 1) Attack
echo 2) Run!
echo.
set /p c=C:\
if "%c%" == "1" goto attack1
if "%c%" == "2" goto refresh
goto encounter1
:encounter2
cls
echo You: %health%
echo Zombies Level 2: %zombie2health%
echo Your damage: %playerdmg%
echo.
echo 1) Attack
echo 2) Run!
echo.
set /p c=C:\
if "%c%" == "1" goto attack2
if "%c%" == "2" goto refresh
goto encounter2
:attack2
set /a zombie2health-=playerdmg
set /a health-=zombiedmg
if %zombie2health% lss 0 goto win1
if %health% lss 0 goto lose
if !zombiehealth! lss 30 set /a coin+=5
goto encounter1
:attack1
set /a zombiehealth-=playerdmg
set /a health-=zombiedmg
if %zombiehealth% lss 0 goto win1
if %health% lss 0 goto lose
if !zombiehealth! lss 30 set /a coin+=5
goto encounter1
:win1
set /a coin+=rewards
set /a level+=1
if level == 1 set /a coin+=10
goto refresh
:lose
cls
echo You lost :(
pause
goto refresh
:shop
cls
echo What would you like to buy? (type q to quit)
echo Coins: %coin%
echo 1) Baseball bat
echo.
set /p c=C:\
if "%c%" == "1" goto bat1
if "%c%" == "q" goto menu
:bat1
if %coin% lss 30 goto nope
set /a playerdmg+=5
set /a coin-=30
goto menu
:nope
echo You don't have enough coins!
pause
goto menu
So that is my code. What I'm trying to make is basically if I am level 1 or 0, it will make me go somewhere else.
For example if I am level 0
goto encounter1
if I am level 3
goto encounter4
But I don't know how to do that. I think I'm close in this line:
if "%level%" == "1" (
if "%c%" == "1" goto encounter2
)
if "%level%" == "0" (
if "%c%" == "1" goto encounter1
)
Your problem is stemming from these lines:
set playerdmg= 23
set zombiedmg= 24
set coin= 0
set rewards= 10
set level= 0
All spaces are significant in a SET statement, so your values include a leading space. Your if "%level%" == "0" ( statement expands to if " 0" == "0" (, which of course is false.
Remove the leading space from the SET statements, and your immediate problem should be solved. (I haven't evaluated your entire code base to see if there are other problems)
A better way to structure your SET statements is to enclose the entire expression in quotes. The quotes mark the end of the assignment, but are not included in the value. This prevents inadvertent trailing spaced from being included in the value.
set "playerdmg=23"
set "zombiedmg=24"
set "coin=0"
set "rewards=10"
set "level=0"

Batch file that accepts 5 positive integers range is 0-9 and display the output in descending order non integers are not accepted

i have the code here but i dont know the logic on how the inputs will sort to descending order..
#echo off
:start1
cls
set /p num1=Enter #1:
if [%num1%]==[] goto error1
if %num1% LSS 0 goto a
if %num1% GTR 9 goto a
:start2
cls
set /p num2=Enter #2:
if [%num2%]==[] goto error2
if %num2% LSS 0 goto b
if %num2% GTR 9 goto b
:start3
cls
set /p num3=Enter #3:
if [%num3%]==[] goto error3
if %num3% LSS 0 goto c
if %num3% GTR 9 goto c
:start4
cls
set /p num4=Enter #4:
if [%num4%]==[] goto error4
if %num4% LSS 0 goto d
if %num4% GTR 9 goto d
:start5
cls
set /p num5=Enter #5:
if [%num5%]==[] goto error5
if %num5% LSS 0 goto e
if %num5% GTR 9 goto e
::Equations.........
::Error Trapping
:error1
echo No input!
pause
goto start1
:error2
echo No input!
pause
goto start2
:error3
echo No input!
pause
goto start3
:error4
echo No input!
pause
goto start4
:error5
echo No input!
pause
goto start5
:a
echo Input must be in the range of 0 to 9
pause
goto start1
:b
echo Input must be in the range of 0 to 9
pause
goto start2
:c
echo Input must be in the range of 0 to 9
pause
goto start3
:d
echo Input must be in the range of 0 to 9
pause
goto start4
:e
echo Input must be in the range of 0 to 9
pause
goto start5
:exit
pause
exit
you might try this:
#echo off &setlocal
for /f "delims==" %%a in ('set "$num" 2^>nul') do set "%%a="
:loop1
set /a count+=1
:loop2
set /p "$num%count%=Enter #%count%: "
call echo %%$num%count%%%|findstr /r "^[0-9]$" >nul|| (echo Error!&goto:loop2)
if %count% lss 5 goto:loop1
(for /f "tokens=2 delims==" %%a in ('set "$num"') do #echo %%a)|sort /r

Resources