2 Methods to Count Lines in a File on Windows

2 Methods to Count Lines in a File on Windows

When working with files processing via command line on Windows, may need to count lines in a file. It can help to determine how large a given file is. This tutorial shows 2 methods how to count lines in a file on Windows.

In order to test, create a new file:

(echo Line1& echo Line2& echo Line3& echo Line4) > test.txt

Method 1 - CMD

To count lines in a file, use type command to read the content of a file and find command to count lines:

type test.txt | find /c /v ""

Combination of parameters /c and /v in the find command means that need to display a count of the lines that don't contain the specified string. In our case empty string "".

Method 2 - PowerShell

In PowerShell, the Measure-Object command can be used with -Line parameter in order to count the number of lines:

type test.txt | Measure-Object -Line | %{$_.Lines}

Leave a Comment

Cancel reply

Your email address will not be published.