---
title: "Shows file count per extension or a progress bar for large folders?"  
description: "Shows file count per extension or a progress bar for large folders?"  
author: "ICSM Computer"  
published: 2025-05-13  
updated: 2025-05-13  
canonical: https://www.mindstick.com/interview/34112/shows-file-count-per-extension-or-a-progress-bar-for-large-folders  
category: "c#"  
tags: ["c#", "file handling"]  
reading_time: 2 minutes  

---

# Shows file count per extension or a progress bar for large folders?

Here’s a version of the **directory size calculator** that also:

1. Shows **file count per extension**
2. Tracks total size
3. Optionally displays **progress** (simple output)

### Full Example

```cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

class Program
{
    static void Main()
    {
        string folderPath = @"C:\MyFolder";

        if (!Directory.Exists(folderPath))
        {
            Console.WriteLine("Folder not found.");
            return;
        }

        long totalSize = 0;
        var extensionCounts = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);

        Console.WriteLine("Processing files...");

        var files = Directory.EnumerateFiles(folderPath, "*", SearchOption.AllDirectories);
        int fileIndex = 0;

        foreach (var file in files)
        {
            try
            {
                var fi = new FileInfo(file);
                totalSize += fi.Length;

                string ext = fi.Extension;
                if (string.IsNullOrEmpty(ext)) ext = "[no extension]";

                if (!extensionCounts.ContainsKey(ext))
                    extensionCounts[ext] = 0;

                extensionCounts[ext]++;
            }
            catch (Exception ex)
            {
                Console.WriteLine($"[Skipped] {file}: {ex.Message}");
            }

            if (++fileIndex % 100 == 0)
            {
                Console.WriteLine($"Processed {fileIndex} files...");
            }
        }

        Console.WriteLine($"\nTotal size: {totalSize / 1024.0 / 1024.0:F2} MB");
        Console.WriteLine("File counts by extension:");
        foreach (var kvp in extensionCounts.OrderByDescending(k => k.Value))
        {
            Console.WriteLine($"  {kvp.Key}: {kvp.Value}");
        }
    }
}
```

### Sample Output:

```plaintext
Processing files...
Processed 100 files...
Processed 200 files...

Total size: 153.42 MB
File counts by extension:
  .txt: 92
  .log: 65
  .json: 31
  [no extension]: 5
```

## Answers

### Answer by ICSM Computer

Here’s a version of the **directory size calculator** that also:

1. Shows **file count per extension**
2. Tracks total size
3. Optionally displays **progress** (simple output)

### Full Example

```cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

class Program
{
    static void Main()
    {
        string folderPath = @"C:\MyFolder";

        if (!Directory.Exists(folderPath))
        {
            Console.WriteLine("Folder not found.");
            return;
        }

        long totalSize = 0;
        var extensionCounts = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);

        Console.WriteLine("Processing files...");

        var files = Directory.EnumerateFiles(folderPath, "*", SearchOption.AllDirectories);
        int fileIndex = 0;

        foreach (var file in files)
        {
            try
            {
                var fi = new FileInfo(file);
                totalSize += fi.Length;

                string ext = fi.Extension;
                if (string.IsNullOrEmpty(ext)) ext = "[no extension]";

                if (!extensionCounts.ContainsKey(ext))
                    extensionCounts[ext] = 0;

                extensionCounts[ext]++;
            }
            catch (Exception ex)
            {
                Console.WriteLine($"[Skipped] {file}: {ex.Message}");
            }

            if (++fileIndex % 100 == 0)
            {
                Console.WriteLine($"Processed {fileIndex} files...");
            }
        }

        Console.WriteLine($"\nTotal size: {totalSize / 1024.0 / 1024.0:F2} MB");
        Console.WriteLine("File counts by extension:");
        foreach (var kvp in extensionCounts.OrderByDescending(k => k.Value))
        {
            Console.WriteLine($"  {kvp.Key}: {kvp.Value}");
        }
    }
}
```

### Sample Output:

```plaintext
Processing files...
Processed 100 files...
Processed 200 files...

Total size: 153.42 MB
File counts by extension:
  .txt: 92
  .log: 65
  .json: 31
  [no extension]: 5
```


---

Original Source: https://www.mindstick.com/interview/34112/shows-file-count-per-extension-or-a-progress-bar-for-large-folders

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
