Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Searching for a string within a text file  (Read 2204 times)

0 Members and 1 Guest are viewing this topic.

yankfootball

  • Guest
Searching for a string within a text file
« on: March 14, 2006, 10:22:49 AM »
I am building a batch file that would only call another batchfile if a certain text string is found within a particular text file:  I was thinking I could use the IF, FINDSTR and CALL commands to build this but since my DOS knowledge is limited at best I am unsure of the syntax.



For example

Execute batch file "A"

IF FINDSTR /I "Database" file.txt CALL B.bat. ELSE CALL C.bat.


anyhelp would be appreciated..

carlos

  • Guest
Re: Searching for a string within a text file
« Reply #1 on: March 14, 2006, 03:56:41 PM »
You can do:

Code: [Select]
FINDSTR /I file.txt "Database" > NUL
if not errorlevel 1 (CALL B.bat) ELSE CALL C.bat

The command FIND/FINDSTR return an errorlevel equal to 0 when the string is found in the file.
The most of the commands return an errorlevel 0 when they are correctly executed.

The sentence:
      IF ERRORLEVEL 5 command
would be equivalent to
       IF ERRORLEVEL >= 5 command    <-- you can't do this

so, if you negate:

      IF NOT ERRORLEVEL 5 command
is equivalent to
      IF ERRORLEVEL < 5 command

and ...

      IF ERRORLEVEL 5 IF NOT ERRORLEVEL 6 command[/tt][/color]
is equivalent to
      IF ERRORLEVEL == 5 command

yankfootball

  • Guest
Re: Searching for a string within a text file
« Reply #2 on: March 14, 2006, 05:49:14 PM »
Thanks Carlos