Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refactoring: Count total lines in all files under a given folder

I wrote a code to count the number of lines in all the files in a given folder. It works fine, but I am trying to include all the possible C#'s features to refactor it to more compact and efficient code. Please help me do that.

Here is the code.

    class LineNumberCounter
{
    public static string Calculate(string folderPath, string pattern = "*.txt")
    {
        DirectoryInfo dirInfo = new DirectoryInfo(folderPath.Trim());
        if (!dirInfo.Exists)
            throw new ArgumentException("No such directory exists");

        StringBuilder returnValue = new StringBuilder();
        long totalLines = 0;

        pattern.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).All(filter =>
        {
            int count = 0;
            dirInfo.GetFiles(filter.Trim(), 
                SearchOption.AllDirectories).All(file =>
                {
                    using (StreamReader reader = file.OpenText())
                    {
                        for (; reader.Peek() > -1; count++)
                            reader.ReadLine();
                    }
                    returnValue.AppendLine(string.Format("Number of lines with {0} pattern is {1}",
                        filter, count));
                    totalLines += count;

                    return true;
                }
            );

            return true;
        });

        //foreach (string filter in
        //    pattern.Split(new char[] { ';' },
        //        StringSplitOptions.RemoveEmptyEntries))
        //{
        //    FileInfo[] files = dirInfo.GetFiles(filter.Trim(),
        //        SearchOption.AllDirectories);
        //    int count = 0;
        //    Array.ForEach<FileInfo>(files, file =>
        //    {
        //        using (StreamReader reader = file.OpenText())
        //        {
        //            for (; reader.Peek() > -1; count++)
        //                reader.ReadLine();
        //        }
        //    });

        //    returnValue.AppendLine(string.Format("Number of lines with {0} pattern is {1}",
        //        filter, count));
        //    totalLines += count;
        //}

        returnValue.AppendLine();
        returnValue.AppendLine("Total Lines = " + totalLines);

        return returnValue.ToString();
    }
}

Commented lines were the ones I wrote originally. I made some attempt to refactor it. But still want to check if it has any more scope.

like image 354
SaravananArumugam Avatar asked Dec 22 '22 04:12

SaravananArumugam


2 Answers

Using new >=.NET 4 method File.ReadLines()

int total = File.GetFiles(folderPath, pattern)
                .Sum(x => File.ReadLines(x).Count());

Some considerations from MSDN:

The ReadLines and ReadAllLines methods differ as follows: When you use ReadLines, you can start enumerating the collection of strings before the whole collection is returned; when you use ReadAllLines, you must wait for the whole array of strings be returned before you can access the array. Therefore, when you are working with very large files, ReadLines can be more efficient.

like image 94
sll Avatar answered Dec 31 '22 07:12

sll


foreach (var filePath in Directory.GetFiles(folderPath, pattern(//standard pattern), SearchOption.AllDirectories))
{
    var count=File.OpenText(filePath).ReadAllLines().Count();
    returnValue.AppendLine(string.Format("Number of lines with {0} pattern is {1}",
            Path.GetExtension(filePath), count));
}
like image 24
Reza ArabQaeni Avatar answered Dec 31 '22 06:12

Reza ArabQaeni