Batch File to count values within a txt file

Jofnn
Jofnn used Ask the Experts™
on
Hi,

I'm working on a batch file at the moment and would like it to read a text file and confirm how many times it has a certain value... i.e. "OUTLOOK.EXE"

Can you advise?

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Test your restores, not your backups...
Top Expert 2016
Commented:
The easiest way to do this is to use the FIND command and have it output just the count of hits.  SO something like this:

FIND /C /I "OUTLOOK.EXE" "c:\temp\textfile.txt"

~bp

Author

Commented:
Absolutely perfect!!

Thinking about that... I already knew that!! (I just could not think of it for anything)  Cheers BillPrew
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
No problem, glad that helped.
~bp
How to Generate Services Revenue the Easiest Way

This Tuesday! Learn key insights about modern cyber protection services & gain practical strategies to skyrocket business:

- What it takes to build a cloud service portfolio
- How to determine which services will help your unique business grow
- Various use-cases and examples

Author

Commented:
On an extra note (which is one I don't know) is how to strip the main line off and only return a number...

(i.e. it returns with  "-------------  \\server\share\docname.txt: 2")

I'd rather dump the value (so 2 in this instance) into a new variable, which can be re-called... ideas?

Commented:
I'm sure Bill would tel you in a minute but I'd just opened this Q up anyway not knowing it was answered already. Try this:
for /f "tokens=2 delims=:" %%a in ('FIND /C /I "OUTLOOK.EXE" "c:\temp\textfile.txt"') do set /a count=%%a

Author

Commented:
I tried that with an echo on the end to echo %count%... Attempt one returned "Missing Operator" and a value of 0...

I amended the line slightly to be (...)"tokens=2 delims=: "(...)  (not the added space after the colon).  This corrected the "Missing Operator" message, but returns a value of 0 for everything, even though I know there are at least 1...

clues?

Commented:
on the mobile at the moment but should work - I tested before posting with only changing the filename and find string.

The count variable is not available until the NEXT line unless you use

setlocal enabledelayedexpansion
and use !count!  

Anyway out at mo so maybe Bill can hep or will look later or post a new Q
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
As long as your filename doesn't contain spaces, this shoould work okay:

for /f "tokens=3" %%a in ('FIND /C /I "OUTLOOK.EXE" "c:\temp\textfile.txt"') do set /a count=%%a

Steve,

I think you neglected to consider the fact that if a full pathname is specified to FIND, then it will echo that in the output, and there for have an extra colon after the drive letter.  So for now I'll assume there is no spaces in the pathname unless Jofnn sees a problem with that.  We can of course work around that, just trying to keep it simple.  Thanks for lending a hand, I was off at lunch, and then getting my H1N1 shot, woo hoo.

~bp

Commented:
Good point, I didn't use a full path.  I suppose another way around that is to use redirection with the for, i.e.
for /f %%a in ('FIND /C /I "OUTLOOK.EXE" ^<"c:\temp\textfile.txt"') do set /a count=%%a
and then it doesn't display the filename at all

Author

Commented:
Thanks guys,

The filenames don't have spaces in them and are called as variables... (full location is "%temp%\QSA_QUERY.QFL" which would be called by variable %tempfile%

I'll check those codes out tomorrow when I'm in the office with the apps that I'm building and will get back to you both.

Thanks again, all the help is appreciated!

Jofnn

Author

Commented:
BillPrew,

That was perfect and works a treat!

Thanks again for all your assistance and additional thanks to dragon-it for helping as well :)

Jonathon

Commented:
No prob!

Steve

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial