MegaRodeon
MegaRodeon

Reputation: 335

Batch files always crash when entering a space

I have been programming a batch file to simulate some kind of game, but it always crash (or simply close) when entering and submitting the Space or just nothing at all. How can I fix this from happening? Example:

set /p option=Option: 

Then I would have:

Option: 

And I need to enter something, but in this case, if I hit Enter without entering anything (or Space) it will close itself.

=== EDIT ===

The full code here:

@echo off
cd Desktop
md TestFolder
goto main

:main
cls
title Test
echo.
echo Enter your name below
echo.
set /p name=Name:
if %name% == %name% set name=%name% & goto finish

:finish
echo.
echo Thank you for entering your name.
echo Press any key to close the window.
pause >nul
exit

The problem will lie in "set /p name=" as when batch prompts you for your name (the "Name:" part), if you hit Enter without anything entered, it will close.

Upvotes: 0

Views: 2487

Answers (3)

Durry42
Durry42

Reputation: 409

Maybe this is what you are after.

The line IF NOT DEFINED name GOTO main checks to see if the user just pressed the Enter key without typing anything and if that is true then will return to the :main label and prompt the user again to enter a name.

The line IF "%name%"==" " GOTO main checks if the user just entered 1 space character and if so, then will return to the :main label also to prompt the user again.

:main
cls
title Test
echo.
echo Enter your name below
echo.
set /p name=Name:
IF NOT DEFINED name GOTO main
IF "%name%"==" " GOTO main

:finish
echo.
echo Thank you for entering your name.
echo Press any key to close the window.
pause >nul
exit

Upvotes: 1

Stephan
Stephan

Reputation: 56180

This is the line which makes the trouble:

if %name% == %name% set name=%name% & goto finish

If you just hit enter at the set /p name=Name:, %name% will be empty. So the line above will execute as:

if == set name= & goto finish

Of course, this will never work To make the syntax correct, use:

if "%name%" == "%name%" set name=%name% & goto finish

Ok, this was the Syntax-part. but what do you try to do? If a is equal to a then make a = a ??

Upvotes: 3

dbenham
dbenham

Reputation: 130839

You have given us next to nothing to go on, but I'll make an educated guess. If you are running the file by double clicking the file (or a shortcut), from the desktop or from Windows Explorer, then your console will close automatically when the program reaches an end point. The end point could be due to your code logic, or it could be due to a fatal syntax error. It will close before you have a chance to read any messages that are printed.

The solution is to change how you run your game, especially while you are developing. Run the console directly by going to Start->All Programs->Accessories and select cmd.exe. Use CD to navigate to the folder where your game resides, and then enter the name of the batch file to run it. Now the console will remain open when the game ends, and you will be able to see all messages and debug much easier.

Upvotes: 2

Related Questions