just one more geek in a sea of austin techies

April 21, 2018

Quick line count via Windows command line #DevGeek

Every so often I need to get a quick line count estimate across a collection of files. Line counts are helpful in broadly defining the scope of reviewing or updating existing code files.  In such cases I only need a ballpark estimate of total lines of code across multiple files.  It turns out that all we need is the Windows command prompt and a one-line command:

At first glance this solution isn't overly intuitive but it's actually quite simple.  The TYPE command parses each line in the specified file and pipes each line through the FIND command which searches for string values matching the specified string.

In this case, we extend the TYPE command's usefulness from parsing just one file to parsing collections of files by using a wildcard search for all files ending with ".log".

For the FIND command, we use the "/V" switch to invert behavior and have the command look for lines that *don't* have the specified string.  We use the "/C" switch to tell FIND to give us a count of matched (unmatched in this case) lines instead of the content of the lines.  Finally we specify any string value we can reasonably expect to *not* exist in our searched files.  I used "zzxxccvvbbnnmm" in my example.

Here's the result of the command on a directory where I placed two sample log files:

See?  Quick and simple as long as you can remember the "/V" and "/C" switches.

Counting lines of files across multiple file types
What if we need to count lines between files of differing file types?  For instance, if I'm counting lines in both ".log" files as well as ".xml" files?  Easy: just specify the additional file search as part of the TYPE portion of the command:

For your copy-and-paste convenience, here's some copy-ready text:

  TYPE c:\temp\*.log | FIND /V /C "zzxxccvvbbnnmm"