Solved

Detect if a String is in a Text File

Posted on 2008-10-24
5
267 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
5 Comments
 
LVL 20

Accepted Solution

by:
edster9999 earned 65 total points
Comment Utility
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 20 total points
Comment Utility
FINDSTR returns 0 for found and 1 for not found
0
 
LVL 2

Assisted Solution

by:hughese
hughese earned 20 total points
Comment Utility
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 20 total points
Comment Utility

: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
Comment Utility
Thanks all!
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

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…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now