If contains batch file

July 1, 2010 at 20:32:15
Specs: Windows XP
Hello,

I am creating a batch file. When the input message contains 'keyword', it will do echo reply.

Example:
I can type keywords into Google, and get many search results.

Also, I want to use & in the message. How would I do that?


See More: If contains batch file

Report •

#1
July 1, 2010 at 22:39:13
Cool. Did you have a question, or a problem you need help with?

Insomniac at large


Report •

#2
July 1, 2010 at 23:17:01
Post has expired

Report •

#3
July 2, 2010 at 06:50:40
I didn't see a clearly defined question in your original post.
Here's an example for displaying "&" in the output:

::21831.bat
@ECHO OFF
ECHO Display this ^& that
EXIT /B

It uses the carat ^ to escape the &'s typical function. HTH!

Insomniac at large


Report •

Related Solutions

#4
July 2, 2010 at 08:57:49
Post has expired

Report •

#5
July 2, 2010 at 09:17:29
My main question was the very first sentence. 'I am
creating a batch file...'

Not to pick nits, but that is a statement, not a question,
hence my first response: "Cool." What do you have so far?
What are the keywords? Where are the keywords stored?
What prompts the user for input? I guess the problem is not
knowing enough about what exactly you want to do.
Are you trying to create a batch front-end to Google? Do you
want to ask the user for something to search Google, and
launch a browser with a Google search address with the
keyword already embedded? Help us help
you.

Insomniac at large


Report •

#6
July 2, 2010 at 09:57:35
I see your point.I have: set /p udefine=. If we look at the example in the first post...

Example:
I can type 'keyword's into Google, and get many search results

The message contains 'keyword', so I want it to go to a section of the batch file. I may need to detect multiple keywords.

I would have it cls, and echo message in that section.


Report •

#7
July 3, 2010 at 03:44:36
In this example, I'm only concerned with keyword*:

::21831.bat
@ECHO OFF
SETLOCAL
CLS

SET /p udefine=.
SET /A token_counter=1

:Token_Loop
FOR /f "tokens=%token_counter% delims= " %%a IN ("%udefine%") DO (
    2>NUL CALL :%%a %%a
    SET /A token_counter+=1
    GOTO Token_Loop
)

EXIT /B

:keyword1
CLS
ECHO %1
GOTO :EOF

:keyword2
CLS
ECHO %1
GOTO :EOF

:keyword3
CLS
ECHO %1
GOTO :EOF

:keyword4
CLS
ECHO %1
GOTO :EOF

Insomniac at large


Report •

#8
July 3, 2010 at 07:49:13
I was testing your example, and the window closes when I enter text. If I understand correctly, it takes the names of the sections to look for?

Source code


Report •

#9
July 3, 2010 at 08:33:59
I was testing your example, and the window closes when I
enter text.

Yes, I usually test the code from a command line, like this:

11:15:58>echo %prompt%
$t$h$h$h$g

11:16:14>21831.bat

Put PAUSE on the line before EXIT /B if you're double clicking
the .bat file. (I included echo %prompt% so you could see
why it shows the current time instead of the typical "C:\>"
prompt)

If I understand correctly, it takes the names of the
sections to look for?

What the example does is takes whatever the user inputs,
and one word at a time tries to "CALL" that word, another way
of executing a subroutine similar to GOTO. In the example, if
the user entered "keyword1 some other text keyword3", only
subroutines keyword1 and keyword3 would execute, because
some, other, and text are not defined as labels in the batch
file.

Insomniac at large


Report •

#10
July 3, 2010 at 14:20:07
When I type a sentence using 'keyword1', it quits. Also, I put multiple keyword detections, and it only directs me to the last one.

When I have EXIT /B, I created a menu to ask if they want to exit, or start a new session. The keyword detection becomes broken.


Report •

#11
July 3, 2010 at 20:13:18
:xx
for /f "tokens=%tokenvalue% delims= " %%a in ("%udefine%") do if "%%a" equ "" (goto :eof) else (call :%%a %%a)
set /a tokenvalue+=1
goto :xx

Report •

#12
July 3, 2010 at 20:26:47
You may need to explain that. When I ask a question, it automatically closes the window.

Is it still looking for the name of each section?


Report •

#13
July 3, 2010 at 21:47:58
You may need to explain that. When I ask a question, it
automatically closes the window.
Is it still looking for the name of each section?

I think that is because in the source code found in your link,
you are omitting:

GOTO :EOF

