Computing.Net > Forums > Programming > Batch File To Find The String Count

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch File To Find The String Count

Reply to Message Icon

Name: SCMBUILD
Date: October 30, 2009 at 13:27:38 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: October 31, 2009 at 01:24:01 Pacific
Reply:

@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


0

Response Number 2
Name: SCMBUILD
Date: November 2, 2009 at 07:22:09 Pacific
Reply:

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.


0

Response Number 3
Name: orange
Date: November 2, 2009 at 16:27:35 Pacific
Reply:

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.


0

Response Number 4
Name: nbrane
Date: November 2, 2009 at 22:39:33 Pacific
Reply:

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.


0

Response Number 5
Name: Mechanix2Go
Date: November 3, 2009 at 04:39:52 Pacific
Reply:

[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


0

Related Posts

See More



Response Number 6
Name: SCMBUILD
Date: November 3, 2009 at 13:14:20 Pacific
Reply:

Hi M2,

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

Thanks.


0

Response Number 7
Name: orange
Date: November 3, 2009 at 19:55:17 Pacific
Reply:

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


0

Response Number 8
Name: Judago
Date: November 4, 2009 at 00:54:07 Pacific
Reply:

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


0

Response Number 9
Name: Judago
Date: November 4, 2009 at 03:02:20 Pacific
Reply:

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


0

Response Number 10
Name: Mechanix2Go
Date: November 4, 2009 at 07:24:45 Pacific
Reply:

orange,

My first comment in #5 is for you.


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

M2


0

Response Number 11
Name: Mechanix2Go
Date: November 4, 2009 at 07:34:52 Pacific
Reply:

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


0

Response Number 12
Name: SCMBUILD
Date: November 4, 2009 at 07:43:30 Pacific
Reply:

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.


0

Response Number 13
Name: orange
Date: November 4, 2009 at 08:58:38 Pacific
Reply:

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


0

Response Number 14
Name: SCMBUILD
Date: November 4, 2009 at 09:15:10 Pacific
Reply:

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.



0

Response Number 15
Name: SCMBUILD
Date: November 6, 2009 at 11:01:43 Pacific
Reply:

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.


0

Response Number 16
Name: Mechanix2Go
Date: November 6, 2009 at 12:30:01 Pacific
Reply:

If it's the same file, the lines are too long for a bat script.


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

M2


0

Response Number 17
Name: Judago
Date: November 6, 2009 at 16:41:19 Pacific
Reply:

set /pc=(!S!*100)/(!S!+!F!)

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


Batch Variable how to


0

Response Number 18
Name: SCMBUILD
Date: November 9, 2009 at 06:51:01 Pacific
Reply:

ThanksM2
the file is MOD.html which i have created using the perl(character per line is lessthan 100)

Thank Judago,
That worked.


Thanks.


0

Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Batch File To Find The String Count

Batch File to find files www.computing.net/answers/programming/batch-file-to-find-files/17076.html

Dos Command/Batch file to find a fo www.computing.net/answers/programming/dos-commandbatch-file-to-find-a-fo/17342.html

batch file to find and copy www.computing.net/answers/programming/batch-file-to-find-and-copy/9236.html