Counting lines in Windows command prompt

By | September 10, 2011

How to easily count output lines in cmd.exe with the builtin FIND tool.

When working in a command line environment it is sometimes very useful to be able to count the lines of output from different tools. Many Unix/Linux operating systems contains the wc tool with many options and there is no real builtin alternative in Windows, however some of same capabilities exists native in the Windows Command Prompt (cmd.exe).

In this short post we will see how we can use the FIND utility for counting lines in cmd.exe. The find tool, somewhat similar to grep in Unix, has been around since MS-DOS and is easy to use since it is included by default in every Windows installation.

Assume for example that we have a Windows Server and want to see how may TCP sessions that is active for the moment. This could be studied with the netstat command and piped into FIND to grep the established sessions.

netstat -nao | find /i “estab”

Established TCP sessions

The output from this could quite overwhelming with hundreds of lines and we might only be interesting in the number of sessions. By just appending the switch /c to the command line we will get the number of open TCP sessions.

Established TCP sessions
We will still use the filter (in this case look for the string “estab” to find only lines containing the ESTABLISHED state) but together with /c the tool will only display the number of matching lines.
ipconfig /displaydns

Another short example could be the notice the number to locally cached dns records, see output above.

Find and count AD groups

The /c option could also be used to count all lines that a commands leaves as output. For example we would like to know the number of groups in Active Directory. By using a pipe to FIND /v “” /c we want to see all lines that does not (/v) match the string “”, i.e. all lines and then count it. By using /v “” we will grep every non-blank line, which could be useful if the output strings are very different. If you have used the Unix tool wc this would be quite like wc -l.

Another example with a command that displays a very large number of output line would be the Event Logger command line tool wevutil. If just wanting to know the number of different logs in modern Windows system we could pipe the names of several hundreds of log files into FIND /v “” /c.

A final example could be that we have a log file or similar with perhaps thousands of lines in total and we quickly want to know the amount of lines with certain key phrases.

TYPE C:\Windows\Schedlgu.txt | FIND /i “task failure” /c

It should be noted that the Unix tool wc has a number of Win32 ports available for free, but often it is most useful to know what could be accomplished from the builtin tools available on all Windows, even legacy versions.

13 thoughts on “Counting lines in Windows command prompt

  1. Pablo Alejandro Hamann

    Thanks! Very useful.

    One question, how can i store the value that FIND returns in a variable??
    I used the next command for count the number of images present in a Wim file:
    dism /Get-WimInfo /wimfile:M:\sources\install.wim /English | FIND /C /I “Index”

    But i need storage this number in a variable. So, i tried:
    SET IN=` dism /Get-WimInfo /wimfile:M:\sources\install.wim /English | FIND /C /I “Index” `

    but no luck. FIND answer: File not found: `

    Any ideas?


    1. Rickard Nobel Post author

      Hello Pablo,
      and thank you for your comment.

      You could use the FOR command to capture the line count into a variable. An example:

      dir C:\ | find /v /c “” > tmp1.txt

      FOR /f %%a IN (tmp1.txt) DO SET number=%%a

      ECHO %number%

      So just replace the line before the > tmp1.txt with your dism command and try it. The line count will here be placed into a variable called “number”, but that could of course be changed to what you find suitable.

      Regards, Rickard

      1. donjoe

        You don’t need FOR to get file contents into a variable, SET already knows how to do that:

        SET /P number=<tmp1.txt

        In fact, SET /P can be used to ECHO text without a newline char at the end (so you can continue to write to the same line afterwards):

        SET /P __DummyVar="And the winner is… "<NUL
        :: Insert here the algorithm to determine the winner and then…
        ECHO Peter Dinklage!


      2. donjoe

        … but what you _could_ use FOR /F to do is avoid writing the value to a file and just put it straight into the variable:

        FOR /F %%g IN (‘dir /a-d /s /b C:\ ^| find /v /c “”’) DO SET numberoffiles=%%g
        ECHO %numberoffiles%

        1. Rickard Nobel Post author

          Hello Donjoe,

          thanks a lot for your comments. Always interesting with different tricks in classic CMD. :)

          I tend to use FOR for the reason of sometimes reading multiple lines and doing some manipulations of them, however the variant with SET /P number=<tmp1.txt when only getting one line/item was good!

          Best regards,

  2. Quan

    I want to count all the lines after a command ran , however, I cannot get the integer value to echo. Could you please help?

    Example: ping | find /v /c “”
    Result is: 11
    Then I want to echo There are 11 lines.

    1. Rickard Nobel Post author

      Hello Quan,

      thank you for your question. If you create a batch file to run the commands you could do it like this:

      PING | FIND /v /c “” > tmp1.txt
      SET /P lines=<tmp1.txt
      ECHO There are %lines% lines.

  3. JahanS

    Not bad at all.
    I use Linux for most of my tasks but this command can prove to be useful if I am not running cygwin on the Windows box lol.


Leave a Reply

Your email address will not be published. Required fields are marked *