Count the Number of Lines in a Text File
I have of late, wherefore I know not, started writing little Batch programs. Batch is ugly and buggy, but at the same time its raw minimalism makes it capable of performing surprisingly complicated tasks. Ah, but knowing how to exploit this power, therein lies the rub!
Let's start with the seemingly straightforward task of counting the lines in a text file and storing the result in a variable.
Counting the Number of Lines
Sometimes it's useful to know the number of lines in a text file. You might be tempted to try something like:
find /c /v "" < file.txt
This works because the file is redirected (that's what the < is for) into
find doesn't know the file's name and can't print it. Type in the command above again, but this time leave out the less than (<) and you'll see what I mean.
/v "" switch means display all lines that don't contain anything ("" is the empty string). In other words, display all lines. And
/c means display the count of the lines that matched.
But this doesn't always give the correct number of lines for files containing trailing newlines or files where the last line isn't terminated by a newline.
The “belt and suspenders” solution is to
type the file and pipe the output into
find as below:
type file.txt | find /c /v ""
Storing the Result in a Variable
Great, but that just outputs a number. How do you store the number of lines in a variable? The
for /f loop to the rescue!
for /f "tokens=1" %%c in ('type file.txt ^| find /c /v ""') ^ do set lines=%%c
The number of lines in
file.txt is stored in the
lines variable. The pipe (|) character is escaped with a circumflex (^) inside the
for /f loop's
in (...) clause. The circumflex at the end of the line means that the current line is continued onto the next line. Loop variables like
%%c in the example above must be preceded by a single per cent (%) on the command line and two per cents (%%) in a Batch file.
The above example only holds true for plain text files with Windows line endings. Results for files with different line endings or containing troublesome control characters may not always be reliable.