Link Details

Link 138084 thumbnail
User 225256 avatar

By mswatcher
via blogs.msdn.com
Published: Dec 03 2008 / 06:40

I wanted to sum the total lines of code in files in a given folder. I thought that writing my own program to do this would be faster than looking for it on the internet, so here's what I came up with
  • 6
  • 5
  • 1932
  • 0
User 306525 avatar

Dave Newton replied ago:

1 votes Vote down Vote up Reply

Spoiler: here's the one line of code.


using System;
using System.Linq;
using System.IO;

class Program
{
static void Main(string[] args)
{
Console.WriteLine(
Directory.GetFiles(
Environment.CurrentDirectory, "*",
string.Join(" ", args).Contains("/s")
? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)
.Select(f => File.ReadAllLines(f).Length)
.Sum());
}
}


Here's mine:


find . -name "*" | xargs wc


Throw a tail in there if you only want the sum, a -l if you only want the lines.

Apparently somebody already thought of that.

User 310113 avatar

b.m replied ago:

3 votes Vote down Vote up Reply

Your code should be:
find . -name "*" -type f | xargs wc

Plus you need to take into account the "recursive" /s parameter (in your case to ignore it). I've just posted my approach in groovy on dzone regarding this issue.

User 306525 avatar

Dave Newton replied ago:

0 votes Vote down Vote up Reply

Ah, good point--I missed that when scanning the one line. Thank goodness for -maxdepth :D

Add your comment


Html tags not supported. Reply is editable for 5 minutes. Use [code lang="java|ruby|sql|css|xml"][/code] to post code snippets.

Apache Hadoop
Written by: Piotr Krewski
Featured Refcardz: Top Refcardz:
  1. Play
  2. Akka
  3. Design Patterns
  4. OO JS
  5. Cont. Delivery
  1. Play
  2. Java Performance
  3. Akka
  4. REST
  5. Java