Solved

Detect if a String is in a Text File

Posted on 2008-10-24
5
269 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

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…
ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

810 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