The Batch file errors and closes - I can't find out why

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Blumekind
Posts: 2
Joined: 27 Apr 2016 10:39

The Batch file errors and closes - I can't find out why

#1 Post by Blumekind » 27 Apr 2016 10:47

Hello! I am VERY new to batch files and I am excited to learn. I have been working on a batch file using choices and the like. It does not seem to work, as it will display a split-second error and close when I enter the correct password. I will post the code here (warning: it's pretty long) and see if you guys can figure it out. If you manage to get it working, please respond with the working code and explain what I did wrong for future reference. Thanks! :D

Code: Select all

@echo off
:start
color 9
title OFENKARTOFFLE ACCESS POINT VERSION 0.1
echo Welcome to Ofenkartoffle's Access Point.
pause
echo Please enter the correct password.
set /password=
if %password%==memes(
goto select
:select
cls
echo 1 Residents
echo 2 Public Facilities
echo 3 Shops
echo 4 Enemies
set /p choice=Welcome! Please choose an option!
if not '%choice% == set choice=%choice:~0.1%
if  '%choice%' =='1' goto :choice1
if  '%choice%' =='2' goto :choice2
if  '%choice%' =='3' goto :choice3
if  '%choice%' =='4' goto :choice4
if not "%choice%" goto :invalidchoice
:invalidchoice
cls
echo Invalid option. Please try again.
goto :select
:choice1
echo OFFICIALS-
echo Frostzauber - Assistant
echo Blumekind - Founder
echo Yeeehaww - Co-Founder
echo REGULAR RESIDENTS-
echo None
echo Press any key to return to selection screen.
pause
goto :select
:choice2
echo PUBLIC SHEEP PEN-
echo Sheep for free killing and shearing.
echo Rebreeding is a must.
echo Keep sheep under breedlimit.
echo If killing, please leave 2-4 sheep for rebreeding, and use wheat for at least one baby.
echo JAIL-
echo Used to report and record griefers and enemies.
echo Please report griefers at the front desk.
echo Griefers will be banned as soon as possible.
echo CHICKEN COOP-
echo A small chicken coop used for public killing.
echo As with the sheep pen, please rebreed when killing.
echo Keep chickens under the breedlimit, and make sure to leave at least 2 for rebreeding.
echo WHEAT FARM-
echo A public wheat farm for the town.
echo Please replant.
echo Press any key to return to the selection screen
pause
goto :select
:choice3
echo SHOP INFORMATION-
echo A marketplace for player stands, used for selling knick-knacks and the like.
echo Restrictions apply, see signs at marketplace.
echo SHOP OWNERS-
echo Yeeehaww- Public REDACTED
echo Blumekind- Monster Card Stand
echo RTP555- Knick Knack Shop
echo Press any key to return to the selection screen.
pause
goto :select
:choice4
echo ENEMIES-
echo GOLDIELOL3 - Former town owner, possible threat.
echo TidgeyBoo - General cancer to the town, sure threat.
echo Neofox07 - Didn't join her nation, possible threat.
echo hollisterxo - Former town owner, possible threat.
echo PRECAUTIONS-
echo Place wall around town.
echo Employ guards.
echo Claim all town chunks.
echo IF GRIEFED-
echo Store records in jail.
echo Screenshot and report.
echo Investigate.
echo Do not accuse unless outnumbering.
echo Repair, up precaution measures.
echo IF DECLARED WAR UPON-
echo Employ more guards.
echo Up propoganda.
echo Employ blacksmiths.
echo Make weapons.
echo Report any sliver of unfairness.
echo Press any key to return to selection screen.

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: Help with batch? Answer promptly.

#2 Post by Squashman » 27 Apr 2016 13:58

Pretty much all your IF statements are incorrect.
The first SET command is creating a variable named assword.

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: Help with batch? Answer promptly.

#3 Post by sambul35 » 27 Apr 2016 14:20

@Blumekind

To refresh some programming skills, one would better start from a short snippet code. For starters, remove all echo lines, and see what's left and how it should & works. :D Separate your subroutings with space lines, end your code with exit /b .

Blumekind
Posts: 2
Joined: 27 Apr 2016 10:39

Re: Help with batch? Answer promptly.

#4 Post by Blumekind » 27 Apr 2016 16:47

Squashman wrote:Pretty much all your IF statements are incorrect.
The first SET command is creating a variable named assword.

Can you please specify how my IF statements are incorrect? Thank you.

ShadowThief
Expert
Posts: 1163
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Help with batch? Answer promptly.

#5 Post by ShadowThief » 27 Apr 2016 19:16

sambul35 wrote:@Blumekind

To refresh some programming skills, one would better start from a short snippet code. For starters, remove all echo lines, and see what's left and how it should & works. :D Separate your subroutings with space lines, end your code with exit /b .

Also, run it from the command prompt instead of double-clicking the .bat file.

ShadowThief
Expert
Posts: 1163
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Help with batch? Answer promptly.

#6 Post by ShadowThief » 27 Apr 2016 20:10

Since simply telling you that your title is no good doesn't add anything helpful, here is roughly what your code should look like (but also with an absurdly huge amount of comments so that you find out why I made the changes I did). Note that since everybody's programming style is different, "what it should look like" is somewhat relative.

Code: Select all

:: Everything with a :: at the start of the line is a comment. Some people will
:: tell you to use REM instead, and it's worth noting that if you want to
:: comment inside of a `for` loop, you absolutely HAVE to use REM, since :: is
:: technically a label and labels break `for` loops.
::
:: You'll also notice that none of the lines are longer than 80 characters. This
:: is considered "good programming practice" since you don't have to scroll
:: horizontally to read the entire comment.
::
:: Also, you may want to put some kind of header here to say what the script
:: does in case you put it away for six months and then find it again later.
:: For example,

::------------------------------------------------------------------------------
::                          OFENKARTOFFLE ACCESS POINT
::                                 VERSION 0.1s
::
:: Provides an access point to Ofenkartoffle. Written by Blumekind.
::
:: USAGE:     oap.bat
:: ARGUMENTS: None
:: RETURNS:   None
::
:: VERSION HISTORY
:: ---------------
:: 0.1s (2016-04-27) - [ShadowThief] Super-commented version for learning
::                                   purposes
::                   - [ShadowThief] Fixed errors
::
:: 0.1  (2016-04-27) - [Blumekind]   Initial Version
::------------------------------------------------------------------------------
@echo off
title OFENKARTOFFLE ACCESS POINT VERSION 0.1
cls

echo Welcome to Ofenkartoffle's Access Point
:: I got rid of the pause because I felt that it didn't really add anything.
:: If you disagree, you can put it back.

:: Put double quotes around variables and values when setting them to preserve
:: spaces.
set /p "password=Please enter the correct password: "

:: The quotes are here in this case to prevent a syntax error in case the user
:: hit enter without entering anything; `if =="memes" goto :select` would break
:: the script, but `if ""=="memes" goto :select` acts correctly. You could
:: really use anything, but double quotes also prevents certain characters like
:: & and | from causing unintended behavior.
::
:: Also, I'm going to `:select` instead of `select` so that it would be more
:: consistent if your code had functions. At this point, it's just for looks.
if "%password%"=="memes" goto :select

:: This is the default action to do if the value of %password% is not memes
exit /b

:select
cls
:: I added dashes purely for aesthetic purposes. Feel free to remove them if you
:: don't like them; they don't do anything. Also, I added a Quit option so that
:: I don't have to click the red X when I'm done testing.
echo 1 - Residents
echo 2 - Public Facilities
echo 3 - Shops
echo 4 - Enemies
echo Q - Quit

:: I'm using choice instead of set so that it is impossible for the user to
:: enter an invalid option.
::
:: The /C option is used to give possible options, and numerical values are
:: assigned based on the order in which they are listed.
::
:: The /M option is used to show a message when asking for input.
::
:: The /N option is used to prevent the choice command from showing the
:: possible options to the user. I'm using this since we already showed them
:: the options earlier.
choice /C:1234Q /M "Welcome! Please choose an option! " /N

:: Errorlevel is a special system variable, similar to $? if you're familiar
:: with bash. If you're not, it's a variable that contains the exit value of
:: the last command that was performed (there are a few exceptions to this,
:: but that's out of scope for this explanation). The options are listed in
:: reverse order because batch considers `if errorlevel 1` to mean "if the
:: errorlevel is 1 or more," so if they were in the regular order and you
:: press 5 to quit, you would be sent to the Residents screen.
if errorlevel 5 exit /b

:: I changed the label names to make them more descriptive for people looking
:: at the code later (or for you six months from now).
if errorlevel 4 goto :show_enemies
if errorlevel 3 goto :show_shops
if errorlevel 2 goto :show_facilities
if errorlevel 1 goto :show_residents

:: I added a blank line above this line so that it is more obvious that this is
:: chunk of code is totally separate from the main code. If I wanted to, I could
:: also give it a header, like this:

::------------------------------------------------------------------------------
:: Shows the residents of the town/game/thing.
::
:: Arguments: None
:: Returns:   None
::------------------------------------------------------------------------------

:: but you don't have to if you don't want to, since your code is just a bunch
:: of echo statements as of right now. If you do decide to add headers, it
:: doesn't have to look like that; that's just how I do them.
:show_residents
cls

:: I added a dash to both sides of your headers for aesthetic purposes.
echo -OFFICIALS-
echo Frostzauber - Assistant
echo Blumekind - Founder
echo Yeeehaww - Co-Founder

:: `echo.` prints a blank line. I'm printing blank lines for better readability.
:: Some people will tell you that you should use `echo/` instead, since `echo.`
:: can have unintended effects if there is a file called echo in your path or
:: current directory. These people will also tell you to use `echo(` instead of
:: `echo` for non-blank lines. It's up to you. I'm not doing it because I know
:: that I don't have any files called echo anywhere, but if you're going to
:: spread this file around online, it probably wouldn't hurt to make it a little
:: more bulletproof.
echo.
echo -REGULAR RESIDENTS-
echo None

:: I got rid of the "press any key to continue" message because `pause` already
:: shows one when you call it.
pause
goto :select

:: Another blank line since this is separate from the previous two code blocks.
:show_facilities
cls
echo -PUBLIC SHEEP PEN-
echo Sheep for free killing and shearing.
echo Rebreeding is a must.
echo Keep sheep under breedlimit.
echo If killing, please leave 2-4 sheep for rebreeding,
echo and use wheat for at least one baby.
echo.
echo -JAIL-
echo Used to report and record griefers and enemies.
echo Please report griefers at the front desk.
echo Griefers will be banned as soon as possible.
echo.
echo -CHICKEN COOP-
echo A small chicken coop used for public killing.
echo As with the sheep pen, please rebreed when killing.
echo Keep chickens under the breedlimit,
echo and make sure to leave at least 2 for rebreeding.
echo.
echo -WHEAT FARM-
echo A public wheat farm for the town.
echo Please replant.
pause
goto :select

:show_shops
cls
echo -SHOP INFORMATION-
echo A marketplace for player stands, used for selling knick-knacks and the like.
echo Restrictions apply, see signs at marketplace.
echo.
echo -SHOP OWNERS-
echo Yeeehaww - Public REDACTED
echo Blumekind - Monster Card Stand
echo RTP555 - Knick Knack Shop
pause
goto :select

:show_enemies
cls
echo -ENEMIES-
echo GOLDIELOL3 - Former town owner, possible threat.
echo TidgeyBoo - General cancer to the town, sure threat.
echo Neofox07 - Didn't join her nation, possible threat.
echo hollisterxo - Former town owner, possible threat.
echo.
echo -PRECAUTIONS-
echo Place wall around town.
echo Employ guards.
echo Claim all town chunks.
echo.
echo -IF GRIEFED-
echo Store records in jail.
echo Screenshot and report.
echo Investigate.
echo Do not accuse unless outnumbering.
echo Repair, up precaution measures.
echo.
echo -IF DECLARED WAR UPON-
echo Employ more guards.
echo Up propoganda.
echo Employ blacksmiths.
echo Make weapons.
echo Report any sliver of unfairness.
pause

:: I added a `goto :select` here because batch scripts always run from the top
:: down and when they run out of code, they end. Without this `goto`, the script
:: would end after you chose to view your enemies.
goto :select

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: Help with batch? Answer promptly.

#7 Post by sambul35 » 28 Apr 2016 08:03

@Blumekind

Checking Windows Cmd Reference for basic commands syntax may help. Or instead specify a narrow task you can't address, as efficient ways may already exist to solve it. Bloated output only makes it harder to help.

@ShadowThief

Weather forecast is still missing in your tutorial. :(

Post Reply