Tech Support Forum banner

[SOLVED] If contains batch file

17311 Views 37 Replies 3 Participants Last post by  Activeradio
I have used the following code in my batch file to detect keywords in a sentence, and go to a section.

Example:
I can type keywords into Google and get search results. It found 'keyword', so it goes to a section of the file.

Currently, it only detects one keyword, and I want it to detect multiple. I am already using ) ELSE (, but there are some keywords that go to the same section.

Code:
@echo off
echo Would you like to ask a question?
SET /p str= - 
SET found=
FOR %%a IN (%str%) DO IF /i "%%a"=="keyword" SET found=1
IF DEFINED found goto :question
) ELSE ( 
FOR %%a IN (%str%) DO IF /i "%%a"=="keyword1" SET found=1
IF DEFINED found goto :question
) ELSE (
FOR %%a IN (%str%) DO IF /i "%%a"=="keyword2" SET found=1
IF DEFINED found goto :question1
Status
Not open for further replies.
1 - 20 of 38 Posts
Re: If contains batch file

Pipe the response to Findstr using Echo, you can check for multiple keywords
Code:
@Echo off
:_Ask
Echo.Would you like to ask a question?
Set /p str= - 
If "%str%"=="" Goto :EOF
If /I "%str%"=="no" Goto :EOF
Echo.%str%|Findstr /I "keyword keyword1 anotherkeyword"
If %Errorlelevel%==0 Goto Question
Echo.%str%|Findstr /I "keyword2 keyword7"
If %Errorlelevel%==0 Goto Question1
Echo.%str%|Findstr /I "keyword8 keyword10 anotherkeyword17"
If %Errorlelevel%==0 Goto Question2
:: If none of the keywords are found, have them ask another question.
Echo.I can't recognize any of those words, please try again
Goto _Ask
See less See more
Re: If contains batch file

Your code results in the closing of the window.
Re: If contains batch file

Are you double clicking the file to run it instead of running from a Command Prompt?
When running from a Command Prompt, what error do you get?
Does it close before you can enter the keywords, or after entering something? (You did add the QuestionX sections to process the keywords, correct?)
Re: If contains batch file

I am double clicking on the bat, which is what my users would do.
Re: If contains batch file

When running from a Command Prompt, what error do you get?
Does it close before you can enter the keywords, or after entering something? (You did add the QuestionX sections to process the keywords, correct?)
Re: If contains batch file

How do I run your code in command prompt? It still need it to work in a .bat.
Re: If contains batch file

If you won't run it in a command prompt it's going to be very hard to troubleshoot. That's the only way to keep the window open to see any error messages.
Click Start | Run (or press WinKey+R), type cmd, press Enter
Navigate to the folder with the .bat file, then type it's name.

Spotted a typo that's probably the problem, I ran it on an XP system, then typed it here on my Win 7 system and misspelt Errorlevel as Errorlelevel"

Give this a try:
Code:
@Echo off
:_Ask
Set str=
Echo Would you like to ask a question?
Set /p str= - 
If "%str%"=="" Goto :EOF
If /I "%str%"=="no" Goto :EOF
Echo.%str%|Findstr /I "keyword keyword1 anotherkeyword"
If %Errorlevel%==0 Goto Question
Echo.%str%|Findstr /I "keyword2 keyword7"
If %Errorlevel%==0 Goto Question1
Echo.%str%|Findstr /I "keyword8 keyword10 anotherkeyword17"
If %Errorlevel%==0 Goto Question2
:: If none of the keywords are found, have them ask another question.\
Echo.I can't recognize any of those words, please try again
Goto _Ask
See less See more
Re: If contains batch file

I have tested the file, and I get this error.

Code:
...is not recognized as an internal or external command,
operable program or batch file
Re: If contains batch file

Testing something
Re: If contains batch file

Post the complete code you are running, as I have no idea what you've added.

What is the complete error message?
The unrecognized Command will be shown in single quotes, like this:
Code:
'badcommand' is not recognized as an internal or external command,
operable program or batch file.
If it's actually '...' then it's part of something you've added, as that isn't present in the above code.

Only error the above code will give is missing label errors, as none of the labels exist.
Also it will find partial matches, so your keywords have to be unique. With the above keywords, keyword8 will match on the first If statement as it contains keyword, so the order if the If statements can make a difference, or you may need to specify exact word matches using \< and \> in the Findstr commands. See Findstr /? for info.
See less See more
Re: If contains batch file

The ... was the location of the bat, but I fixed the error. I typed in keyword, and I got the following error.

Code:
Goto was unexpected at this time
Source Code
Re: If contains batch file

You haven't corrected my typo, you still have errorlelevel instead of errorlevel
Spotted a typo that's probably the problem, I ran it on an XP system, then typed it here on my Win 7 system and misspelt Errorlevel as Errorlelevel"
Anything you type containing keyword will go to question. to use keyword and keyword5 for example, you need to add the word boundries \< and \>:
Code:
Echo %str%|Findstr /I "\<keyword\> \<keyword1\> \<keyword2\>"
If %Errorlevel%==0 Goto question
Echo %str%|Findstr /I "\<keyword3\> \<keyword4\>"
If %Errorlevel%==0 Goto question1
Echo %str%|Findstr /I "\<keyword5\> \<keyword6\> \<keyword7\>"
If %Errorlevel%==0 Goto question2
See less See more
Re: If contains batch file

Your code seems to be working now. I had quite a few errors in my old code, but this solves it all.

When I type in <, or >, the window closes. I am using if %udefine%==/<, and if %udefine%==/>

Code:
< was unexpected at this time.
The syntax of the command is incorrect
Code:
> was unexpected at this time.
The syntax of the command is incorrect
See less See more
Re: If contains batch file

< and > are redirection symbols, used to redirect console input and output
The are also used by findstr as part of the word boundry Regular expression operators:
\< is beginning of word
\> is end of word
These characters are not part of the search string, and should not be entered. They just indicate that the match needs to start or end on a word boundary.
Examples:
The user types wordsmith or keyword:
Echo %str%|Findstr /I "word" will match wordsmith and keyword
Echo %str%|Findstr /I "\<word" will match wordsmith but not keyword
Echo %str%|Findstr /I "word\>" will match keyword but not wordsmith
Echo %str%|Findstr /I "\<word\>" will not match either, it will only match word

They can only be used inside the quotes in a search string, or as redirection operators.

If you need to see if the variable udefine has the characters \> you have to quote both operands:
if "%udefine%"=="/<" Echo Found
See less See more
Re: If contains batch file

I am using if "%udefine%"=="/<" goto :ask, and if "%udefine%"=="/>" goto :ask.

Code:
< was unexpected at this time
Code:
> was unexpected at this time
Re: If contains batch file

Using where? Post your code, if they are quoted it shouldn't give that error. Do you have \> or \< someplace else in the file?

You should comment out the Echo off and cls statements so you can debug the code. That what you can see what line is causing the error.
Only put them in when you have all the bugs worked out.
I added if "%udefine%"=="/<" goto :ask for question and it works fine:
Code:
::@Echo off
:ask
::cls
Echo Would you like to ask a question?
Set /p str= - 
If "%str%"=="" Goto :EOF
If /I "%str%"=="no" Goto :EOF
Echo %str%|Findstr /I "keyword keyword1 keyword2"
If %ErrorLevel%==0 Goto question
Echo %str%|Findstr /I "keyword3 keyword4"
If %ErrorLevel%==0 Goto question1
Echo %str%|Findstr /I "keyword5 keyword6 keyword7"
If %ErrorLevel%==0 Goto question2
Goto ask

:question
::cls
echo Question
SET /p udefine= -
[B]If "%udefine%"=="/<" goto :ask
Echo You Entered "%udefine%", exiting[/B]
goto :EOF

:question1
::cls
echo Question 1
SET /p udefine= - 
goto :EOF

:question2
::cls
echo Question 2
SET /p udefine= - 
goto :EOF
and the result (Prompt is blue, text I typed is red:
Trying to add colors in a code box really messes up the spacing, so no box)
==============================

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\TheOutcaste>cd\scripts

C:\Scripts>ask

C:\Scripts>Echo Would you like to ask a question?
Would you like to ask a question?

C:\Scripts>Set /p str= -
- keyword

C:\Scripts>If "keyword" == "" Goto :EOF

C:\Scripts>If /I "keyword" == "no" Goto :EOF

C:\Scripts>Echo keyword | Findstr /I "keyword keyword1 keyword2"
keyword

C:\Scripts>If 0 == 0 Goto question

C:\Scripts>echo Question
Question

C:\Scripts>SET /p udefine= -
-/<

C:\Scripts>If "/<" == "/<" goto :ask

C:\Scripts>Echo Would you like to ask a question?
Would you like to ask a question?

C:\Scripts>Set /p str= -
- keyword

C:\Scripts>If "keyword" == "" Goto :EOF

C:\Scripts>If /I "keyword" == "no" Goto :EOF

C:\Scripts>Echo keyword | Findstr /I "keyword keyword1 keyword2"
keyword

C:\Scripts>If 0 == 0 Goto question

C:\Scripts>echo Question
Question

C:\Scripts>SET /p udefine= -
-/>

C:\Scripts>If "/>" == "/<" goto :ask

C:\Scripts>Echo You Entered "/>", exiting
You Entered "/>", exiting

C:\Scripts>goto :EOF

C:\Scripts>
==============================

If you leave off the quotes you get this, so you can see exactly which line caused the error, the line displayed right after the error:
==============================

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\TheOutcaste>cd\scripts

C:\Scripts>ask

C:\Scripts>Echo Would you like to ask a question?
Would you like to ask a question?

C:\Scripts>Set /p str= -
- keyword

C:\Scripts>If "keyword" == "" Goto :EOF

C:\Scripts>If /I "keyword" == "no" Goto :EOF

C:\Scripts>Echo keyword | Findstr /I "keyword keyword1 keyword2"
keyword

C:\Scripts>If 0 == 0 Goto question

C:\Scripts>echo Question
Question

C:\Scripts>SET /p udefine= -
-/<
< was unexpected at this time.

C:\Scripts>If /< == /< goto :ask

C:\Scripts>
See less See more
Re: If contains batch file

I am not trying to enter "/>", or "/<" into the search box. I am trying to enter <, and >. I was not sure if you understood.

Source Code
Re: If contains batch file

I'm not sure what you mean by Search box, unless you mean when the code stops and waits for you to type something.
If you type a &, < or > at the input prompt right after the "Would you like to ask a question?
" prompt, it will give this error:
| was unexpected at this time.
This is because the variable in the Echo %str%|Findstr command is not quoted. If you need to be able to use those symbols, you need to put quotes around %str%

I get no other error running that. Doesn't matter if I enter <, >, \<, \>, /<, or /> at the question prompt after entering a keyword.
I'm not sure what you are trying to accomplish. If the user types /< it will go back to the ASK label and start over, otherwise it exits. I don't see why the user be typing /< at that point, or the > symbol at all.

There is no way to get to question1 or question2
If they enter keyword3, the first If statement will match on the keyword part of keyword3 and they will go to question, not question1
See less See more
Re: If contains batch file

I am not sure what is happening, but it works. When I enter a keyword, I get the following error. I used ) ELSE ( between each set keywords because some words need to be priority.

Code:
'}' is not recognized as an internal or external command,
operable program or batch file.
1 - 20 of 38 Posts
Status
Not open for further replies.
Top