Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2014-04-23
6
Medium Priority
?
352 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 85

Accepted Solution

by:
oBdA earned 2000 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 85

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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 

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 85

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Screencast - Getting to Know the Pipeline

610 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