I need the choice command to do the same thing as this block of code:
set /p start= Select Mode:
if %start% == 1 goto money
if %start% == 2 goto payouts
if %start% == 3 goto tutorial
if %start% == 4 exit /b
I have been trying for quite some time now and cannot figure it out Thanks in advance!
The simplest solution is not check the value of errorlevel at all, but directly use it in a goto command. In this way, you avoid the usual series of if commands, so the code is simpler.
choice /C 1234 /M "Select Mode: "
goto option-%errorlevel%
The rest of the code shoud be like this:
:option-1 money
echo Money
goto loop
:option-2 payouts
echo Payouts
goto loop
:option-3 tutorial
echo Tutorial
goto loop
:option-4
exit /B
Using the command choice like this
choice /c 1234 /M "Select Mode: "
upon exit, errorlevel will be set to the index (starting with 1) of the selected choice. For acting upon the errorlevel it's important to remember that "if errorlevel n" traps not only n, but also all higher values, meaning that they need to be specified in reverse
if errorlevel 4 goto exit
if errorlevel 3 goto tutorials
if errorlevel 2 goto payout
if errorlevel 1 goto money
An alternative to the answer by fvu, is to use the %ErrorLevel% variable as Squashman suggested:
Choice /C 1234 /M "Select Mode: "
If %ErrorLevel%==1 GoTo money
If %ErrorLevel%==2 GoTo payouts
If %ErrorLevel%==3 GoTo tutorial
Exit/B
[:money | :payouts | :tutorial]
Related
I want to put a 30s time limit for the :choice Y/N/P and after the time is up goto :start
The code I have need help for the timeing thing
#echo off
:start
echo AmishCraft will start
TIMEOUT /T 5
echo (%time%)
java -Xms2048M -Xmx4096M -jar server.jar
call C:\Program Files (x86)\Common Files\Oracle\Java\javapath\java.exe
ping 1.1.1.1 -n 1 -w 3000 >nul
:choice
set /P a=do you want to restart? Yes No Pause [Y/N/P]?
if /I "%a%" EQU "Y" goto :restart
if /I "%a%" EQU "N" goto :stop
if /I "%a%" EQU "P" goto :pause
goto :start
:restart
cls
echo server will restart
cls
goto :start
:stop
cls
echo closing server
TIMEOUT /T 5
exit
cls
echo server is paused
:pause
:choice
set /P a=do you want start? Restart Stop [R/S]?
if /I "%a%" EQU "R" goto :restart
if /I "%a%" EQU "S" goto :stop
goto :start
pause
/T is the timeout switch for choice.
/D is the switch to define the default errorlevel / option to set if the
time Elapses.
Example:
CHOICE /T 5 /N /C 1234 /M "Select Option 1,2,3 or 4" /D 1
Applies a timeout of 5 seconds, with the errorlevel being set to option 1, equal to errorlevel 1 in this instance.
/N Hides the default Choice Prompt String.
/M Allows you to Define your own Prompt string
/C Allows alphanumerical characters to be defined as Choice options
Note:
Errorlevel is Set from Left to Right with regards to listed options.
After the Choice Command Errorlevel Needs to be Assessed From Highest to lowest
OR
Used Directly; Such as in a Goto :LabelName%errorlevel% Command
* Response to comment *
CHOICE /C 123 /T Timeout 25 /D goto :start /M 1 choice menu 25s
IF %ERRORLEVEL% EQU 1 goto :choice1
There are multiple errors in the above.
/T Timeout 25 should be: /T 25
Timeout is implicit in the /T switch and does NOT form a part of correct usage of the choice command.
/D goto :start should be: /D 1 OR /D 2 OR /D 3
Only the defined /C options should be used following the /D switch
/M 1 choice menu 25s is incorrect.
The prompt after /M should be encased in Doublequotes: "[1] Option 1. [2] Option 2. [3] Option 3."
Errorlevel Assessment should be done on the Line After the CHOICE Command.
Again, to be clear, Assesment should be done from Highest to Lowest. When errorlevel is Assessed following Choice it is actually interpreted as If ERRORLEVEL GTR n , Despite being scripted Using If ERRORLEVEL n
An example of the Correct usage of all of the above:
#echo off
:menu
cls
CHOICE /N /T 25 /C 123 /M "[1] Option 1. [2] Option 2. [3] Start." /D 3
IF ERRORLEVEL 3 (
GOTO :start
) else (
GOTO :choice%errorlevel%
)
:start
ECHO( You are at the start
Pause
GOTO :menu
:choice1
ECHO( You are at option 1
Pause
GOTO :menu
:choice2
ECHO( You are at option 2
Pause
GOTO :menu
Okay I am trying to ask the user the below question in a batch file but don't think that I am entering the correct choice command.
echo Would you like to know the time? (Y/N)
CHOICE /C YN /N
GOTO OPTION-%ERRORLEVEL%
:OPTION-Y Yes
echo %time%
goto cont
:OPTION-N No
:cont
P.S today is my first day of the couse so I am a newbie, please don't judge.
Because %errorlevel% is a number not Y or N
Your labels should be :OPTION-1 and :OPTION-2:
#echo off
echo Would you like to know the time? (Y/N)
CHOICE /C YN /N
GOTO OPTION-%ERRORLEVEL%
:OPTION-1
echo %time%
goto cont
:OPTION-2
:cont
Here is another example so you can understand how it assigns the %errorlevel% number to the key you selected.
#echo off
:start
cls
CHOICE /C YNM /N /M "Should I display the Time? Select (Yes (Y) No (N) or Maybe (M))"
if %errorlevel%==1 echo %time%
if %errorlevel%==2 echo Ok, I won't then
if %errorlevel%==3 echo it is fine, I will ask again in 10 seconds & timeout /T 10 & goto :start
Here you can see it assigns the first key to %errorlevel% 1, the second key to %errorlevel% 2 and third key to %errorlevel% 3 etc.
CHOICE does not return the selected key as %ERRORLEVEL%, it returns the index of the selected key - that is, for CHOICE /C YN, if you select Y, %ERRORLEVEL% will be 1; for N, it will be 2. See SS64 on CHOICE.
You also have to be careful about the order that you test %ERRORLEVEL%; the standard construct IF ERRORLEVEL n ... is actually testing to see whether %ERRORLEVEL% is equal to or greater than n. See SS64 on ERRORLEVEL.
You could also reduce all of your provided snippet to two lines, (continuing your script beneath them as necessary):
Choice /M "Would you like to know the time"
If Not ErrorLevel 2 Echo %TIME% & Timeout 3 >Nul
I am a dabbler in batch files so my knowledge is limited to my experiences. What I am trying to do is limit the "Y or N" inputs to just that Y or N. Right now you can put anything in the fields and the code progresses. What I am attempting to do is create a hotspot using a batch file. I have yet to figure out how to "save" the created network but that isn't really an issue.
I have included what I have, the lines being the start and finish, If anyone happens to see anything that can be improved upon or made less bulky feel free to comment.
#echo off
:: BatchGotAdmin
:-------------------------------------
REM --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
pushd "%CD%"
CD /D "%~dp0"
:--------------------------------------
#echo off
:SSID
set /P inputA="Input desired Network SSID:"
echo.
set /P c=Is %inputA% correct? [Y/N]?
echo.
if /I "%c%" EQU "Y" goto :PSWD
if /I "%c%" EQU "N" goto :SSID
:PSWD
set /P inputB="Input desired 8 to 63 character Network Password:"
echo.
set /P c=Is %inputB% correct? [Y/N]?
echo.
if /I "%c%" EQU "Y" goto :SETUP
if /I "%c%" EQU "N" goto :PSWD
:SETUP
netsh wlan set hostednetwork mode=allow ssid=%inputA% key=%inputB% >NUL
#echo Creating Network...
echo.
timeout /t 5 /nobreak > NUL
#echo Network Created!
echo.
timeout /t 1 /nobreak > NUL
set /P c=Would you like to start your new Network? [Press "Y" to continue/Press "N" to abort]
if /I "%c%" EQU "Y" goto :START
if /I "%c%" EQU "N" goto :BYE
:START
netsh wlan start hostednetwork
timeout /t 5 /nobreak > NUL
#echo Your Network has started!
pause
:BYE
Exit
Instead of using set /p, use the choice command. I, personally, would use:
choice /m Correct?
if %errorlevel% equ 1 goto PSWD
if %errorlevel% equ 2 goto SSID
This will display: Continue? [Y/N]?. If the hit y, it will go to :PSWD. If they hit n, it will go to :SSID.
The help section of the choice command (brought up in Command Prompt by choice /?)
CHOICE [/C choices] [/N] [/CS] [/T timeout /D choice] [/M text]
Description:
This tool allows users to select one item from a list
of choices and returns the index of the selected choice.
Parameter List:
/C choices Specifies the list of choices to be created.
Default list is "YN".
/N Hides the list of choices in the prompt.
The message before the prompt is displayed
and the choices are still enabled.
/CS Enables case-sensitive choices to be selected.
By default, the utility is case-insensitive.
/T timeout The number of seconds to pause before a default
choice is made. Acceptable values are from 0 to
9999. If 0 is specified, there will be no pause
and the default choice is selected.
/D choice Specifies the default choice after nnnn seconds.
Character must be in the set of choices specified
by /C option and must also specify nnnn with /T.
/M text Specifies the message to be displayed before
the prompt. If not specified, the utility
displays only a prompt.
/? Displays this help message.
NOTE:
The ERRORLEVEL environment variable is set to the index of the
key that was selected from the set of choices. The first choice
listed returns a value of 1, the second a value of 2, and so on.
If the user presses a key that is not a valid choice, the tool
sounds a warning beep. If tool detects an error condition,
it returns an ERRORLEVEL value of 255. If the user presses
CTRL+BREAK or CTRL+C, the tool returns an ERRORLEVEL value
of 0. When you use ERRORLEVEL parameters in a batch program, list
them in decreasing order.
Examples:
CHOICE /?
CHOICE /C YNC /M "Press Y for Yes, N for No or C for Cancel."
CHOICE /T 10 /C ync /CS /D y
CHOICE /C ab /M "Select a for option 1 and b for option 2."
CHOICE /C ab /N /M "Select a for option 1 and b for option 2."
I'm trying to make a simple text-based game and am trying to design fighting mechanics. Right now, once you enter a fight you have to either type "1" to attack or "2" to enter the fightmenu. I want to change it so that you can just press space to do a regular attack, but still be able to type "1" to open the fightmenu. Here is the code so far if you're interested:
:fighting
cls
set /a yourhit=%random% %%dmg%
set /a theirhit=%random% %%theirdmg%
set /a armor = %armor% - %theirhit%
if %theirhit% gtr %armor% set /a armor = 0 & set /a hp = %hp% - %theirhit%
if %yourhit% geq %theirhp% goto winner
if %theirhit% gtr %hp% goto loser
set /a theirhp = %theirhp% - %yourhit%
echo.
call :fightheadsup
echo - You hit them for %yourhit%!
echo.
echo - They hit you for %theirhit%!
echo.
echo 1.Continue Attacking
echo 2.Return to Fight Menu
echo.
set /p input12=Enter:
if %input12% equ 1 goto fighting
if %input12% equ 2 goto fightmenu
goto fightmenu
Enclose compared values in a pair of "double quotes" as follows (get used that always as a value could be empty or could contain a blank space character, in particular a value entered by a user):
if "%input12%" equ " " goto fighting
if "%input12%" equ "1" goto fightmenu
Another approach: set a default value in advance (self-explained in code):
echo.
echo 1. Continue Attacking ^(default choice, hitting ^<Enter^> suffices^)
echo 2. Return to Fight Menu
echo.
set "input12=1"
set /p "input12=Your entry [default=%input12%]:"
if "%input12%" equ "1" goto fighting
rem superabundant: if "%input12%" equ "2" goto fightmenu
goto fightmenu
Note proper escaping <, >, ( and ) characters in echo 1. ... command
In this type of applications is preferable to use choice command instead of set /P, because CHOICE get one key and continue without need to press Enter key:
choice /C 12 /N /M "Enter: "
if %errorlevel% equ 2 goto fightmenu
if %errorlevel% equ 1 goto fighting
The disadvantage of CHOICE command is that it can not read a space key. However, in important advantage of CHOICE over SET /P is that in CHOICE command you never will get a wrong input, so you may directly use CHOICE answer this way:
choice /C 12 /N /M "Enter: "
goto option-%errorlevel%
. . .
:option-1 fighting
. . .
:option-2 fightmenu
. . .
I do something like this:
echo 1-exit
echo 2-about
echo 3-play
choice /c 123 >nul
if errorlevel 1 goto exit
if errorlevel 2 goto about
if errorlevel 3 goto play
:play
blah
:about
blah
:exit
cls
If I select the "play" option, it exits. How do I prevent this from happening?
The if errorlevel expression evaluates to true if actual error level returned by choice is greater or equal to given value. So if you hit 3, the first if expression is true and script terminates. Call help if for more information.
There are two simple workarounds.
First one (better) - replace if errorlevel expression with actual comparision of %ERRORLEVEL% system variable with a given value:
if "%ERRORLEVEL%" == "1" goto exit
if "%ERRORLEVEL%" == "2" goto about
if "%ERRORLEVEL%" == "3" goto play
Second one - change order of comparisions:
if errorlevel 3 goto play
if errorlevel 2 goto about
if errorlevel 1 goto exit
The easiest way to solve this problem is to use the %errorlevel% value to directly go to the desired label:
echo 1-exit
echo 2-about
echo 3-play
choice /c 123 >nul
goto option-%errorlevel%
:option-1
rem play
blah
:option-2
rem about
blah
:option-3
exit
cls