Solved Why does my input cause a batch file crash?

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

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

:LABEL1
set /p input=Do you like apples (Y/N)?
if %input%==Y goto YAY
if %input%==N goto BOO
echo.
echo I did not recognize %input% as a valid response.
goto LABEL1
:YAY
echo YAY! Apples are great.
:BOO
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 •


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

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

=====================
M2 Golden-Triangle


Report •

#2
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. :-(

Thanks!


Report •

#3
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

#4
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

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

:y
echo here's y
goto :eof

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

=====================
M2 Golden-Triangle


Report •


Ask Question