Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I'm using the following command
for /F %x in ('findstr /r /n "Output" C:\test\sasi.log | find /C ":"')
do set XXX=%x
echo %xand get the error message as
"| was unexpected at this time."Actually I'm trying to get the number of lines which contains the word "Output" in the file "sasi.log" and trying to store the result in a variable. (which will be finally used as exit code of the bat file)
Any help would be appreciated.
Skumar.

The pipe and redirector when coded inside the For /F clause must be prefixed by the caret (^), i.e.
C:\test\sasi.log ^| find /C ":"You are in a Win XP/2K window NOT under DOS (that doesn't exist at all in NT based systems) and For /F is a NT batch statement unknown to plain DOS.
Code %%x too NOT %x and at first sight more errors may be present.

Thanks much!!! Adding the ^ symbol worked.
Yes. I'll use %%x when it is used in BAT file. But when used enter it from command prompt %x has to be used. Correct me if I am wrong.
Once again thanks much for your response.
Skumar.

Why do you not code the following?
for /F %x in ('find /C "Output" ^< C:\test\sasi.log')do set XXX=%x
echo. %XXX%as you are not looking for "regular expressions".

I was under the impression that
find /C "Output" ^< C:\test\ftp_troy.log
will return the value in the below format. Since I needed only the count I went for the other option.
---------- C:\TEST\sasi.LOG: 2
But now I tried your options and it works good. Just one more question. Can it me used to search multiple words???For Example
find /C "Output sasi" ^< C:\test\ftp_troy.log
Will it search for two individual words "Output" and "sasi" and retrieve the total count???
Skumar.

set XXX=0
for %i in (Output sasi) do (
for /F %j in ('find /C "%i" ^< C:\test\ftp_troy.log') do set /A XXX+=%j
)
echo.%XXX%As you can see the goal is achieved by embracing the parsing For /F statement with a plain For to trig a loop and performing arithmetics on the var XXX via the /A switch.
Type set /? to know more and beware dynamic variables in for statements (beyond this short note).

Hi IVO,
I love it
=====================================
If at first you don't succeed, you're about average.M2

If the file ftp_troy.log is very large, finding multiple strings using Ivo's code will read the same file multiple times and would therefore take longer than doing it using findstr, which only reads through the file once while searching for multiple strings. The only problem is findstr can't count the lines. Which is why Skumar piped it to "find", which can count.

I want to point out that the findstr/find and find standalone methods lead to different results as they actually count the number of LINES holding the strings NOT the strings themselves.
So if you have the file
After Print Print
After Afterthe findstr/find results in 3 while find outputs 4. The first counts lines holding After OR Print while the second takes into account the SUM of the lines with After and those with Print.
So better to clarify accurately what we want to count and how.

Good point Ivo. There's also the possibility of needing the count of individual occurrences of all the words, i.e. 6 in your example. Neither the "findstr/find" nor the "find" method gives us this.

I really appreciate your explanation on this IVO. My requirement is to find either of words present in the file. So I would go for findstr.
And for Klint question beleive we need to introduce counter for calculating the individual occurrences of all words.
Skumar.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |