?
Solved

Detect if a String is in a Text File

Posted on 2008-10-24
5
Medium Priority
?
274 Views
Last Modified: 2010-08-05
Hello,

In a batch program (.bat) how can I detect if a string (say "ABCD") is in a batch file.

Here is psuedo code on how I need to use it:
IF isStringInFile("ABCD") ( do this )

Caveats
* I cannot call out to vbscript.
* I cannot use any third part tools (limited to Microsoft 2003 OS and perhaps the Resource Kit).

Thanks,

Michael
0
Comment
Question by:michael4606
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 20

Accepted Solution

by:
edster9999 earned 260 total points
ID: 22795866
the command is
findstr

It seraches for text in files (a bit like grep in linux)
You could do something like

findstr /I /C:"ABCD" file.bat
if errorlevel 0 echo "whatever"

(you'll have to play with the errorlevel bit as I dont know if 0 = found or not found)
0
 
LVL 5

Assisted Solution

by:PaulKeating
PaulKeating earned 80 total points
ID: 22796025
FINDSTR returns 0 for found and 1 for not found
0
 
LVL 2

Assisted Solution

by:hughese
hughese earned 80 total points
ID: 22797499
find /c "ABCD" C:\test.txt

returns the number of instances of said string. Pretty similar to above really.


findstr has more arguments for fine tuning
0
 
LVL 2

Assisted Solution

by:shanedsouza
shanedsouza earned 80 total points
ID: 22812045

:main
@echo off
find /i "String" FileName.Ext
if %errorlevel%==0 goto found
goto notfound
 
:found
program.exe ...
goto end
 
:notfound
echo "String" notfound
goto end
 
:end

Open in new window

0
 

Author Closing Comment

by:michael4606
ID: 31509638
Thanks all!
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Learn about cloud computing and its benefits for small business owners.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question