Solved

Detect if a String is in a Text File

Posted on 2008-10-24
5
268 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
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 20 total points
ID: 22796025
FINDSTR returns 0 for found and 1 for not found
0
 
LVL 2

Assisted Solution

by:hughese
hughese earned 20 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 20 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I've always wanted to allow a user to have a printer no matter where they login. The steps below will show you how to achieve just that. In this Article I'll show how to deploy printers automatically with group policy and then using security fil…
On July 14th 2015, Windows Server 2003 will become End of Support, leaving hundreds of thousands of servers around the world that still run this 12 year old operating system vulnerable and potentially out of compliance in many organisations around t…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

910 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

20 Experts available now in Live!

Get 1:1 Help Now