Ad Code

Responsive Advertisement

Console App Task 16: How to get up to day -1 files using Directory.GetFiles() in C#

Console App Task 16: How to get upto day -1 files,  string[] files = Directory.GetFiles(logFile_SchedulerUtilityPath, "*.log"); ?  

To get all files from a directory up to the day before the current day, you can filter files based on their creation date or last modified date, depending on your needs. Here's how you can do it:


Example Code

using System;
using System.IO;
using System.Linq;

class Program
{
    static void Main()
    {
        // Define the directory path where log files are stored
        string logFile_SchedulerUtilityPath = @"c:\abc\";

        // Get the current date and subtract one day to get the date for day -1
        DateTime yesterday = DateTime.Now.Date.AddDays(-1);

        // Get all .log files in the specified directory
        string[] files = Directory.GetFiles(logFile_SchedulerUtilityPath, "*.log");

        // Filter files created or modified before the start of the current day
        var previousFiles = files.Where(file => File.GetLastWriteTime(file) < DateTime.Today).ToList();

        // Output the list of files found
        if (previousFiles.Any())
        {
            Console.WriteLine("Files created or modified up to day -1:");
            foreach (string file in previousFiles)
            {
                Console.WriteLine(file);
            }
        }
        else
        {
            Console.WriteLine("No files found up to day -1.");
        }
    }
}

Explanation

1. `DateTime yesterday = DateTime.Now.Date.AddDays(-1);`: This line calculates the date for yesterday. `.Date` removes the time component, so you're comparing only the date.

2. `Directory.GetFiles(logFile_SchedulerUtilityPath, "*.log");`: This retrieves all `.log` files in the specified directory.

3. `File.GetLastWriteTime(file) < DateTime.Today`: This checks if the file was last modified before the start of today. If you want to filter based on creation date instead, use `File.GetCreationTime(file)`.

4. `.Where(file => ... ).ToList();`: Filters the list of files based on the condition that their last write time is before today.

5. Output: The filtered list of files is then printed to the console.

Customization

- If you want to filter based on the file name rather than the last modified date, you could adapt the logic to parse the date from the file name.
- You can adjust the logic to look at the file creation time (`File.GetCreationTime(file)`) instead of the last modified time.

Full Example with Logging

Here’s a more detailed example that includes both logging and filtering based on the last modified date:

using System;
using System.IO;
using System.Linq;

class Program
{
    static void Main()
    {
        // Define the directory path where log files are stored
        string logFile_SchedulerUtilityPath = @"c:\abc\";

        // Get the current date and subtract one day to get the date for day -1
        DateTime yesterday = DateTime.Now.Date.AddDays(-1);

        // Get all .log files in the specified directory
        string[] files = Directory.GetFiles(logFile_SchedulerUtilityPath, "*.log");

        Console.WriteLine($"Looking for files modified before: {DateTime.Today}");

        // Filter files created or modified before the start of the current day
        var previousFiles = files.Where(file => File.GetLastWriteTime(file) < DateTime.Today).ToList();

        // Output the list of files found
        if (previousFiles.Any())
        {
            Console.WriteLine("Files modified up to day -1:");
            foreach (string file in previousFiles)
            {
                Console.WriteLine(file);
            }
        }
        else
        {
            Console.WriteLine("No files found up to day -1.");
        }
    }
}

This code will help you find and display all log files in the specified directory that were created or modified before today.

If you like comment and share. 🚀

Post a Comment

0 Comments

Ad Code

Responsive Advertisement