Introduction

When you work with logs, documents, or any fast-changing directory, it’s often useful to quickly identify which files were updated in the last hour.
Instead of manually sorting in Explorer, we can build a lightweight command-line app in C# that recursively scans a folder (including subfolders) and lists all files changed within a configurable time window.

This article shows the step-by-step process and gives you ready-to-use code.


Step 1 — Create the project

Open your terminal and create a new console app:

dotnet new console -n LastHourLister
cd LastHourLister


Step 2 — Replace the code

Replace the content of Program.cs with the following minimal but robust implementation:

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

class Program
{
    static int Main(string[] args)
    {
        // Usage:
        // dotnet run -- [rootPath] [minutes]
        // defaults: rootPath = current directory, minutes = 60

        string root = args.Length > 0 && !string.IsNullOrWhiteSpace(args[0])
            ? args[0]
            : Directory.GetCurrentDirectory();

        int minutes = 60;
        if (args.Length > 1 && int.TryParse(args[1], out var m) && m > 0)
        {
            minutes = m;
        }

        if (!Directory.Exists(root))
        {
            Console.Error.WriteLine($"Directory not found: {root}");
            return 2;
        }

        DateTime utcCutoff = DateTime.UtcNow.AddMinutes(-minutes);

        var options = new EnumerationOptions
        {
            RecurseSubdirectories = true,
            IgnoreInaccessible = true,
            ReturnSpecialDirectories = false
        };

        var hits = new List<(string Path, DateTime LastWriteUtc)>();

        foreach (var file in Directory.EnumerateFiles(root, "*", options))
        {
            DateTime lastWriteUtc;
            try
            {
                lastWriteUtc = File.GetLastWriteTimeUtc(file);
            }
            catch
            {
                continue; // skip unreadable files
            }

            if (lastWriteUtc >= utcCutoff)
            {
                hits.Add((file, lastWriteUtc));
            }
        }

        foreach (var h in hits.OrderByDescending(h => h.LastWriteUtc))
        {
            var local = h.LastWriteUtc.ToLocalTime();
            Console.WriteLine($"{local:yyyy-MM-dd HH:mm:ss}  {h.Path}");
        }

        Console.Error.WriteLine($"Found {hits.Count} file(s) updated in the last {minutes} minute(s).");
        return 0;
    }
}


Step 3 — Run the app

Run it with default values (last 60 minutes, current directory):

dotnet run

Run it on a specific directory:

dotnet run -- "C:\Data"

Run it for a custom time window (e.g., last 15 minutes):

dotnet run -- "C:\Data" 15


Example output

2025-09-03 13:05:41  C:\Data\Logs\server.log
2025-09-03 12:58:22  C:\Data\Reports\summary.xlsx
2025-09-03 12:47:09  C:\Data\Notes\todo.txt
Found 3 file(s) updated in the last 60 minute(s).


Why this approach?

  • Recursive by default → covers all subfolders.
  • Safe → ignores access-denied errors instead of crashing.
  • Flexible → you can change the time window in minutes.
  • Portable → single file, no external libraries.

Next steps

  • Add filters (e.g., only .log files).
  • Export results to CSV or JSON.
  • Schedule it as a task to monitor changes automatically.

Conclusion

In a few lines of C#, you can build a practical CLI tool that saves you time when monitoring directories.
This app is a great starting point, and you can extend it with reporting or integration into larger automation workflows.

Edvaldo Guimrães Filho Avatar

Published by