Solved

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

Posted on 2014-04-23
6
343 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
  • 3
  • 3
6 Comments
 
LVL 83

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 83

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 83

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

914 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now