Batch File To Find The String Count

October 30, 2009 at 13:27:38
Specs: Windows XP
Hi all,

I have a HTML file, i want to find the string count "Failed" and "Success" and write the output in the csv file with the column1 as current day date, column3 as Success count and column3 as the Failure count

The output should append when ever you run the script

I want the output file as follows
----------------------------------
If run the batch file today the output should be

Date SucessCount FailureCount
---- ----------------- --------------
10/29/2009 30 10

If run the batch file tomorrow the output should be
Date SucessCount FailureCount
---- ----------------- ----------------
10/30/2009 30 10
10/31/2009 20 20

Can you please help me..

Thanks


See More: Batch File To Find The String Count

Report •


#1
October 31, 2009 at 01:24:01
@echo off & setLocal EnableDELAYedExpansion

for /f "tokens=* delims= " %%a in ('find /c /i "success" ^< my.html') do (
set S=%%a
)
for /f "tokens=* delims= " %%a in ('find /c /i "failure" ^< my.html') do (
set F=%%a
)
>> my.csv echo %date%, !S!, !F!


=====================================
Helping others achieve escape felicity

M2


Report •

#2
November 2, 2009 at 07:22:09
Hi M2 ,
thank for the quick reply , i excuted your script on my HTML File i got the results wrong. i should get 36 Success count and 1 Failed Count ia m gettign 0 count,my HTML File get created by a JAVA Program so it not in proper format as HTML.

My HTMLFile looks like this( i tired to attach the file i dont find the attachment option) so i am pasting it here.
---------------------------------

