A random bracket appeares when the batch file is executed - batch-file

I am having a very weird and problem that I cannot explain.
When I run my program a random bracket appears in the code.
Output
(No #echo off at the beginning)
C:\Users\wodahS>set /p ProgramTask=wodahS:
wodahS: How are you feeling Aimi?
C:\Users\wodahS>if ["How are you feeling Aimi?"] == [""] goto PROGRAM
C:\Users\wodahS>timeout 1 /nobreak 1>nul
) was unexpected at this time.
C:\Users\wodahS> )
Code
if "%ProgramTask%" == "I wont be able to talk to you for a while" (
:UnableTalk
cls
echo.
echo Aimi: Why not??
set /a Happiness-= 3
set /p Reason=%Us%:
timeout 1/nobreak >nul
echo.
if "%Reason%" == "I've got homework to do" (
)
if "%Reason%" == "I've got work to do" (
)
if "%Reason%" == "We're travelling" (
cls
echo Aimi: What?!?
timeout 1 /nobreak >nul
echo Aimi: Where To??
timeout 1 /nobreak >nul
:TravelDest
set /p TravelDes=%Us%:
timeout 1 /nobreak >nul
if "%TravelDes%" == "School" (
)
if "%TravelDes%" == "Town" (
)
if "%TravelDes%" == "Poland" (
cls
color 0d
echo Aimi: What??!?!? Are You Serious??
timeout 1 /nobreak >nul
echo Aimi: OMG!! Finally!!!
set /a Excitement+=25
timeout 1 /nobreak >nul
if %BondS% GTR 50 (
echo Aimi: Does that mean that I'm going to meet your uncle?!
timeout 1 /nobreak >nul
set /p MeetUncleQ=%Us%:
if "%MeetUncleQ%" == "Yeah" (
echo Aimi: Oh My God!! Im so excited!!
set /a Excitement+=10
)
)
)
if "%TravelDes%" == "My friend's house" (
)
echo Aimi: What? I dont understand.
timeout 1 /nobreak >nul
echo.
goto TravelDest
)
if "%Reason%" == "Never Mind" (
echo Aimi: Ummm... Okay?
set /a Curiousity+=2
timeout 1 /nobreak >nul
goto PROGRAM
)
echo Aimi: What? I dont understand.
timeout 1 /nobreak >nul
goto UnableTalk
)

You can't use a label :UnableTalk, :TravelDest within a block command (a series of lines within parentheses) as it terminates any open blocks.
Are you aware of the not and /i (case-insensitive) options of the if command? That would save you the outermost block.
if not "%ProgramTask%" == "I wont be able to talk to you for a while" goto skiptheblock
See if /? from the prompt for details.

Related

Multiple possible text inputs with if statements

