spaces in an if statement

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
gotouser
Posts: 7
Joined: 26 Feb 2018 07:36

spaces in an if statement

#1 Post by gotouser » 07 Mar 2018 23:51

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?
Last edited by Squashman on 08 Mar 2018 08:15, edited 1 time in total.
Reason: MOD EDIT: Please use code tags.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: spaces in an if statement

#2 Post by aGerman » 08 Mar 2018 00:57

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

gotouser
Posts: 7
Joined: 26 Feb 2018 07:36

Re: spaces in an if statement

#3 Post by gotouser » 08 Mar 2018 02:43

It works, thank you! :)

Post Reply