<body>
<body>
<!-- DOWNLOAD_LINK --><!--Table1--><div name="hidden" style="background-color: #EFEFEF; width: 500px; "><table style="width: 100%;"><tr style="background-color:white; "><td style="font-weight:bold; font-size:120%; " colspan="4">D1</td></tr><tr><td style="border-bottom-color: black; border-bottom-style: solid; border-bottom-width: 1px; font-weight:bold; ">Project</td><td style="border-bottom-color: black; border-bottom-style: solid; border-bottom-width: 1px; font-weight:bold; ">Configuration:</td><td style="border-bottom-color: black; border-bottom-style: solid; border-bottom-width: 1px; font-weight:bold; ">Status:</td></tr><!-- TABLE ENTRY -><tr name="highlightrow"><td></td><td>SB</td><td style="background-color:#316745; color:white; width:7%; ">Success</td><td></td></tr><!-- END TABLE ENTRY--><!-- TABLE ENTRY --><tr name="highlightrow"><td>M1</td><td>SB</td><td style="background-color:#316745; color:white; width:7%; ">Success</td><td></td></tr><!-- END TABLE ENTRY --><!-- TABLE ENTRY --><tr name="highlightrow"><td>Installer</td><td>SB</td><td style="background-color:#316745; color:white; width:7%; ">Success</td><td></td></tr><!-- END TABLE ENTRY --><!-- TABLE ENTRY --><tr name="highlightrow"><td>D1</td><td>SB</td><td style="background-color:#316745; color:white; width:7%; ">Success</td><td></td></tr><!-- END TABLE ENTRY --><!-- TABLE ENTRY --><tr name="highlightrow"><td>Engine</td><td>SB</td><td style="background-color:#316745; color:white; width:7%; ">Success</td><td></td></tr><!-- END TABLE ENTRY --><!-- TABLE ENTRY --><tr name="highlightrow"><td>Logic1</td><td>SB</td><td style="background-color:#316745; color:white; width:7%; ">Success</td><td></td></tr><!-- END TABLE ENTRY --><!-- TABLE ENTRY --><tr name="highlightrow"><td>Prod1</td><td>SB</td><td style="background-color:#316745; color:white; width:7%; ">Success</td><td></td></tr><!-- END TABLE ENTRY --><!-- TABLE ENTRY --><tr name="highlightrow"><td>Pub1</td><td>SB</td><td style="background-color:#316745; color:white; width:7%; ">Success</td><td></td></tr><!-- END TABLE ENTRY --><!-- TABLE ENTRY --><tr name="highlightrow"><td>Pub2</td><td>SB</td><td style="background-color:#316745; color:white; width:7%; ">Success</td><td></td></tr><!-- END TABLE ENTRY --><!-- TABLE ENTRY --><tr name="highlightrow"><td>Designer</td><td>DB</td><td style="background-color:#316745; color:white; width:7%; ">Success</td><td></td></tr><!-- END TABLE ENTRY --><!-- TABLE ENTRY --><tr name="highlightrow"><td>DesignM</td><td>DB</td><td style="background-color:#316745; color:white; width:7%; ">Success</td><td></td></tr><!-- END TABLE ENTRY --><!-- TABLE ENTRY --><tr name="highlightrow"><td>D2 </td><td>DB</td><td style="background-color:#316745; color:white; width:7%; ">Success</td><td></td></tr><!-- END TABLE ENTRY --><!-- TABLE ENTRY --><tr name="highlightrow"><td>Differ</td><td>DB</td><td style="background-color:#316745; color:white; width:7%; ">Success</td><td></td></tr><!-- END TABLE ENTRY --><!-- TABLE ENTRY --><tr name="highlightrow"><td>Engine</td><td>DB</td><td style="background-color:#316745; color:white; width:7%; ">Success</td><td></td></tr><!-- END TABLE ENTRY --><!-- TABLE ENTRY --><tr name="highlightrow"><td>LogicDesigner</td><td>DB</td><td style="background-color:#316745; color:white; width:7%; ">Success</td><td></td></tr><!-- END TABLE ENTRY --><!-- TABLE ENTRY --><tr name="highlightrow"><td>ProdEngine</td><td>DB</td><td style="background-color:#316745; color:white; width:7%; ">Success</td><td></td></tr><!-- END TABLE ENTRY --><!-- TABLE ENTRY --><tr name="highlightrow"><td>SUN DB </td><td>SUN</td><td style="background-color:#316745; color:white; width:7%; ">Success</td><td></td></tr><!-- END TABLE ENTRY --><!-- TABLE ENTRY --><tr name="highlightrow"><td>SUN DB </td><td>SUN</td><td style="background-color:#316745; color:white; width:7%; ">Success</td><td></td></tr><!-- END TABLE ENTRY --><!-- TABLE ENTRY --><tr name="highlightrow"><td>SUN Differ</td><td>SUN</td><td style="background-color:#316745; color:white; width:7%; ">Success</td><td></td></tr><!-- END TABLE ENTRY --><!-- TABLE ENTRY --><tr name="highlightrow"><td>SUN </td><td>SUN</td><td style="background-color:#316745; color:white; width:7%; ">Success</td><td></td></tr><!-- END TABLE ENTRY --><!-- TABLE ENTRY --><tr name="highlightrow"><td>AIX DB </td><td>AIX</td><td style="background-color:#316745; color:white; width:7%; ">Success</td><td></td></tr><!-- END TABLE ENTRY --><!-- TABLE ENTRY --><tr name="highlightrow"><td>AIX DB </td><td>AIX</td><td style="background-color:#316745; color:white; width:7%; ">Success</td><td></td></tr><!-- END TABLE ENTRY --><!-- TABLE ENTRY --><tr name="highlightrow"><td>AIX Differ</td><td>AIX</td><td style="background-color:#316745; color:white; width:7%; ">Success</td><td></td></tr><!-- END TABLE ENTRY --><!-- TABLE ENTRY --><tr name="highlightrow"><td>AIX Engine</td><td>AIX</td><td style="background-color:#316745; color:white; width:7%; ">Success</td><td></td></tr><!-- END TABLE ENTRY --><!-- TABLE ENTRY --><tr name="highlightrow"><td>HPUX Differ</td><td>HPUX</td><td style="background-color:#316745; color:white; width:7%; ">Success</td><td></td></tr><!-- END TABLE ENTRY --><!-- TABLE ENTRY --><tr name="highlightrow"><td>HPUX Engine</td><td>HPUX</td><td style="background-color:#316745; color:white; width:7%; ">Success</td><td></td></tr><!-- END TABLE ENTRY --><!-- TABLE ENTRY --><tr name="highlightrow"><td>Linux DB </td><td>Linux</td><td style="background-color:#316745; color:white; width:7%; ">Success</td><td></td></tr><!-- END TABLE ENTRY --><!-- TABLE ENTRY --><tr name="highlightrow"><td>Linux DB </td><td>Linux</td><td style="background-color:#316745; color:white; width:7%; ">Success</td><td></td></tr><!-- END TABLE ENTRY --><!-- TABLE ENTRY --><tr name="highlightrow"><td>Linux Differ</td><td>Linux</td><td style="background-color:#316745; color:white; width:7%; ">Success</td><td></td></tr><!-- END TABLE ENTRY --><!-- TABLE ENTRY --><tr name="highlightrow"><td>Linux Engine</td><td>Linux</td><td style="background-color:#316745; color:white; width:7%; ">Success</td><td></td></tr><!-- END TABLE ENTRY --></table></div><p/><!--Anywhere--><div name="hidden" style="background-color: #EFEFEF; width: 500px; "><table style="width: 100%;"><tr style="background-color:white; "><td style="font-weight:bold; font-size:120%; " colspan="4">Anywhere</td></tr><tr><td style="border-bottom-color: black; border-bottom-style: solid; border-bottom-width: 1px; font-weight:bold; ">Project</td><td style="border-bottom-color: black; border-bottom-style: solid; border-bottom-width: 1px; font-weight:bold; ">Configuration:</td><td style="border-bottom-color: black; border-bottom-style: solid; border-bottom-width: 1px; font-weight:bold; ">Status:</td></tr><!-- TABLE ENTRY --><tr name="highlightrow"><td>Anywhere</td><td>SB</td><td style="background-color:#316745; color:white; width:7%; ">Success</td><td></td></tr><!-- END TABLE ENTRY --><!-- TABLE ENTRY --><tr name="highlightrow"><td>Anywhere2</td><td>SB</td><td style="background-color:#316745; color:white; width:7%; ">Success</td><td></td></tr><!-- END TABLE ENTRY --><!-- TABLE ENTRY --><tr name="highlightrow"><td>DDS.ear</td><td>SB</td><td style="background-color:#316745; color:white; width:7%; ">Success</td><td></td></tr><!-- END TABLE ENTRY --><!-- TABLE ENTRY --><tr name="highlightrow"><td>DesignServer</td><td>SB</td><td style="background-color:#316745; color:white; width:7%; ">Success</td><td></td></tr><!-- END TABLE ENTRY --></table></div><p/><!--Live_Table--><div name="hidden" style="background-color: #EFEFEF; width: 500px; "><table style="width: 100%;"><tr style="background-color:white; "><td style="font-weight:bold; font-size:120%; " colspan="4">Live</td></tr><tr><td style="border-bottom-color: black; border-bottom-style: solid; border-bottom-width: 1px; font-weight:bold; ">Project</td><td style="border-bottom-color: black; border-bottom-style: solid; border-bottom-width: 1px; font-weight:bold; ">Configuration:</td><td style="border-bottom-color: black; border-bottom-style: solid; border-bottom-width: 1px; font-weight:bold; ">Status:</td></tr><!-- TABLE ENTRY --><tr name="highlightrow"><td>Live</td><td>SB</td><td style="background-color:#316745; color:white; width:7%; ">Success</td><td></td></tr><!-- END TABLE ENTRY --><!-- TABLE ENTRY --><tr name="highlightrow"><td>Live</td><td>SB</td><td style="background-color:#316745; color:white; width:7%; ">Success</td><td></td></tr><!-- END TABLE ENTRY --><!-- TABLE ENTRY --><tr name="highlightrow"><td>Live</td><td>SB</td><td style="background-color:#316745; color:white; width:7%; ">Success</td><td></td></tr><!-- END TABLE ENTRY --></table></div><p/><!--Maint--><div name="hidden" style="background-color: #EFEFEF; width: 500px; "><table style="width: 100%;"><tr style="background-color:white; "><td style="font-weight:bold; font-size:120%; " colspan="4">Maintenance Notes</td></tr><tr><td style="border-bottom-color: black; border-bottom-style: solid; border-bottom-width: 1px; font-weight:bold; ">Project</td><td style="border-bottom-color: black; border-bottom-style: solid; border-bottom-width: 1px; font-weight:bold; ">Configuration:</td><td style="border-bottom-color: black; border-bottom-style: solid; border-bottom-width: 1px; font-weight:bold; ">Status:</td></tr><!-- TABLE ENTRY --><tr name="highlightrow"><td>Maintenance </td><td>SB</td><td style="background-color:#8F2E14; color:white; ">Failed</td><td></td></tr><!-- END TABLE ENTRY --></table></div><p/><!--Unacct_Table--><div name="hidden" style="background-color: #EFEFEF; width: 500px; "><table style="width: 100%;">

