I cant get my batch mini-game to work - batch-file

I have problems with this code, it should only perform an action when the correct input is given.
enter code here
#echo off
echo I want to play a game. Do you?
set /P INPUT=[Y/N]: %=%
If %INPUT%=="Y" goto YES
If %INPUT%=="y" goto YES
If %INPUT%=="N" goto NO
If %INPUT%=="n" goto NO
:NO
echo FOOL!
goto end
:YES
echo Good, good...
goto end
:end
PAUSE
But the input i give doesnt make a difference, also letters like "h" will trigger an reaction. It will perform the "NO" echo everytime. This is because its the first code after the choise section. Does anybody have an idea how to fix this?

You missed the quotes:
If "%INPUT%"=="Y" goto YES
If "%INPUT%"=="y" goto YES
If "%INPUT%"=="N" goto NO
If "%INPUT%"=="n" goto NO
Else you compare Y with "Y".

After
If "%INPUT%"=="n" goto NO
you need to
goto somewhere
otherwise, cmd simply continues processing, line by line, until it reaches a goto or exit or end-of-file.
:somewhere should be just before ypu do you want to play prompt.
BTW, you can use
if /i ".....
to make the comparison case-insensitive, and
"%input:~0,1%"
will return the first character of the string (so a response like "yello" will return y)

