Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Total number of lines in a file

604 views
Skip to first unread message

bigdave

unread,
Apr 16, 2006, 12:20:01 PM4/16/06
to
I'm attempting to create a bat file which will return the total number
of lines in a .txt file. I'm attempting to use FIND /C to do this, and
have find just search for any character, or a newline character, but I
can't find any reference to FIND being able to use wildcards, and the
newline character doesn't seem to work either.. I would like to do
something like:

FIND /C "*" *.txt

Is there a way to enter wildcards into FIND? Or is there a better way
to get total number of lines, with or without find? Thanks!

foxidrive

unread,
Apr 16, 2006, 12:42:11 PM4/16/06
to

find /c /v "" < file.txt

but it has a bug which returns one line too few (I think when two blank
lines are at the end of a file).

This works though.
@echo off
for /f "delims=:" %%a in ('findstr /n "^.*$" file.txt') do set num=%%a

Timo Salmi

unread,
Apr 16, 2006, 1:28:59 PM4/16/06
to
bigdave wrote:
> I'm attempting to create a bat file which will return the total number
> of lines in a .txt file.

64} How to count the number of lines in a file, empty lines inclusive?
157253 Mar 9 2006 ftp://garbo.uwasa.fi/pc/link/tscmd.zip
tscmd.zip Useful NT/2000/XP script tricks and tips, T.Salmi

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Timo's FAQ materials at http://www.uwasa.fi/~ts/http/tsfaq.html

foxidrive

unread,
Apr 17, 2006, 12:29:17 AM4/17/06
to
On Sun, 16 Apr 2006 20:28:59 +0300, Timo Salmi wrote:

> bigdave wrote:
>> I'm attempting to create a bat file which will return the total number
>> of lines in a .txt file.
>
> 64} How to count the number of lines in a file, empty lines inclusive?
> 157253 Mar 9 2006 ftp://garbo.uwasa.fi/pc/link/tscmd.zip
> tscmd.zip Useful NT/2000/XP script tricks and tips, T.Salmi
>
> All the best, Timo

You might like to note my comment about using find, to add to your FAQ.

find /c /v "" < file.txt

It has a bug which returns one line too few when two blank
lines are at the end of a file.

Timo Salmi

unread,
Apr 17, 2006, 2:33:47 AM4/17/06
to
foxidrive wrote:
> On Sun, 16 Apr 2006 20:28:59 +0300, Timo Salmi wrote:
>> 64} How to count the number of lines in a file, empty lines inclusive?
>> 157253 Mar 9 2006 ftp://garbo.uwasa.fi/pc/link/tscmd.zip

> You might like to note my comment about using find, to add to your FAQ.

Thank you for the welcome notification. Gladly. I have added to the
item

Other options:
@echo off & setlocal enableextensions
:: Make a test file
for %%f in (myfile.txt) do if exist %%f del %%f
for %%f in (first second third fourth) do (
echo This is the %%f line>>myfile.txt)
echo.>>myfile.txt
for %%f in (sixth seventh eight) do (
echo This is the %%f line>>myfile.txt)
echo.>>myfile.txt
echo.>>myfile.txt
::
:: Count the lines
for /f %%n in ('find /c /v "" ^< myfile.txt') do set lineCount1=%%n
echo There are %lineCount1% lines in myfile.txt
::
for /f "delims=:" %%n in ('findstr /n "^.*$" myfile.txt') do (
set lineCount2=%%n)
echo There are %lineCount2% lines in myfile.txt
::
:: Clean up
for %%f in (myfile.txt) do if exist %%f del %%f
endlocal & goto :EOF
The output will be
C:\_D\TEST>cmdfaq
There are 9 lines in myfile.txt
There are 10 lines in myfile.txt
The first method may produce a faulty results.

and

References/Comments:
http://www.google.com/groups?selm=1rzseucveq575$.53nvvmu63px4.dlg%4040tude.net

0 new messages