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
Related
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]
I have a batch file that detects if the user presses key A and if so should change the char variable to A instead of . But for some unknown reason that does not work. I don't know why.
Here is the code:
#echo off
set char=.
:start
cls
batbox.exe /k
if %errorlevel%==97 set %char%=A && goto next
goto start
:next
echo %char%
pause
If you need the batbox command info, here is the link.
Looks like your trying to do something like this:
#Echo off
Set Char=.
:Start
Cls
Choice /C an /T 10 /D n /N /M "Press a or wait"
Echo %Char%
Pause
Goto :start
You only have to throw in some of your own code to make it work like you want.
Quick run down from here /C is choices /T is time /D is what answer it does when the time runs out /N is for not showing the available answers and /M is message for the user.
Source: Batch Choice Information
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."
EDIT
I think i formulated myself wrong,
the sword is an item you can obtain ingame, so the value starts at 0 (FALSE).
When the player obtains the sword, the variable goes to 1 (TRUE).
Now, after the variable is true, I want to be able to use
choice /c 12 /n /m "What do you want to do?"
echo.
IF %errorlevel%==1 goto Continiue1
IF %errorlevel%==2 goto Gameover1
**IF %sword%==TRUE choice /c 3 /n /m "[SWORD] -Attack!"**
IF %sword%==TRUE choice /c 3 /n /m "[SWORD] -Attack!"
/EDIT
Please help me understand how I can return a value from a variable and use it later to check if the variable is true.
I am new to batch programming, so I'm still learning new things!
#echo OFF
:start
set sword=False
IF %sword%==True echo You have a sword!
echo.
IF %sword%==False echo You don't have a sword.
echo.
pause
I have been searching the web for two days without any luck.
Try this:
#echo off
set sword=true
:start
if %sword% equ true echo You have a sword.
if %sword% neq true echo You do not have a sword.
pause
You can do it like this :
#echo OFF
set "sword="
:start
IF defined sword (echo You have a sword!
) else (echo You don't have a sword.)
echo.
pause
Edit :
Then just use :
if not %sword%==0 goto:attack
So I want to code an optional, where the user can input y or n, shut down. And this is what I have been trying:
#echo off
echo ---WARNING---
echo.
echo DO YOU WANT YOUR COMPUTER TO SHUTDOWN? (y/n)
If /I "%Input%"=="y" goto yes
If /I "%Input%"=="n" goto no
:yes
shutdown /s
:no
pause
Am I along the right track even?
Try adding this.
set /p Input=Enter Yes or No:
I have also added a goto because if you typed something that isn't a yes or a no then it would have automatically gone to yes. Failing the below code you could add this but the code at the bottom should work.
set /P INPUT=Type input: %=%
If %INPUT%=="y" goto yes
If %INPUT%=="n" goto no
Your code should be like this:
#echo off
echo ---WARNING---
echo.
echo DO YOU WANT YOUR COMPUTER TO SHUTDOWN? (y/n)
set /p Input=Enter Yes or No:
If /I "%Input%"=="y" goto yes
goto no
:yes
shutdown /s
:no
pause
Another much simpler way to do this in two lines, without using the IF statement is this:
2 LINE EDIT:
choice /c yn /n /m "DO YOU WANT YOUR COMPUTER TO SHUTDOWN? (y/n)"
goto %ERRORLEVEL%
WHOLE CODE (WITH 2 LINE EDIT):
#echo off
echo ---WARNING---
echo.
choice /c yn /n /m "DO YOU WANT YOUR COMPUTER TO SHUTDOWN? (y/n)"
goto %ERRORLEVEL%
:1
shutdown /s
:2
pause
Explanation of choice, a built in command:
This tool allows users to select one item from a list of choices and returns the index of the selected choice. The /c yn specifies the choices the user has to choose from. The /n hides the default prompt from the user. The /m gives a custom message to the user, defined inside the " ". The second line needed is the goto %ERRORLEVEL%, which sends the code to the desired : in the code. %ERRORLEVEL% (this explanation is for the choice command only. %ERRORLEVEL% can be used in MANY other ways.) returns a number between 1 and x, where x is the total number of choices. In this example, 1 will be returned when y is pressed, and 2 will be returned when n is pressed.
EDIT:
You are kind of on track, but you just were using the wrong commands. Another way you could implement my fix is this:
#echo off
echo ---WARNING---
echo.
choice /c yn /n /m "DO YOU WANT YOUR COMPUTER TO SHUTDOWN? (y/n)"
set INPUT=%ERRORLEVEL%
if %INPUT% EQU 1 goto yes
if %INPUT% EQU 2 goto no
:yes
shutdown /s
:no
pause
For the if command, you have the C++ syntax for equals. In batch, you use EQU for equals, NEQ for not equal, LSS for less than, LEQ for less than of equal to, GTR for greater than, and GEQ for greater than or equal to. For some more in depth information, you may want to do if /? when you open command prompt up next. It is full of sooo much information.
This also removes any id10t user errors, meaning anything the user could do to try to break it or use it wrong.
~Burn
Here is how I'd do it:
#echo off
setlocal
:again
set /p ans=Do you want to do something? (y/n)
if /i "%ans:~0,1%" EQU "Y" (
Echo you selected Yes.
REM do yes stuff
) ELSE (if /i "%ans:~0,1%" EQU "N" (
Echo you selected No.
REM Do no stuff
) ELSE (
Echo You need to select yes or no only.
goto :again
)
)
That way, you catch if they don't enter either Yes or No and catch if they enter any variation of Yes or No.
Suffice to task, it will prompt the user as if they type
set DAYNUM={user value}
at the command-prompt.
for example
set /p DAYNUM=Enter DAYNUM:
It's hard to get more simple than that, there's only one command-flag not four, it doesn't depend on Command Extensions and there's little question of version-compatibility.
In terms of validation, well, the user has to put in the right parameter.
If they don't, it's wrong. Simple as that.
Echo it back to confirm with a pause and the user can control-break out of it.
Could more time be spent arguing the pros and cons of different approaches? Sure.
Is that wise? Probably not. Efficient? Certainly not. Is a 50 score required to comment on the existing answers, while no score at all is required to post a new answer? Sure.