-1

I'm trying to store number occurrence of a value to count variable.

set ATTR_TO_VERIFY=W10SBRS
for /F "tokens=*" %%N  in ('findstr /r "^<.*^>.*%ATTR_TO_VERIFY%.*:" lofile.log ^| find /v "" /c') do set "COUNT=%%N"
echo %COUNT%

but count always give me value zero.

Nishabu
  • 134
  • 1
  • 15

1 Answers1

0

The regex error that @MCDN pointed out in the comments is the one causing the trouble.
You can also do it without the find /c. To count the number of lines in the result of your findstr command the following should do:

set ATTR_TO_VERIFY=W10SBRS
for /F "tokens=*" %%N in ('findstr /n /r "<.*>.*%ATTR_TO_VERIFY%.*:" lofile.log') do set /a COUNT+=1
echo %COUNT%

The FOR /F automatically discards empty lines and lines starting with a ; though. A way to solve this is to add the /n flag to the findstr and let findstr add the line numbers at the beginning of each matched line.

Community
  • 1
  • 1
J.Baoby
  • 2,167
  • 2
  • 11
  • 17
  • 2
    Just to be precise, the original code in the question does not *need* `tokens` nor `delims` clauses. The output of the `findstr` command is piped to a `find /v "" /c` command that directly returns the number of matching lines returned by the `findstr` command. – MC ND Feb 03 '17 at 14:30
  • Of course...I had the `findstr /n` in mind that adds the line numbers instead! Terrible mistake. Thanks for remembering me – J.Baoby Feb 03 '17 at 15:33