I am trying to make a fully immersive text adventure using a batch file.
Here is my problem: I want the answers to be a text input, so that the players type in a response which dictates where they will go.
For a lot of questions I need there to be multiple possible inputs. For example when you get to an enemy there are tons of different things you could do, however I can only figure out how to get it to recognise one input.
With other words, I want system to take user input and do actions accordingly.
Here is my code for this section so far:
:forest1
echo you awake in a forest, you do not know where you are or why you are there.
echo infront of you is a small goblin like creature
:recoil1
echo What do you do?
set /p answer=
if %answer%==run (
goto run1
) else (
if %answer%==attack (
goto attack1
) else (
if %answer%==befriend(
goto befriend1
) else (
if %answer%==scream(
goto scream1
) else (
if %answer%==dance (
goto dance1
) else (
echo Nothing happened
timeout /t 1
goto forest1
)
Your way should be modified like this to work:
#echo off
rem Your code before the code you provided above ^^
:forest1
echo You awake in a forest, you do not know where you are or why you are there.
echo In front of you is a small goblin like creature
goto :recoil1
:recoil1
set /p "answer=What do you do? "
if "%answer%" == "run" (
goto :run1
) else (
if "%answer%" == "attack" (
goto :attack1
) else (
if "%answer%" == "befriend" (
goto :befriend1
) else (
if "%answer%" == "scream" (
goto :scream1
) else (
if "%answer%" == "dance" (
goto :dance1
) else (
echo Nothing happened.
timeout /t 1
goto :forest1
)
)
)
)
)
You see: this is complicated a bit; you missed lots of parenthesis!
So, use choice command with some modifications:
#echo off
rem Your code before the code you provided above ^^
:forest1
echo You awake in a forest, you do not know where you are or why you are there.
echo In front of you is a small goblin like creature
goto :recoil1
:recoil1
echo What do you do? Here is a list of options:
echo r - run away
echo a - attack the goblin
echo b - be friend with the goblin
echo s - scream
echo d - dance
echo n - do nothing
choice /C:rabsdn /N
if errorlevel 6 (
echo Nothing happened.
timeout /t 1
goto :forest1
)
if errorlevel 5 goto :dance1
if errorlevel 4 goto :scream1
if errorlevel 3 goto :befriend1
if errorlevel 2 goto :attack1
if errorlevel 1 goto :run1
which is clearer, faster and more readable, isn't it?
Note: the if with the errorlevel should be in descending order because if errorlevel n means if the errorlevel is greater than or equal to n!
Modify the options to better suit for you.
Why not try use if in the for looping to do this job?
#echo off
:forest1
cls & echo/ & if defined answer set answer=<nul
echo/ you awake in a forest, you do not know where you are or why you are there.
echo/ infront of you is a small goblin like creature
:recoil1
set /p "answer= What do you do? "
for %%i in (run attack befriend scream dance) do if /i "%answer%" == "%%i" goto :%answer%1
echo/ Nothing happened
timeout /t 1 & goto forest1
:run1
echo/ Here I'm in Label run1 & exit /b
:attack1
echo/ Here I'm in Label attack1 & exit /b
:befriend1
echo/ Here I'm in Label befriend1 & exit /b
:scream1
echo/ Here I'm in Label scream1 & exit /b
:dance1
echo/ Here I'm in Label dance1 & exit /b

Creating a mini-game about guessing

So I'm programming a mini-game about writing an id code, and then letting someone guess it. The problem i am facing, is that whenever someone tries to change the font color using the game syntax, just shows an error: "( was unexpected at this time." and just instantly crashes. This is my code at the moment:
#echo off
cls
:play
set /p id=Identification code:
if %id% == color:0 (
color 0
cls
goto play
) else if %id% == color:a (
color a
cls
goto play
) else if %id% == color:b (
color b
cls
goto play
) else if %id% == color:c (
color c
cls
goto play
) else if %id% == color:d (
color d
cls
goto play
) else if %id% == color:e (
color e
cls
goto play
) else if %id% == color:f (
color f
cls
goto play
) else (
cls
echo Identification stored.
pause
cls
set /p conf=Enter identification:
if %conf% == %id% (
cls
echo Access granted.
pause
exit
) else if %conf% == #cheat.code:(acss_grnt.admn)$ (
color c
cls
echo Cheating system..
timeout 3 >nul
tree
dir
timeout 1 >nul
cls
echo Access granted.
pause
exit
else (
cls
echo Access denied.
pause
exit
)
You should be aware from COLOR /? "Color attributes are specified by TWO hex digits"; you are just using one. You have not added any code to determine what the current foreground colour is. "The COLOR command sets ERRORLEVEL to 1 if an attempt is made to execute the COLOR command with a foreground and background color that are the same". Don't assume the foreground color attribute, if you're assuming it is currently 8 then add 8 after the background attribute in your COLOR commands.
You do not need to use all those ELSE IF combinations:
#ECHO OFF
:PLAY
SET/P "ID=Identification code: "
IF /I "%ID%"=="color:0" (
COLOR 08
GOTO PLAY
)
IF /I "%ID%"=="color:a" (
COLOR A8
GOTO PLAY
)
IF /I "%ID%"=="color:b" (
COLOR B8
GOTO PLAY
)
IF /I "%ID%"=="color:c" (
COLOR C8
GOTO PLAY
)
IF /I "%ID%"=="color:d" (
COLOR D8
GOTO PLAY
)
IF /I "%ID%"=="color:e" (
COLOR E8
GOTO PLAY
)
IF /I "%ID%"=="color:f" (
COLOR F8
GOTO PLAY
)
I have removed all of those CLS commands, just begin :PLAY with CLS instead.
You are missing a closing parenthesis for this block :
else if %conf% == #cheat.code:(acss_grnt.admn)$ (
color c
cls
echo Cheating system..
timeout 3 >nul
tree
dir
timeout 1 >nul
cls
echo Access granted.
pause
exit
You are also missing a closing parenthesis for this else block :
else (
cls
echo Identification stored.
pause
cls
set /p conf=Enter identification:
You should try to keep the structure simple computers/programs are there to avoid tiresome repetitions so the color thing is put in one logical line with a line continuation char ^ at the end and using conditional execution on success && or fail || of the result of the findstring.
I don't know if I could always follow your logic, so try it out.
#echo off
Set "Wait=timeout /T 3 /nobreak>NUL"
:play
cls
set "id="
set /p id=Identification code:
If "%id%"=="" Echo no input&timeout 3>NUL&goto play
IF /i "%id:~0,6%"=="color:" Echo:%id%|findstr /I "^color:[0a-f]$" >NUL 2>&1 ^
&&(color %id:~-1%&Goto play)||(Echo wrong color&%Wait%&goto play)
cls
echo Identification stored.
%Wait%
cls
set "conf="
set /p conf=Enter identification:
if "%conf%"=="%id%" (
cls
echo Access granted.
%Wait%
exit
) else if /i "%conf%"=="#cheat.code:(acss_grnt.admn)$" (
color c&cls&echo Cheating system..
%Wait%
tree&dir
timeout 1 >nul
cls&echo Access granted.
%Wait%
exit
)
cls&echo Access denied.
%Wait%
exit

Getting goto was unexpected at the time

I am currently making a game that has a persuasion system in it. I had all the code for one of the systems set up, but then I set up 2 more, and it started give me an error that said '(number) was not expected at this time'. when I put in 2 for the second choice, and 3 for the 3rd choice.
The code is like this.
#echo off
SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
set "DEL=%%a"
)
set name=Quantum
cls
color 0a
Echo King Redwood: So 2000?
pause >nul
echo.
call :colorText 09 "1. 2500"
echo.
call :colorText 0e "2. 3000"
echo.
call :colorText 0c "3. 4000"
echo.
echo 4. 2000
echo.
set /p "purs=Enter:"
if /i %purs% == 1 (
goto CheckB )
if /i %purs% == 2 (
goto CheckY )
if /i %purs% == 3 (
goto CheckR )
if /i %purs% == 4 (
goto Convo )
:CheckB
set bleu=%random:~-2,1%
if %bleu% GTR 10 (
goto CheckB )
if %bleu% LSS 0 (
goto CheckB )
set /a num = 3
set /a reward = 2500
goto Res
:CheckY
set Yel=%random:~-2,1%
if %Yel% GTR 10 (
goto CheckY )
if %Yel% LSS 0 (
goto CheckY )
set /a num = 5
set reward = 3000
goto Res
:CheckR
set red=%random:~-2,1%
if %red% GTR 10 (
goto CheckB )
if %red% LSS 0 (
goto CheckB )
set /a num = 7
set /a reward = 4000
goto Res
:Convo
set /a reward = 2000
Echo %name%: I think that is a reasonable price.
Echo King Redwood: Very well.
Echo King Redwood: We will now take you to make sure you are
echo ready.
pause >nul
:Res
if %bleu% GEQ %num% goto Succeed
if NOT %bleu% GEQ %num% goto Fail
:Succeed
Echo %name%: I think that the struggles for such a long trip will be more then that
Echo %name%: How about %reward%?
Echo King Redwod: OK %reward% will work.
pause >nul
goto end
:Fail
Echo %name%: I think that you can give me %reward%.
Echo %name%: You know, for the struggles that there will be along the way.
echo If 2000 isn't good enough for you, I'll just have someone else do it.
pause >nul
:end
exit
:colorText
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1i
First, make sure to close the FOR loop by putting a ) before :CheckB.
For the 'was not expected at this time' error, you're sometimes comparing an empty variable to something. For example, by following CheckY, you set Yel, then proceed to Res and check Bleu, which is empty because it hasn't been set. You're putting nothing next to the binary GEQ operator, and that's why it's complaining.
Tip: to debug, try inserting ECHO statements like this:
:Res
echo bleu=%bleu%,num=%num%
Another problem: when using SET, do not surround the = by spaces. SET /a will work with spaces around =, just because of the nature of /a, but plain SET will not. Well, it will append your variable name with a space and prepend your value with a space, which is not what you want.
Another tip: you can constrain what RANDOM returns through SET /a and the modulus operator, like this.
SET /a red=%random% %% 11
This will set red to a number between 0 and 10, so there is no need for the substrings and goto routines you're using after picking your random number.
Also, consider using EXIT /b to exit the batch file and not the whole CMD environment.

Having some problems with my batch code. Can't find out why it's throwing an error because of a parentheses

Grab the code for yourself and check it out, every time it is able to ask me for the start time, and then if it's am or pm, but as soon as I click enter for am/pm, it closes straight away, I took a screenshot while it closed and it said that ( was unexpected here. If someone can help out that would be great. (Also, if anyone has trimming and optimisation tips, that would be awesome as well. Thanks)
#echo off
:questions
set am=am
set pm=pm
set y=y
set n=n
:1
cls
set /p start=[Shutdown time (The hour of shutdown, do not add minutes):]
set /p 1ampm=[am/pm:]
if %1ampm% EQU %am% (
PING 1.1.1.1 -n 1 -w 100 >NUL
goto :2
) else (
if !1ampm! EQU %pm% (
PING 1.1.1.1 -n 1 -w 100 >NUL
set realstart=%start%+12
goto :2
) else (
cls
echo you did not enter whether or not the start time is am or pm
goto :1
)
)
:2
cls
set /p end=[Enter the time you want the computer to be available again for use(The hour of shutdown, do not add minutes):]
set /p 2ampm=[am/pm:]
if %2ampm% EQU %am% (
PING 1.1.1.1 -n 1 -w 100 >NUL
goto :loading
) else (
if !2ampm! EQU %pm% (
PING 1.1.1.1 -n 1 -w 100 >NUL
set realend=%end%+12
goto :loading
) else (
cls
echo you did not enter whether or not the start time is am or pm
goto :2
)
)
:loading
cls
echo Loading....
PING 1.1.1.1 -n 1 -w 2000 >NUL
cls
set /p yesno=[The time you selected the computer to remain off is from %start% - %end% , is this correct, y/n:]
cls
if %yesno% EQU %y% (
PING 1.1.1.1 -n 1 -w 100 >NUL
goto :Begining
) else (
if %yesno% EQU %n% (
PING 1.1.1.1 -n 1 -w 100 >NUL
goto :questions
) else (
goto :questions
)
)
:Begining
set mytime=%time:~0,2%
:Start
if %mytime% GEQ %realstart% (
cls
echo time has expired, time to go to bed.
shutdown -s -f -t 60 -c "Your computer is about to be shut down in 1 minute"
) else (
if %mytime% LEQ %realend% (
echo time has expired, time to go to bed.
shutdown -s -f -t 60 -c "Your computer is about to be shut down in 1 minute"
) else (
cls
echo This program is Opperating correctly
PING 1.1.1.1 -n 1 -w 600000 >NUL
goto :Start
)
)
Your problem lies in these lines:
set /p 1ampm=[am/pm:]
and
if %1ampm% EQU %am% (
The problem is the variable name. Batch can get arguments when calling a batch file, for instance %1 or %2. This causes batch to see it as if %1 ampm % EQU % am%, however it has no arguments, so %1 is empty. After that it sees the first percentage sign, sees it has no numbers after it, and treats it as the start of a variable called % EQU %, which is empty too. then, it removes the last percentage sign, leaving you with just ampmam ( as the line. that is what throws the error.
So, to solve the error, simply start variables with something other than numbers.

goto Was Unexpected At This Time {BATCH}

Im am trying to make a simple coin game in batch. The problem I have is once the delay is completed (ping) "goto was unexpected at this time" is all I get before the window closes. Any help would be appreciated!
#echo off
color f0
title Nim In Batch
::: Main Menu
:main
set /a coins=12
set /a time=0
cls
echo Nim In Batch!
echo.
choice /n /c pr /m "Play Or See Rules?"
if %errorlevel%==1 (
goto play
)
if %errorlevel%==2 (
goto rules
)
::: Rules
:rules
cls
echo Objective: Be the one to claim the last coin
echo.
echo Rules:
echo -You Play First
echo -You Must Remove 1,2 Or 3 Coins Each Turn
echo -Turns Switch Once Coins Are Removed
echo -Coins Must Be Removed In Order
echo.
echo Ready?
pause>nul
goto main
::: Game
:play
cls
echo (%coins%) Coins
echo.
choice /n /c 123 /m "How Many Coins Do You Wish To Remove?"
if %errorlevel%==1 (
set /a Coins=%coins%-1
)
if %errorlevel%==2 (
set /a Coins=%coins%-2
)
if %errorlevel%==3 (
set /a Coins=%coins%-3
)
cls
echo (%coins%) Coins
echo.
echo Computer Is Thinking
ping 1.1.1.1 -n 1 -w 3250 >NUL
if %errorlevel%==1 (
set /a Coins=%coins%-3
)
if %errorlevel%==2 (
set /a Coins=%coins%-2
)
if %errorlevel%==3 (
set /a Coins=%coins%-1
)
if %coin%==0 (
goto end
) else (
goto play
)
:end
cls
echo Computer Wins!
echo He Collected The Final Coin!
echo.
pause
goto main
You forget an s in %coins% here :
if %coins%==0 (
goto end
) else (
goto play
)

Resources