0

I am trying to make a chatbot.

This is my script:

:start
set /p message=
if %message%==Hello echo Hi!
goto start

But if I try to type something in like.. hello = world, it closes. How do I prevent that?

Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • Please read the long answers on [How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?](https://stackoverflow.com/a/49834019/3074564) and [Symbol equivalent to NEQ, LSS, GTR, etc. in Windows batch files](https://stackoverflow.com/a/47386323/3074564) if you are interested in why the command line `if %message%==Hello echo Hi!` fails with a string entered containing a space or one of these characters ``"&()[]{}^=;!'+,`~<|>``. Double quoting the compared strings is helpful with exception of user entered string contains one or more `"`. – Mofi May 14 '18 at 11:02

3 Answers3

1
if /i "%message%"=="Hello world" echo Hi!

Quote both sides of the comparison-operator. Note that /i makes the test case-insensitive.

Magoo
  • 77,302
  • 8
  • 62
  • 84
1

If you want to test if the message starts with hello then test %message:~0,5%

@echo off
:start
cls
set /p "message=type something: "
if /i "%message:~0,5%"=="Hello" echo Hi!
goto start

So by typing hello world it will detect the characters 0 - 5 being hello, /i makes it case insensitive as mentioned by @magoo

Gerhard
  • 22,678
  • 7
  • 27
  • 43
1

Your question has already been answered succinctly by Magoo, this is just an example to show a way of detecting if the input string contained the case insensitive string hello:

If /I Not "%message%"=="%message:hello=%" Echo Hi!
Compo
  • 36,585
  • 5
  • 27
  • 39