Solved Why does my input cause a batch file crash?

October 6, 2014 at 21:27:56
Specs: Windows 7

I have the following type of code that works most of the time...

set /p input=Do you like apples (Y/N)?
if %input%==Y goto YAY
if %input%==N goto BOO
echo I did not recognize %input% as a valid response.
goto LABEL1
echo YAY! Apples are great.
echo BOO! Apples are great.

If I type Y or N at the prompt, it works fine. If i type any other string of letters or numbers, it works fine. The problem is when I type something that has a space in it, such as 'HECK YEAH', it seems to error and the batchfile closes.

message edited by uniform129

See More: Why does my input cause a batch file crash?

Report •

October 6, 2014 at 22:29:52
✔ Best Answer

if "%input%"=="Y" goto YAY
if "%input%"=="N" goto BOO

M2 Golden-Triangle

Report •

October 6, 2014 at 22:43:22
That seems to have fixed it.... but now I have 16,000 lines of code to go through and add a lot of " " too. :-(


Report •

October 7, 2014 at 01:20:15
Instead of enclosing every input with "" you could just disallow spaces.

Or use CHOICE, which allows ONE keypress. [just one]

M2 Golden-Triangle

Report •

Related Solutions

October 7, 2014 at 02:12:20
You can save a bunch of clutter.

If you're only accepting y or n:

::====== script starts here ===============
:: clutter.bat 2014-10-07 15:37:57.26
@echo off & setLocal enableDELAYedeXpansioN

set /p I= y or n ?
if /i !I! neq y if /i !I! neq n echo NG && goto :main
call :!I!
goto :eof

echo here's y
goto :eof

echo here's n
goto :eof
::====== script ends here =================

M2 Golden-Triangle

Report •

Ask Question