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.
Related
i'm trying to make a program that lets you write something like a book or a todo list
this i an idea i just had
SET /P line1=""
SET /P adlne=[add another line? Y or N?]
IF "adlne"=="Y"
GOTO :a
IF "adlne"=="N"
GOTO :b
:a
SET /P line2=""
SET /P adlne=[add another line? Y or N?]
IF "adlne"=="Y"
GOTO :a1
IF "adlne"=="N"
GOTO :b
:b
set list = "line1, line2, line3"
(for %%a in (%list%) do (
echo %%a
echo/
)) > theFile.txt
pause
i expect it to take the input and when you awnser "N" is takes the input variables and put them in a file but when i awnser "N" it stops asamething with "Y"
a Better version maybe? It would seriously be a tedious tool to use and it makes no sense at all, seeing as there are 100s of great editers out there, but anyhow:
#echo off
choice /C CA /M "Clean file or append to previous notes? "
if "%errorlevel%"=="1" type nul>theFile.txt
:write
set /P line=line :
echo %line% >>theFile.txt
echo/>>theFile.txt
choice /C YN /M "add another line? "
if "%errorlevel%"=="1" goto :write
pause
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 trying to just make a casual conversation using notepad as a batch file creator but whatever I search and try, it keeps saying the syntax of the command is wrong.I was just making it and went to test it and it came up. I have tried doing other stuff on different lines and then set /p ... but it never works. What am I doing wrone; I just want to continue on my just-for-fun project. Thanks:
%echo off
set /p name="Hello, what's your name?"
set /p S?1="Oh, well: hello %name%! Is that what you would like me to call you? (Y/N)"
if /i "%S?1?%" EQU "Y" goto :Yes
if /i "%S?1%" EQU "N" goto :No
goto :No answer
:Yes
echo Okay! Just making sure so I do not get on your nerves!
goto :Next1
:No
set /p name="Oh, then what shall I call you, then?"
echo Oh, alright. I will cal you %name% from now on! Sorry.
goto :Next1
:No answer
echo Sorry, but I do need an answer.
timeout 1
:No answer again
set /p S?2=So, is that what you would like me to call you? (Y/N)
if /i "%S?2%" EQU "Y" goto :Yes
if /i "%S?2%" EQU "N" goto: No
goto :No answer again
:Next1
pause
Get rid of the extra question mark on line four, (typo).
Then change your labels.
Labels are single strings without spaces, so some of them are returning to :No because it sees "No", "No Answer" and "No Answer Again" as the same.
I'd suggest you change them perhaps to :No, :Answer and :Again.
I can't fix this I want to be able to write anything as a response which works with
:being
but not
:Fap
here is my code
:begin
cls
echo You wake up and realize you are never going to amount to anything, oh well, might as well get on with your worthless life
echo fap or vidya games
echo.
set /p input=
if /i "%input%"=="Fap" goto Fap
if /i "%input%"=="Vidya games" goto vidya games
if /i "%input%"=="vidya" goto vidya games
if not "%input%"=="Fap"/"vidya"/"Vidya games" goto begin
:Fap
cls
echo Since you can't get a girl you decide to fantize about the girl of your dreams so you download some watamote doujins after a mixture of crying and masterbaiting you decide to change to mood
echo vidya or sleep
echo.
set /p input=
if /i "%input%"=="vidya" goto vidya fap
if /i "%input%"=="sleep" goto sleep
if not "%input%"=="vidya"/"sleep" goto Fap
Labels in batch scripts are marked by a colon like :begin. Same approach is used when redirecting to a certain label from IF, FOR or similar statement. So, edit your script by following this example:
if /i "%input%"=="Fap" (goto :Fap
) else if /i "%input%"=="Vidya" (goto :Vidya_games
) else (goto :begin)
Instead of those many ifs, you can check, if a label exists and if yes, jump to it:
If you have several questions with different destinations, you can name your labels accordingly:
#echo off
:loop
echo fap or vidya games
set /p "label=Command: "
findstr /b /i ":Q1-%label%" "%~f0" && goto :Q1-%label%
REM previous line checks, if the label exists and if yes, jumps to it
echo no such command: %label% & goto :loop
:Q1-fap
echo reached Question1 label one.
goto :eof
:Q1-vidya
echo reached Question1 label two.
goto :eof
Some notes:
labels are single word only. Everything after the first space is ignored.
findstr /i makes it insensitive to capitalization.
/b searches for lines that start with the string.
&& means "if previous command (findstr here) was successful, then"
%~f0 is the name of your batchfile.
This goes to Q1-vidya when you enter vidya, Vidya, VIDYa, vidya Games or Vidya anything.
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