Ad Code

Responsive Advertisement

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

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

To get files from the previous day using the `.NET` framework in `C#`, you can use the following approach:

1. Calculate the date for the previous day.
2. Format the date to match the file naming convention.
3. Filter files based on the formatted date.


Here’s how you can implement this:

Example Code

using System;
using System.IO;

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

        // Get the date for the previous day
        DateTime previousDay = DateTime.Now.AddDays(-1);

        // Format the date to match the file naming convention (e.g., "Logs_13_8_2024")
        string formattedDate = previousDay.ToString("dd_M_yyyy");

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

        // Filter files that contain the formatted date
        string[] previousDayFiles = Array.FindAll(files, fileName => fileName.Contains(formattedDate));

        // Output the list of files found
        foreach (string file in previousDayFiles)
        {
            Console.WriteLine(file);
        }
    }
}

Explanation

1. `DateTime previousDay = DateTime.Now.AddDays(-1);`: This line calculates the date for the previous day.

2. `string formattedDate = previousDay.ToString("dd_M_yyyy");`: Formats the date to match the file naming convention. Adjust the format string (`"dd_M_yyyy"`) according to your file naming format.

3. `Directory.GetFiles(logFile_SchedulerUtilityPath, "*.log");`: Retrieves all `.log` files in the specified directory.

4. `Array.FindAll(...)`: Filters the files array to include only those files that contain the formatted date.

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

Customization

If your file naming convention differs, you may need to adjust the `formattedDate` to match your specific needs. For instance, if your files are named with a different pattern, ensure the `formattedDate` aligns with that pattern.

Complete Code with Logging

Here’s a more complete version of the code with added comments and logging:

using System;
using System.IO;

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

        // Get the date for the previous day
        DateTime previousDay = DateTime.Now.AddDays(-1);

        // Format the date to match the file naming convention (e.g., "Logs_13_8_2024")
        string formattedDate = previousDay.ToString("dd_M_yyyy");

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

        Console.WriteLine($"Searching for files with date: {formattedDate}");

        // Filter files that contain the formatted date
        string[] previousDayFiles = Array.FindAll(files, fileName => fileName.Contains(formattedDate));

        // Check if any files were found
        if (previousDayFiles.Length > 0)
        {
            Console.WriteLine("Files from the previous day:");
            foreach (string file in previousDayFiles)
            {
                Console.WriteLine(file);
            }
        }
        else
        {
            Console.WriteLine("No files found for the previous day.");
        }
    }
}

This script will help you find and display all log files from the previous day, based on the naming pattern of your log files.

If you like comment and share. 🚀

Post a Comment

0 Comments

Ad Code

Responsive Advertisement