Open In App

C# Program to Count the Files Based on Extension using LINQ

Last Updated : 09 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Given files, now we count the files based on extension using LINQ. We are considering all types of file formats like pdf, txt, xml and going to count these files based on the extension. For that, we have to know the following methods:

  • Path.GetExtension(): This method is used to get an extension of the given path.
  • TrimStart(): This method is used to cut the path and starts with the given string.
  • ToLower(): This method is used to convert the string to lower.
  • GroupBy(): This function is used to group the data that shares the common attribute from the specified sequence or list.
  • count(): This method is used to get the total number of elements from the given sequence.

Example:

Input: {myfile1.txt, myfile2.txt, myfile3.xml}
Output: 3 file ---> txt
        1 file ---> xml
        
Input: {file12.txt, file23.xml, file45.pdf}
Output: 1 file ---> txt
        1 file ---> xml
        1 file ---> pdf

Approach:

  • Create a list of files with different extensions.
  • Read the files using .GetExtension() method. Then convert the file extensions into lower case to remove case sensitiveness. Then apply trim function before this and use groupby method to get same file extensions in to one group and then use count function to count file extensions which are already in group.
var result = files.Select(f => Path.GetExtension(f)
                             .TrimStart('.').ToLower())
                             .GroupBy(y => y, (ex, excnt) => new
                             {
                                Extension = ex,
                                Count = excnt.Count()
                             });
                             
  • Display the file count and extension using file count
foreach (var i in result)
{
    Console.WriteLine(i.Count + " File --> " + 
                      i.Extension + " format ");
}

Example:

C#




// C# program to count the files according 
// to their extension
using System;
using System.IO;
using System.Linq;
  
class GFG{
  
public static void Main()
{
      
    // Declare files with different extensions
    string[] files = { "171fa07058.txt", "171fa07058.pdf",
                       "171fa07058.xml", "171fa07058.txt"
                       "171fa07058.txt", "171fa07058.xml" };
  
    // Group by files with an extension
    var result = files.Select(f => Path.GetExtension(f)
                             .TrimStart('.').ToLower())
                             .GroupBy(y => y, (ex, excnt) => new
                             {
                                Extension = ex,
                                Count = excnt.Count()
                             });
                               
    // Display the final result
    foreach (var i in result)
    {
        Console.WriteLine(i.Count + " File --> "
                      i.Extension + " format ");
    }
}
}


Output:

3 File --> txt format 
1 File --> pdf format 
2 File --> xml format


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads