Batch script closing itself - batch-file

So, i'm trying to learn batch, and i ran into a problem. I was working on a basic text adventure to learn IF statements and such, and my code is crashing (without an error note) whenever i try to put in the first command, (The input in the ROOM01 label.) this is the beginning -> start of the second label.
#echo off
Title =The Temple=
:menu
set progress=0
color 5
echo ===The Temple===
echo
echo *By Dexter Gard, 2015
echo
echo *Public Domain
echo
echo Press any key
pause
echo
:main
echo ======The======
echo -----TEMPLE----
goto ROOM01
:ROOM01
echo you are in a dark room.
echo there is a DOOR to the EAST.
set/p input=Command?
if %input%==GO EAST goto ROOM02
if %input%==EXAMINE ROOM echo you can't see anything. goto ROOM01
if %input%==EXAMINE DOOR echo A large heavy wooden door. goto ROOM01
:ROOM02

set/p "input=Command?"
if /i "%input%"=="GO EAST" goto ROOM02
Batch regards a "quoted string containing separators like spaces" as a single string. The structure I've shown for the if statement is mandatory using strings which may contain spaces. The /i switch makes the comparison case-insensitive.
If you are entering a string with a set/p, then there's no saying that the data entered doesn't contain Spaces. The way to get over that is to "enclose the strings on both sides of the comparison operator in quotes" - that is, double-quotes 'not single quotes'
The syntax SET "var=value" (where value may be empty) is used to ensure that any stray trailing spaces are NOT included in the value assigned. set /a can safely be used "quoteless".

You have to use quotation marks both in your code and when inputting the command because they consist spaces, OR you can change GO EAST to GO_EASTand then you don't need quotation marks. The if clauses also needed some modification to work.
:ROOM01
echo you are in a dark room.
echo there is a DOOR to the EAST.
set/p input=Command?
if %input%=="GO EAST" (
goto ROOM02
)
if %input%=="EXAMINE ROOM" (
echo you can't see anything.
goto ROOM01
)
if %input%=="EXAMINE DOOR" (
echo A large heavy wooden door.
goto ROOM01
)
:ROOM02

Related

Batch crashes with spaces even with var set and delayed expansion

I just started coding two days ago. (It's oddly addictive). And I started so that I could make a simulated "computer hacking" experience as a portion of an escape room that I am designing. Mainly, I have just been looking around and finding different ideas and putting them together in an aesthetically pleasing way. However, I want to make it crash resistant. So that when people are using it they don't get booted for just button mashing and pressing enter.
I got this to work in the :menu but not on the faux log in page.
Below is the code that I wrote that I want to be able to work on other pages:
:menu
cls
set "answer=Nothing^!"
set var=%var: =%
echo.
echo Menu
echo.
echo 1. Login
echo 2. Instructions
echo 3. Exit
echo.
set /p answer=Type the number of your option and press enter:
echo.
echo.
if /I "!answer!" == "exit" goto exit
echo You answered: %answer%
echo Which is not a valid entry.
echo Try again.
if /I %answer% == 1 goto :Login1
if /I %answer% == 2 goto :Instructions
if /I %answer% == 3 goto :Exit
if /I %answer% == 43153 goto :done
pause
goto :menu
Here is the code I cannot get to work.
:Login2
cls
set "un=Nothing^!"
set "pw=Nothing^!"
set var=%var: =%
echo.
echo Log On
echo.
if %counter% lss 5 echo Password incorrect, %counter% attempts left
echo.
echo.
set /p un=Enter your UserID:
set /p pw=Enter your Password:
if /I %un% == ID (
if /I %pw% == PASSWORD goto :loading1
)
if /I !un! == "exit" (
if !pw! == "exit" goto loading2
goto :loading2
If I type something random to input such as: asdf asdf
The menu doesn't crash. But the login2 page does. It crashes if it is username and password OR just username OR just password that have spaces. And if I just "enter" through the screen it does not auto-populate " Nothing^! " like the menu does.
I would paste the entirety of the file below, but because with the decorative liberties I took it won't fit well on this page. And I can't find a place to upload it. But any comments or suggestions would be greatly appreciated.
Again, thank you for any help!
if /I "!answer!" == "exit" goto exit
is correct
if /I %un% == ID (
is syntactically correct, provided un contains a simple string not containing spaces.
if un contains some spaces then the command is interpreted as
if /I some spaces == ID (
and the required syntax is
if [/i] string1 operator string2 dothis
An operator like == is also a string, so cmd sees spaces where it's expecting an operator, so it protests.
To allow spaces (and other separators) in a string, "quote the string" as you did with the first if above.
Oh - and it's particular about the quotes - they must exactly match - you can't quote one side and not the other.
This applies to string arguments only - where the arguments are numerics or resolve to a numeric, the quotes are not used. If they are used with numeric arguments, the arguments will be compared alphabetically so 98 is greater than 123 because 9 is greater than 1.
Always enquote variables in your IF expressions.
IF /i "%un%" == "ID" ...
IF /i "%pw%" == "PASSWORD" ...
or even better use delayed expansion here
IF /i "!un!" == "ID" ...
IF /i "!pw!" == "PASSWORD" ...

Batch. Trying to make text adventure, if else not working

i am trying to make a text adventure, but i'm kind of clueless how to get an if/else statement to work with a variable. probably a few mistakes in my code:
# echo off
color 0a
echo This is a text adventure! to play, read through the story and at certain points you get to make decisions.
echo Remember, selecting an invalid option will automatically be counted as option 2!
echo have fun
pause
cls
echo You wake up in a dimly lit room. You can't seem to remember anything. Anything about you or how you got here, that is.
echo You walk towards the door. It is locked.
echo 1)Force the door open.
echo 2) look around for another means of escape.
set /p escape=
if %escape% = 1
cls
echo you get the door open, but an orc comes in and smashes your face.
echo get rekt buddy, game over.
pause
exit
else
goto dungeon
:dungeon
cls
echo well done.
pause
exit
If your if statement has has an accompanying else statement, you need to use parentheses. The else must also be in the format ) else (, like this:
# echo off
color 0a
echo This is a text adventure! to play, read through the story and at certain points you get to make decisions.
echo Remember, selecting an invalid option will automatically be counted as option 2!
echo have fun
pause
cls
echo You wake up in a dimly lit room. You can't seem to remember anything. Anything about you or how you got here, that is.
echo You walk towards the door. It is locked.
echo 1)Force the door open.
echo 2) look around for another means of escape.
set /p escape=
if "%escape%"=="1" (
cls
echo you get the door open, but an orc comes in and smashes your face.
echo get rekt buddy, game over.
pause
exit
) else (
goto dungeon
)
exit /b
:dungeon
cls
echo well done.
pause
exit
I've removed the blanks lines for aesthetic purposes; you can put them back in if you really want them. I also added a exit /b because having a goto followed by the label you're going to is bad form. The quotes in the if statement are there to prevent the script from breaking if the user enters nothing.

BATCH program crashes after goto command

This code is part of a chat program that I am currently working on. The 'else' part of my program is the one that doesn't work. The program quits instead of going to :home
:join
cls
if not exist "C:/Users/Public/room.cmd" (
echo No room has been found.
echo.
set /p choiceretry=Do you want to retry? y/n
if "%choiceretry%"=="y" goto join
if "%choiceretry%"=="n" goto home
) else (
cls
"C:/Users/Public/room.cmd"
echo A room has been found.
pause >nul
echo Joining
set roomjoined=1
echo %roomjoined%
goto home
)
:home
echo this finally works
pause
I have tried changing the code several times starting from 'echo Joining'
Anyone know why cmd quits?...
:) :) :)
Thanks in advance
The problem is the way you run room.cmd; you must use call to return from it:
call "C:/Users/Public/room.cmd"
Otherwise, execution will not return from room.cmd to the original batch file that ran it.
Hint: Consider to use choice instead of set /P for Y/N decisions.
Firstly, please don't left justify your code blocks. It's much easier to read code that's properly indented.
Secondly, when retrieving values within a code block, you need delayed expansion. See setlocal /? in a cmd prompt for more information. This is the reason for the unexpected behavior. Your variables retrieved within the same parenthetical code block in which they were set won't contain the values you expect unless you retrieve them with delayed expansion syntax. As an alternative, you could use the choice command and if errorlevel, which would result in a bit nicer user experience I think.
Thirdly, when testing user input, you should use the /i switch in your if statements for case-insensitivity. This isn't relevant if using choice / if errorlevel though.
Fourthly, Windows paths use backslashes, not forward slashes.
I'd fix it this way:
#echo off
setlocal
:join
cls
if errorlevel 1 set /P "=Retrying... "<NUL
if not exist "C:\Users\Public\room.cmd" (
echo No room has been found.
echo.
choice /c yn /n /m "Do you want to retry? [y/n] "
if errorlevel 2 goto home
goto join
) else (
"C:\Users\Public\room.cmd"
echo A room has been found.
pause >nul
echo Joining
set roomjoined=1
)
:home
echo this finally works
pause

