Page 1 of 1

spaces in an if statement

Posted: 07 Mar 2018 23:51
by gotouser
I am currently writing a script which asks you to type in multiple words and then checks if they were correct. I know you can put them in "" , but if you do it like this:

Code: Select all

@echo off
:loop
echo Type "hello world".
set /p input=
if %input%=="hello world" goto helloworld
echo Error.
pause >nul
goto loop

:helloworld
echo Hi!
pause >nul
goto loop
You have to put the answer in "" when typing as well. If you don't, it crashes. Does anyone have a solution?

Re: spaces in an if statement

Posted: 08 Mar 2018 00:57
by aGerman
The quotes will be compared as well which is the reason why you have to quote the input. Of course you can simply put the variable in quotes and you're done.

Code: Select all

if "%input%"=="hello world" goto helloworld
Steffen

Re: spaces in an if statement

Posted: 08 Mar 2018 02:43
by gotouser
It works, thank you! :)