Well in order to achieve what you want the following modifications are needed for that to happen.
Here is the original code you programmed:
#echo off
echo I want to play a game. Do you?
set /P INPUT=[Y/N]: %=%
If %INPUT%=="Y" goto YES
If %INPUT%=="y" goto YES
If %INPUT%=="N" goto NO
If %INPUT%=="n" goto NO
:NO
echo FOOL!
goto end
:YES
echo Good, good...
goto end
:end
PAUSE
Here is the modifications that I made to the code to make it run more smoothly:
echo off
goto :menu
cls
:menu
cls
echo I want to play a game. Do you?
echo[
set /P INPUT=[Y/N]: %=%
If %INPUT% equ Y goto :YES
If %INPUT% equ y goto :YES
If %INPUT% equ N goto :YES
If %INPUT% equ n goto :YES
else goto :NO
:NO
cls
echo FOOL!
pause
goto end
:YES
echo Good, good...
goto end
:end
I am assuming that based on the question that you asked that you want the following inputs (Y,y,N,n) to generate a result and anything else will go to :NO section of the code? Your question wasn't really detailed.

Related

How to set the input to take the first one or first two or first three characters in batch?

I am trying to figure out how the "%var:~,1%" setion of the provided code work.
I thought "%var:~,1%" would accept the first character correct and ignore everything else after. "%var:~,2%" would accept only the first two correct characters and so on. In this example "Y" would suffice for "YES". "NO" would suffice for "NOO" and "CLS" would suffice for "CLS"
What happens is only option three "CLS" is a valid option. I can change YES and NOO to "%var:~,3%" so that they are valid options. But how would have if /I "%var:~,1%" EQU "YES" goto :yes accept a one character input?
:start
set /p var=is this a yes or no question?
if /I "%var:~,1%" EQU "YES" goto :yes
if /I "%var:~,2%" EQU "NOO" goto :no
if /I "%var:~,3%" EQU "CLS" goto :cls
echo not a valid choice
goto :start
:yes
echo this is YES but you only have to type first letter correct
pause
goto :start
:no
echo this is NO but you have to type the first two letters correct
pause
goto :start
:cls
echo this will CLS but you have to type the first three letters correct
pause
cls
goto :start```
#TripeHound already explained in a comment that you are testing a single character against a word. it should just be if /i "%var:~0,1%"=="y".
a much better method however is to use choice
#echo off
:start
choice /C YNC /M "Press Y for Yes, N for No or C for Cancel.
goto :%errorlevel%
:3
echo this will CLS but you have to type the first three letters correct
pause
cls
goto :start
:2
echo this is NO but you have to type the first two letters correct
goto :start
:1
echo this is YES but you only have to type first letter correct
goto :start
If you are determined to not use choice this will work similarly using set /p by using only the first character of the word the user inputs.
#echo off
:start
set /p var=is this a yes or no question?
if /i not "%var:~0,1%"=="y" if /i not "%var:~0,1%"=="n" if not "%var:~0,1%"=="c" echo Incorrect choice & goto :start
goto :%var:~0,1%
:c
:C
echo this will CLS but you have to type the first three letters correct
pause
cls
goto :start
:n
:N
echo this is NO but you have to type the first two letters correct
goto :start
:y
:Y
echo this is YES but you only have to type first letter correct
goto :start

GOTO and IF statements aren't working correctly

I have a batch-game where there's some questions for one person in particular, I always change the file when she(the person) isn't nearby so its kind of a surprise.
The thing is I keep on displaying questions of different types but sometimes the GOTO or IF statements just simply doesn't work properly, it just follow the line or exit the file.
#ECHO OFF
setlocal EnableDelayedExpansion
ECHO.
ECHO "i just skipped all this echo for you"
pause>nul
:choice
cls
set /p c=Question 1[y/n]
if /I "%c%" EQU "Y" goto :somewhere
if /I "%c%" EQU "N" goto :somewhere_else
goto :choice
:somewhere
cls
cd "-somewhere in my pc-"
start "-the file-"
cls
goto :somewhere1
:somewhere1
cls
echo.
echo "skip"
pause>NUL
goto :somewhere2
:somewhere2
cls
echo.
set /p c1=question 2
if /I "!c1!"== "option1" goto :correct
if not /I "!c1!"== "option1" goto :somewhere_else1
:correct
cls
echo.
echo "skip"
echo.
echo enter to exit
pause>nul
exit
:somewhere_else1
cls
echo.
echo bad answer
pause>nul
goto :somewhere2
Here are my problems:
When it ask's you the question
:choice
cls
set /p c=Question 1[y/n]
if /I "%c%" EQU "Y" goto :somewhere
if /I "%c%" EQU "N" goto :somewhere_else
goto :choice
it's just work properly, it wasn't working in the past but I magically fixed it with no idea what i'm doing.
but then I just wanted to make it bigger and add the next lines:
:somewhere2
cls
echo.
set /p c1=question 2
if /I "!c1!"== "option1" goto :correct
if not /I "!c1!"== "option1" goto :somewhere_else1
and now the file is executing perfectly the ":correct" part but no the ":somewhere_else1". in the case of ":somewhere_else1" its just exit the file. in the past was exiting the file the right one too but again I fixed it just writing it again.
The problem is your line if not /I "!c1!"== "option1" goto :somewhere_else1
The /I needs to be before not
if /I not "!c1!"== "option1" goto :somewhere_else1
Though I should also mention that your use of ! instead of % is unnecessary in this example.
You should have a read through this link to get some insight into how to fix these errors yourself in the future, and perhaps this link on good practice.

All my batch file questions come up that the syntax is incorrect

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.

What's Wrong With My Batch File

Trying to create a batch file to make these tasks run faster.
Can someone please correct my errors or suggest a better way to write this script
Basically everytime I run it says "request timed out"
#echo off
color 0a
Title
:Beginning
set /p UserInput = What Would You Like To Start?
echo.
if %UserInput% == 1 goto :Windows Update
if not %UserInput% == "" goto :Exit
else goto :Exit
if %UserInput% == 2 goto :Group Policy Update
if not %UserInput% == "" goto :Exit
else goto :Exit
if %UserInput% == 5 goto :Favorites
if not %UserInput% == "" goto :Exit
else goto :Exit
if %UserInput% == 3 goto :Tools
if not %UserInput% == "" goto :Exit
else goto :Exit
if %UserInput% == 4 goto :Printer
if not %UserInput% == "" goto :Exit
else goto :Exit
:Windows Update
start C:\Windows\system32\wuapp.exe
pause
exit
:Group Policy Update
start gpupdate.exe
pause
exit
:Favorites
move %userprofile%\favorites\*.* G:\
pause
exit
:Tools
start \\NoneOFyourBusiness
pause
exit
:Printer
start iexplore.exe http://www.Google.com
pause
exit
:Exit
set /p beginning == Return To The Start?
echo.
echo Y=Yes or N=No
if %beginning% == "Y" goto :Beginning
if not %beginning% == "N" goto :Exit 2
:Exit 2
pause
exit**'
set /p UserInput = What Would You Like To Start?
Will apply the input string to a variable named UserInputSpace
Batch is sensitive to spaces on both sides of the = in a set (but not a set /a)
if not %UserInput% == "" goto :Exit
Because you have no control over the use's input, that may contain spaces and other character to which batch is sensitive.
use
if not "%UserInput%"=="" goto :Exit
Note that the two strings must be exactly equal. IF /i ... will make the test case-insensitive.
Note also that responding simply Enter to a set /p will leave the variable unchanged, it will not "set" it to an empty string. If you want that, use
set "var="
set /p var=
Also, to detect whether var has been set, use
if defined var
if "%UserInput%"=="1" goto :Windows Update
This is a fail-to-fail scenario. The real command executed will be
if "%UserInput%"=="1" goto Windows
The remaining text after the space-separator is documentation.
Not a good idea IMHO to use the colon in a goto. It works, but there is a difference with a CALL statement. CALL :something will execute an internal subroutine (ie subroutine in this batch file) named something, whereas CALL something without the colon will call an external executable. By analogy, GOTO should also not be used with a colon. The one exception is the documented special condition, GOTO :EOF where the colon is required. GOTO :EOF means 'go to the end of the physical batch file.The labelEOFneed not (indeed **should** not) be included in the batch.CMD` knows where it has to go...
So - fix those and it should work a lot better. Not sure what the error response you have is, but I'd suggest it's some utility you'd not expected to run getting tired of waiting for input.
Try this...
color 0a
Title Testing
REM Since exit is a keyword - changed to leave
:Begining
echo choose an option
choice /c 12345
rem set /p UserInput = What Would You Like To Start?
echo.
if %errorlevel% == 1 goto WindowsUpdate
if %errorlevel% == 2 goto GroupPolicyUpdate
if %errorlevel% EQU 5 goto Favorites
if %errorlevel% EQU 3 goto Tools
if %errorlevel% EQU 4 goto Printer
:WindowsUpdate
start C:\Windows\system32\wuapp.exe
pause
goto leave
:GroupPolicyUpdate
start gpupdate.exe
pause
goto leave
:Favorites
move %userprofile%\favorites\*.* G:\
pause
goto leave
:Tools
start \\NoneOFyourBusiness
pause
goto leave
:Printer
start iexplore.exe http://www.Google.com
pause
goto leave
:leave
echo leave the script?
choice /c YN
if %errorlevel% NEQ 1 goto Begining
if %errorlevel% EQU 1 exit
** #echo off
color 0a
Title
:Beginning
set UserInput=Error
set /p UserInput=What Would You Like To Start?
echo.
if "%UserInput%"=="WU" goto Windows Update
if not "%UserInput%"=="" goto Exit
if "%UserInput%"=="GPU" goto Group Policy Update
if not "%UserInput%"=="" goto Exit
if "%UserInput%"=="Fav" goto Favorites
if not "%UserInput%"=="" goto Exit
if "%UserInput%"=="T" goto Tools
if not "%UserInput%"=="" goto Exit
if "%UserInput%"=="P" goto Printer
if not "%UserInput%"=="" goto Exit
:Windows Update
start C:\Windows\system32\wuapp.exe
goto exit
:Group Policy Update
start gpupdate.exe
goto exit
:Favorites
move %userprofile%\favorites\*.* G:\
goto exit
:Tools
start \\T
goto exit
:Printer
start iexplore.exe http://Google.com/
:Exit
set Beginning==Error
set /p Beginning==Return To The Start?
echo.
echo y=Yes or n=No
if "%Beginning%"=="y" call :Beginning
if not "%Beginning%"=="n" goto Exit 2
:Exit 2
pause
exit**

Batch text based game bugs when the player doesn't provide the correct input

So, me and two other friends are trying to make a text based game running on batch and everything's going smooth, well, except for this one thing, normally on text based games, when a player doesn't write the correct input, the game usually responds something like "don't know what is (the player input)" and in ours, it just bugs and goes on to the next line, so, my question is, how can we be able to fix this and do it the way text based games are supposed to do it?
Thanks!
here is some of the code:
set/p input="What do you want to do?"
if "%input%"=="play" goto :beg:
if "%input%"=="quit" goto :quit:
if "%input%"=="credits" goto :credits:
if "%input%"=="start" goto :lvl1:
if "%input%"=="ilrdj87imwa" goto :lvl2:
if "%input%"=="valveisevil" goto :lvl3:
pause
Without seeing the code you are using it's hard to know what's going wrong.
Try using something like this
:LOOP
set /p input=Enter input
if %input%==ok (
goto :CONTINUE
) else (
echo Wrong input
goto :LOOP
)
:CONTINUE
REM Input ok, carry on
Update
set /p input=What do you want to do?
if "%input%"=="play" goto :beg
if "%input%"=="play again" goto :pa
if "%input%"=="quit" goto :quit
:play
echo Play
pause >nul
:pa
echo Play again
pause >nul
:quit
exit >nul
You could also try using something like this...
Im not sure but i think you an use the parameters eq(equals) neg(Negative)
Note: the eq might actually be eg but im not sure...
EDIT: The actual parmeters are equ and neg
:loop
set /p choice=
if %choice% equ 1 goto game
if %choice% equ 2 goto info
if %choice% equ 3 goto options
if %choice% equ 4 goto exit
if %choice% neg 1 goto error
:error
echo Error you choose the wrong one...
echo Type c to continue
set /p start=
if %start% egu c goto loop
if %start% egu C goto loop
if %start% neg c goto error
if %start% neg C goto error
You have to put the neg 1 last or the code will never work if you where to not enter 1
if you dont enter in anything then it goes to ERROR

Resources