in your "keyword" labels, which would "return" back to the next line
after the

CALL :%%a %%a

I may be wrong about that, since the labels are at the actual
end of file, and might return by default...

Also, you may want to add some pauses at the end of each label (just before GOTO :EOF)

Insomniac at large


Report •

#14
July 4, 2010 at 04:10:44
I need it to detect 'keyword' anywhere in the sentence. It generates an error if the input word that is not 'keyword'. I need it to refresh.

Also, I have a menu that allows 'new', or 'exit' and it to do the same.


Report •

#15
July 4, 2010 at 17:20:28
Unfortunately, I think you are choosing only bits and pieces of
what is offered, and trying to incorporate it into your code.
Right off, there is a mix-match of "token_counter" and
"tokenvalue" in the FOR /F loop. Also, to avoid the error when
a keyword is NOT encountered, the solution was provided in
response 7 with:

2>NUL CALL :%%a %%a

Reviewing the code in your provided link, there seems to be
some more inherent problems: in :menu, what happens if the
user chooses something other than a or b? Like capital A or
capital B? As it is, the code falls through directly to :links,
which suffers the same problem of not accounting for "user
error", selecting something other than 0-11 and immediately
falling through to :ask.

:links also doesn't have an exit strategy even if the user
does select 0-11. After launching the browser, there is
nothing in the code to prevent immediately going onto the
:ask section.

I took the code a pared much of it down to presenting a menu,
and incorporating the user input interrogation loop:

@ECHO OFF
SETLOCAL
color 07
title Help Database
:main
cls
echo Help Database
PING 1.1.1.1 -n 1 -w 1000 >NUL

:menu
cls
echo Welcome to the Help Database
echo.
echo Please select from one of the following menu
echo items, or simply enter some relevant keywords:
echo.
echo  0. Item  0
echo  1. Item  1
echo  2. Item  2
echo  3. Item  3
echo  4. Item  4
echo  5. Item  5
echo  6. Item  6
echo  7. Item  7
echo  8. Item  8
echo  9. Item  9
echo 10. Item 10
echo 11. Item 11
echo.
echo  X  Exit this menu
echo.
SET /p udefine= - 

SET /A tokenvalue=1
:token_loop
for /f "tokens=%tokenvalue% delims= " %%a in 
("%udefine%") do (
    if /I "%%a" equ "x" EXIT /B
    if "%%a" equ "" (
        goto :eof
    ) else (
        2>NUL CALL :%%a %%a
    )
    set /a tokenvalue+=1
    goto :token_loop
)
PAUSE 
GOTO menu

:0 
start <a href="http://www.google.ca" target="_blank">http://www.google.ca</a>
goto :eof
:1 
start <a href="http://www.google.ca" target="_blank">http://www.google.ca</a>
goto :eof
:2 
start <a href="http://www.google.ca" target="_blank">http://www.google.ca</a>
goto :eof
:3 
start <a href="http://www.google.ca" target="_blank">http://www.google.ca</a>
goto :eof
:4 
start <a href="http://www.google.ca" target="_blank">http://www.google.ca</a>
goto :eof
:5 
start <a href="http://www.google.ca" target="_blank">http://www.google.ca</a>
goto :eof
:6 
start <a href="http://www.google.ca" target="_blank">http://www.google.ca</a>
goto :eof
:7 
start <a href="http://www.google.ca" target="_blank">http://www.google.ca</a>
goto :eof
:8 
start <a href="http://www.google.ca" target="_blank">http://www.google.ca</a>
goto :eof
:9 
start <a href="http://www.google.ca" target="_blank">http://www.google.ca</a>
goto :eof
:10 
start <a href="http://www.google.ca" target="_blank">http://www.google.ca</a>
goto :eof
:11 
start <a href="http://www.google.ca" target="_blank">http://www.google.ca</a>
goto :eof
	
:rsbot
cls
echo Testing 123
echo.
echo Testing 123
PAUSE 
goto :EOF

:crash
cls
echo Testing 456
echo.
echo Testing 456
PAUSE 
goto :EOF

I am unaware of how much more I'd be able to help without more
information and details...
Insomniac at large


Report •

#16
July 4, 2010 at 18:19:14
Unfortunately, I think you are choosing only bits and pieces of
what is offered, and trying to incorporate it into your code.

You seen like an expert coder, but I am not.

1. I still need the main menu
2. I fixed some of the errors using goto

I still have a problem with the 'keyword' detection. I was trying your code, and it kept closing the window.