Can you please help

Thanks.


Report •

#3
November 2, 2009 at 16:27:35
M2,

I modified his html to make it valid.
I also set up a small test file of my own with 3 records.
I do not get Counts back from your bat file???
I get only the Date and 2 commas?

My small file called MyTest.txt has 3 records
Success
Success
Failed

The revised bat file based on yours.
@echo off & setLocal EnableDELAYedExpansion

for /f "tokens=* delims= " %%a in ('find /c /i "Success" ^< C:\Documents and Settings\NewAdmin\My Documents\MyTest.txt') do (
set S=%%a
)
for /f "tokens=* delims= " %%a in ('find /c /i "Failed" ^< C:\Documents and Settings\NewAdmin\My Documents\tmp.html') do (
set F=%%a
)
>> orange.txt echo %date% , !S!, !F!

The output:
Mon 11/02/2009 , ,


I also tried the Find statement in cmd window:
C:\Documents and Settings\NewAdmin\My Documents>Find /c /i "Success" MyTest.txt

---------- MYTEST.TXT: 2


It seems it is not getting to the files. Any ideas?
I've done some google searching for bat files with a find string and count but can not find the syntax to verify it or to try something different.

Thanks.


Report •

Related Solutions

#4
November 2, 2009 at 22:39:33
maybe try using "success"/"fail" as the delimiters? then test for value not null and if not null, increment a counter.
something like:
set delayed expansion...
for /f "tokens=1.. delims=success" %%a in (filename) do (
if %%a NEQ "" set !n!+=
)
i'm new to this, so my code is bad. also, obviously require
two file passes to check the two conditions of succ./fail.
i know this doesn't write the program. maybe point to direction to try.
ps: and i'm sure you're familiar with this, but just to mention: batch script breaks up files into lines based on crlf (car.ret/linefeed) and if the html use obscure variations it might foil the batch parser (for ex: lf-cr, or lf only, or cr only ...) I don't know, i haven't enough experience to say if this matters or not. my feeb attempt based on only one "success"/"fail" per line.

