How do I get the total number of lines of all the files in a directory ?

Hi,

How do I get the total number of lines of all the files in a directory ? - in DOS command prompt?

if there are 3 files , say a.txt, b.txt and c.txt each having 30 lines. Then I want to see the total 90. Do we have any command to do this?

Also how do I get the total no of lines of a specified file - in DOS command prompt?
GouthamAnandAsked:
Who is Participating?
 
oBdACommented:
For a single file:
type "a.txt" | find /c /v ""

Open in new window

And here's a script to sum; just pass the files or folders as command line arguments (include in double quotes if the names contain spaces): Whatever.cmd a.txt b.txt c.txt or Whatever.cmd "S:\ome\folder\*.txt"
@echo off 
setlocal enabledelayedexpansion
set /a Total = 0
:ArgLoop
for /f "delims=" %%a in ('dir /b /a:-d "%~1"') do (
	for /f %%c in ('type "%%~a" ^| find /c /v ""') do set Lines=%%c
	echo [!Lines!] - '%%~fa'
	set /a Total += Lines
)
if not "%~2"=="" (
	shift
	goto ArgLoop
)
echo Total: %Total%

Open in new window

Edit: changed the script to support folders.
0
 
GouthamAnandAuthor Commented:
The number of files are lot many. So I cannot pass all the names as arguments. But I can pass the directory name.

Please suggest me now.
0
 
oBdACommented:
Sorry, I concentrated too much on your examples in my first attempt; I already changed the script to now support folders as well. Please copy it again from above.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
GouthamAnandAuthor Commented:
Thanks a lot. But I have one more constraint - I want to include only certain types of files like *.txt or .sql to take into consideration in that directory.

Can you please suggest me now.
0
 
oBdACommented:
Just call it like in my example above: Whatever.cmd "S:\ome\folder\*.txt" or Whatever.cmd "S:\ome\folder\*.sql"
0
 
GouthamAnandAuthor Commented:
Thanks a lot.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.