1

I am new to batch and I am try making a "brain like" program for a project, it should be able to complete simple short conversations. I am using set /p to ask the user questions, like this:

set /p a= Hello: 

I want to be able to see if the user said a specific word in their answer to help determine what the computer will reply.

Thanks.

4 Answers 4

2
@echo off
set "specific_word=something"

set /p a= Hello: 


setlocal EnableDelayedExpansion
if /I not "!a:%specific_word%=!" EQU "!a!" (
    echo it contains the word
) else (
    echo it does not contain the word
)


echo %a%|find /i "%specific_word%" >nul 2>&1

echo --OR--

if errorlevel 1 (
    echo it does not contain the word

) else (
    echo it contains the word
)

the IF approach is faster.

Sign up to request clarification or add additional context in comments.

Comments

2

Not bulletproof code, just a skeleton. This code tests for the presence of the "word" word in the typed text

@echo off

    setlocal enableextensions disabledelayedexpansion

:input
    set "typed="
    set /p "typed=what? "
    if not defined typed goto :input

    rem Option 1 - Use find 
    echo( %typed% | find /i " word " >nul 
    if not errorlevel 1 echo FIND : "word" has been used 

    rem Option 2 - Use substring replacement
    set "text= %typed% "
    if not "%text: word =%"=="%text%" (
        echo IF   : "word" has been used
    )

    rem Option 3 - Tokenize the input
    set "text=%typed:"= %"
    for %%a in ("%text: =" "%") do (
        if /i "%%~a"=="word" echo FOR  : "word" has been used
    )

    endlocal

Aditional spaces are added where needed to ensure "word" is not found inside "sword".

Comments

1

IF would not help you, as batch has no native substring funktions. But you can emulate it with a little trick:

set a=user inputted something with a word in it.
echo %a%|find /i "word" >nul && (echo there is "word" in the input)

the /i tells it to ignore capitalzation

>nul tells it not to show it's findings on the screen

&& acts as "if find was successfull, then..."

Comments

1

The find command can be used

it isn't very elegant but you could use a series of FIND commands and if statements.

@echo off
set /p a= "Hello: "

echo %a% | C:\Windows\System32\FIND /I "Hi" >  nul 2>&1
set FIND_RC_0=%ERRORLEVEL%

echo %a% | C:\Windows\System32\FIND /I "Howdy" > nul 2>&1
set FIND_RC_1=%ERRORLEVEL%

if "%FIND_RC_0%" == "0" (
    set /p b= "How are you today?: "
)

if "%FIND_RC_1%" == "0" (
    set /p b= "How you doing partner?: "
)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.