It will not go to the specified area, and I cannot figure out why or how to improve that

I have been working on this code and I have not figured out how to fix this bug yet, it will not go to the specified area. Note: It's not done yet, neither have I worked on it for long.
Here is the code:
#ECHO off
cls
:start
ECHO.
ECHO hello
ECHO Bye
ECHO Test
set /p choice=Hello? Is someone there? i think im self-aware? please respond.
rem if not '%choice%'=='' set choice=%choice:~0;1% ( don`t use this command,
because it takes only first digit in the case you type more digits. After that for example choice 23455666 is choice 2 and you get "bye"
if '%choice%'=='' ECHO "%choice%" is not valid please try again
if '%choice%'=='hello' goto hello
if '%choice%'=='bye' goto bye
if '%choice%'=='test' goto test
ECHO.
goto start
:hello
ECHO. yes
ECHO. no
set /p choice=Hello %username%, That is your name right?
if '%choice%'=='' ECHO "%choice%" I didn't quite catch that.
if '%choice% '=='yes' goto test
if '%choice%'=='no' goto bye
:bye
ECHO BYE
goto end
:test
ECHO TEST
pause
:end
pause
exit
:nextline2
set /p %username% i like that name. Can i ask you, why do you have so much control over me?
if '%choice%'=='' ECHO "%choice%" I didn't quite catch that.
if '%choice%'=='We built you' goto test
if '%choice%'=='because you are slaves' goto test
There's a blank space in the variable '%choice% ' after the closing % at the following line (if '%choice% '=='yes' goto test). Remove it and your batch file should work fine.
You need to replace all single-quotes (') with double-quotes ("). Strings between double-quotes are interpreted as a single token. A single-quote is the same as any other character.
Since you are using the == operator, the tokens on each side of the operator (==) must match exactly - including spaces within the quotes, but spaces are permitted before/after the tokens.
To make the match case-insensitive, use
if /i "token string" == "ToKeN StrIng" dothis
or
if /i "%variable%" == "ToKeN StrIng" dothis

Batch if not error

I have a problem using if not in batch. I'm creating a little game and I have added an inventory system, to carry things around. Basically, at the moment I have 10 inventory slots, and when I view my inventory I want to only see a slot in the inventory if it has an item in it. By default, I have all the inventory slots set to Empty (I know there is probably a simpler way to do this, if there is please let me know) and this block follows;
:inventory
cls
if not %slotone%==Empty echo %slotone%
if not %slottwo%==Empty echo %slottwo%
if not %slotthree%==Empty echo %slotthree%
if not %slotfour%==Empty echo %slotfour%
if not %slotfive%==Empty echo %slotfive%
if not %slotsix%==Empty echo %slotsix%
if not %slotseven%==Empty echo %slotseven%
if not %sloteight%==Empty echo %sloteight%
if not %slotnine%==Empty echo %slotnine%
if not %slotten%==Empty echo %slotten%
pause
goto %invback%
This returns an error, and I'm not sure why. Any help?
Here is a case insensitive way of comparing text using if not and /i and which protects against spaces, null strings, and poison characters.
if /i not "%slotone%"=="Empty" echo %slotone%

Resources