Solved

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

Posted on 2014-04-23
6
348 Views
Last Modified: 2014-04-23
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?
0
Comment
Question by:GouthamAnand
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 84

Accepted Solution

by:
oBdA earned 500 total points
ID: 40018743
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
 

Author Comment

by:GouthamAnand
ID: 40018776
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
 
LVL 84

Expert Comment

by:oBdA
ID: 40018786
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
[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

 

Author Comment

by:GouthamAnand
ID: 40018807
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
 
LVL 84

Expert Comment

by:oBdA
ID: 40018821
Just call it like in my example above: Whatever.cmd "S:\ome\folder\*.txt" or Whatever.cmd "S:\ome\folder\*.sql"
0
 

Author Closing Comment

by:GouthamAnand
ID: 40018826
Thanks a lot.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Suggested Courses

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question