Report •

#5
November 3, 2009 at 04:39:52
[1] Good idea to make a simple test file. Bad idea to bury it in docu...

[2] The html pasted in by OP is too mangled to use. You can zip it up and email it to me.

[3] Keep in mind that a batch and FIND process BY LINE so we'll get a LINE COUNT. If what's needed is a WORD count, that's another story.


=====================================
Helping others achieve escape felicity

M2


Report •

#6
November 3, 2009 at 13:14:20
Hi M2,

Can you please send me your email id so that i can send the ZIP file to you.

Thanks.


Report •

#7
November 3, 2009 at 19:55:17
M2,

I'm trying to get the bat file you gave to work on a small sample.
My small file called MyTest.txt has 3 records ONLY
Rec# Content
1 Success
2 Success
3 Failed

The modified bat file is

orange1.bat

@echo off & setLocal EnableDELAYedExpansion

for /f "tokens=* delims= " %%a in ('find /c /i "Success" ^< C:\Documents and Settings\NewAdmin\My Documents\MyTest.txt') do (
set S=%%a
)
>> orange.txt echo %date% , !S!

The output goes to orange.txt
The result I get is:

Tue 11/03/2009 ,


I'm running XP pro sm3. Any advice appreciated.
Orange


Report •

#8
November 4, 2009 at 00:54:07
I have no way to test it at the moment but maybe this may help:

<untested>

@echo off
setlocal enabledelayedexpansion

for /f "usebackq delims=" %%a in ("yourfile.htm") do (
    set line=%%a
    if not "!line:^>=!"=="!line!" set line=!line:^>= !
    for %%b in (!line!) do >>tempfile.tempfile echo %%b
)

for /f %%a in ('type "tempfile.tempfile" ^| find /i "success" ') do set success=%%a

for /f %%a in ('type "tempfile.tempfile" ^| find /i "failure" ') do set failure=%%a

>> "output.csv" echo !date! !success! !failure!
del "tempfile.tempfile"
endlocal


The only thing I'm a little iffy on is the special characters. It could be done without a tempfile, but I couldn't see a way to do it without parsing it twice.....


Batch Variable how to


Report •