The keyword detection is suppose to detect anywhere in the sentence. Right now, the keyword has to be alone. If I ask a question, the window closes.


Report •

#17
July 4, 2010 at 20:34:25
@op: my uneducated guess is you are trying to do too much with native batch script. I would strongly suggest investing in learning vbscript, perl, or visbasic (vb). But hell, might as well have some fun in the meantime!
@Orb:
for /f "tokens=%tokenvalue% delims= " %%a in
("%udefine%") do (
if /I "%%a" equ "x" EXIT /B
if "%%a" equ "" (
goto :eof
) else (
2>NUL CALL :%%a %%a
)
set /a tokenvalue+=1
goto :token_loop
)
::-----
shouldn't the incrementation and looping be done outside the for-loop? Since the for-loop is basically only set up to loop once, for each token...

:: foll. moved outside the for-loop
if /i "%udefine%" equ "x" echo "goodbye!" & pause & exit /b
:tokenloop
for /f "tokens=%tokenvalue% delims= " %%a in
("%udefine%") do (
:: putting this here will disallow "x""X" in user's input...
:: if /I "%%a" equ "x" EXIT /B
:: i think this can only happen when all tokens exhausted
if "%%a" equ "" goto :hold
2>NUL CALL :%%a %%a
)
:: end-for
set /a tokenvalue+=1
goto :token_loop

shrink-wrap:
:tokenloop
for /f "tokens=%tokenvalue% delims= " %%a in ("%udefine%") do if "%%a" equ "" (goto :hold) else (2>NUL CALL :%%a %%a)
set /a tokenvalue+=1
goto :token_loop

:hold
echo done processing...
pause
goto :eof


Report •

#18
July 4, 2010 at 22:10:02
You seen like an expert coder, but I am not.

I'm not trying to be mean or anything, I'm just pointing out some of the problems
that jumped out at me, and noted some things where you might think about when
when dealing with user error types of conditions.

I still have a problem with the 'keyword' detection. I was trying your code, and it
kept closing the window.

The keyword detection is suppose to detect anywhere in the sentence. Right
now, the keyword has to be alone. If I ask a question, the window closes.

It looks like you missed one. Please stop defining token_counter=1 and then try to
use %tokenvalue% in it's place in this FOR /F loop:

:ask
cls
echo Would you like to ask a question?
SET /p udefine= - 
SET /A token_counter=1
if %udefine%==yes goto :ask
if %udefine%==no goto :main

:token_loop
    for /f "tokens=%tokenvalue% delims= " %%a in ("%udefine%") do if "%%a" equ "" (goto :eof) else (call :%%a %%a)
    set /a tokenvalue+=1
    goto :token_loop

I am sure you are getting an error message along the lines of: " delims= " was unexpected
at this time, due to %tokenvalue% not being defined, and the interpreter not knowing what
to do next...

Insomniac at large


Report •

#19
July 4, 2010 at 22:20:31
My uneducated guess is you are trying to do too much with native batch script.

What I am trying to accomplish is not alot. Was the code directed at orangeboy?

Even if I change the code, it still results in a error. If I type in a question, the window closes.


Report •

#20
July 4, 2010 at 22:42:01
"for further assistance, please provide current code"
No idea where you're at currently, not even sure what exactly you wanna do other than identify key word or words.
I kind-of get the idea, but somewhere i'm missing the boat...
Code provided by Orb should have covered multiple keywords.

ps: these are live mines drifting in the harbour:
"I can type < or >, and the window closes. When I use %udefine%==, it still closes. What should I do?"

"special" characters will sink your script unless provided for: ! > < & | %
and even , ( )
depending. handle all quotes (single, dbl, back) with special attention and care.


Report •

#21
July 4, 2010 at 22:46:08
This is the code I have, and I want it to detect keywords in a sentence. Currently, it only detects if the keyword is alone.

Example:
If my keyword is 'google', and my question is What is Google?, it will detect that. Google can be anywhere.

If the section is not found, it would refresh :ask. I want the user to type < or > and goto :section. As I said above, it is not working.

Source Code


Report •

#22
July 5, 2010 at 06:18:06
SET str=word word keyword word word
SET found=
FOR %%a IN (%str%) DO IF /i "%%a"=="keyword" SET found=1
IF DEFINED found ECHO Found keyword


Report •

#23
July 5, 2010 at 07:40:57
Post has expired

Report •

#24
July 6, 2010 at 10:51:14
This thread has been solved

Report •

Ask Question