sexta-feira, outubro 12, 2007

Writing a Text Files Line Counter in C#

A friend asked me today if I knew an application that could count all the lines in all .php files in a directory, and my answer was no, but I can make one in five minutes, and thats all it took.

Bellow is the code, it's so simple that I'm lazy to explain, enjoy:


DirectoryInfo dInfo = new DirectoryInfo(args[0]);
FileInfo[] Files = dInfo.GetFiles("*.php");
int i = 0;
foreach (FileInfo fInfo in Files)
{
string[] temp = File.ReadAllLines(fInfo.FullName);
i += temp.Length;
}
Console.WriteLine("Total Lines: " + i);
Console.ReadLine();


It's dirty but it works, if you have a better solution, please comment, I'll be pleased to learn a better way to do this.

2 comentários:

tasuki disse...

#!/bin/bash
wc -l *.php

Not sure if it's better, but sure as hell it's simpler :)

fabio.gomes disse...

Well, sadly windows doesn't have bash script, maybe someone could hack a batch script to do the same...