Solved

Detect if a String is in a Text File

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

856 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