Home Ask Login Register

Developers Planet

Your answer is one click away!

Andrew Houser February 2016

Batch File how to search for keywords from user input?

Is there a way so my program can search for keywords in a user input? For example if I type in "Hello, who are you?" it will tell that I said Hello and says Hello back.

Answers


Dennis van Gils February 2016

Try this:

@echo off
set /p "str=say something containing the word Hello "
if not "x%str:hello=%"=="x%str%" echo Hello, how may I help you?
pause


svasa February 2016

A more explicit way:

@echo off
set input=
set /p input="Type some text : "
if "%input%"=="" (
   echo You didn't type anything!
) else (
   for  %%a in (%input%) do (
      if /i "%%a"=="Hello" (
         echo Hello!
      ) 
   )

)


Stephan February 2016

a more flexible way (possibilty to search for several words (synonymes)) - here "Hello", "Hallo" and "Hi" (but not "Halloween" or "China"):

set input=Hello, it's me
echo %input% |findstr /i "\<h[ae]llo\> \<hi\>" >nul && echo Hello back
set input=Hallo, it's me
echo %input% |findstr /i "\<h[ae]llo\> \<hi\>" >nul && echo Hello back
set input=Hi, it's me
echo %input% |findstr /i "\<h[ae]llo\> \<hi\>" >nul && echo Hello back
set input=it's Halloween
echo %input% |findstr /i "\<h[ae]llo\> \<hi\>" >nul && echo Hello back

the last line gives no output, because it searches for whole words only.

see findstr /? for explanations.

&& works as "if previous command (findstr) was successful then" (opposite would be ||)

Post Status

Asked in February 2016
Viewed 2,871 times
Voted 5
Answered 3 times

Search




Leave an answer


Quote of the day: live life