#9
November 4, 2009 at 03:02:20
I have actually managed to test this(the script counted 37
"sucess" and 1 "failed", M2's scripts probably work too.
The issue is line length, keep the lines in your html
file under 1000 characters and it should work:

The for loop will ignore lines that are too long...

@echo off
setlocal enabledelayedexpansion

for /f "usebackq eol= delims=" %%a in ("yourfile.htm") do (
    set line=%%a
    set line=!line:"=!
    set line=!line:^<= !
    set line=!line:^>= !
    set line=!line:^|= !
    for %%b in (!line!) do >>tempfile.tempfile echo %%b

)

for /f "tokens=2 delims=:" %%a in ('find /c /i "success" "tempfile.tempfile" ') do set /a success=%%a


for /f "tokens=2 delims=:" %%a in ('find /c /i "failed" "tempfile.tempfile" ') do set /a failure=%%a

>> "output.csv" echo !date! !success! !failure!
del "tempfile.tempfile"
endlocal
goto :eof


Batch Variable how to


Report •

#10
November 4, 2009 at 07:24:45
orange,

My first comment in #5 is for you.


=====================================
Helping others achieve escape felicity

M2


Report •

#11
November 4, 2009 at 07:34:52
I got the html in the mail. [Not zipped up. LOL] It's only got 7 lines, 5 of which are about 2000 chars long. So of course it doesn't work.


=====================================
Helping others achieve escape felicity

M2


Report •

#12
November 4, 2009 at 07:43:30
Hi M2,

Sorry for not zipping it up, ya the html is not in proper format , it is getting created by a JAVA program, when i am opeing that html file in notepad++ or notepad it is showing me all code in one line.

Thanks.


Report •

#13
November 4, 2009 at 08:58:38
M2,

I saw your comment in #5.
My stuff in #7, was trying to get your .bat file to work.
I created a file with only 3 records.
Modified the bat
and get no counts in output.

I'm just trying to understand the .bat, especially the For statement with the FIND.

Any help would be appreciated.

I'm not trying to bury this request, I thought it was part of the original since that's where your .bat was given.

Thanks in advance.

orange


Report •

#14
November 4, 2009 at 09:15:10
Hi all,

Thanks to all of you for putting the effort and time for me, i did a little tweak , and solved the problem i splited the HTML file with perl script (spl.pl) to write each line lessthan 100 chars and created a new HTML file(mod.HTML) and ran the batchscript which you people gave me (fs.bat) ,i put the perl (sp.pl)and batch scritpt(fs.bat) in one batch script (man.bat) and ran the man.bat batch script it ran fine i got the expected results.

Thanks once again to all.

Thanks.



Report •

#15
November 6, 2009 at 11:01:43
Hi all,

i am trying to calculate the percenstage of the Success Count , i am not getting the output to the excel file., i am using the following script made changes but still i am not getting the output written to the excel file , i want the percentage succes in the other excel file.

@echo off & setLocal EnableDELAYedExpansion
for /f "tokens=* delims= " %%a in ('find /c /i ">Success<" ^< C:\text\man.html') do (
set S=%%a
)
for /f "tokens=* delims= " %%a in ('find /c /i ">Failed<" ^< C:\text\man.html') do (
set F=%%a
)
set /pc=(!S!*100)/(!S!+!F!)
>> my.csv echo %date:~4,14%, !S!, !F!, !pc!
>> per.csv echo !pc!

Can you please help where i am doing mistake.

Thanks.


Report •

#16
November 6, 2009 at 12:30:01
If it's the same file, the lines are too long for a bat script.


=====================================
Helping others achieve escape felicity

M2


Report •

#17
November 6, 2009 at 16:41:19
set /pc=(!S!*100)/(!S!+!F!)

Don't you mean "set /a pc=..."?


Batch Variable how to


Report •

#18
November 9, 2009 at 06:51:01
ThanksM2
the file is MOD.html which i have created using the perl(character per line is lessthan 100)

Thank Judago,
That worked.


Thanks.


Report •

#19
November 29, 2009 at 07:44:49
Another script.

# Script SuccessFailed.txt
var str content
cat "http://........." > $content
echo gettime() "," { sen -c "^success^" $content } "," { sen -c "^failed^" $content } >> "C:/count.csv"

Will get the html directly from the web site (use the correct URL starting with http://). Can also specify local file. Will do a case-insensitive search (-c), append timestamp and counts in CSV format to file C:/count.csv.

Save the script in file C:/Scripts/SuccessFailed.txt. Schedule the following command in task scheduler to do this automatically hourly.

"C:/biterScripting/biterScripting.exe" "C:/Scripts/SuccessFailed.txt"



Report •


Ask Question