Add "date condition" to search files

Ant6729

Well-known member
Joined
Jan 22, 2019
Messages
56
Programming Experience
Beginner
Hello!
My code looks for txt files and prints lines containing the values of regular expressions specified in this code to the console.


I would like to add a condition that allows me to search files for a specific date.


I commented out my attempt..

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Text.RegularExpressions;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string rootFolder = @"C:\Users\Anton\Desktop\?#folder";
            string pattern = @"\b(Nina|Ira|Sveta)\b";

            foreach (var file in Directory.EnumerateFiles(rootFolder, "*.txt", SearchOption.AllDirectories))
            {
                using (StreamReader sr = new StreamReader(file, System.Text.Encoding.Default))
                {
                    string line;

                    while ((line = sr.ReadLine()) != null)
                    {
                        if (Regex.IsMatch(line, pattern, RegexOptions.IgnoreCase))
                            Console.WriteLine(file + " " + line);
                    }
                }
            }

            Console.ReadKey();
        }
    }
}

Could you help me to add a search "date condition" in this code?
 
Last edited by a moderator:
I have edited your code to remove huge wads of whitespace and some lines that were commented out. Please don't include that sort of stuff in a post that just makes the whole thing harder to read. I have also aligned the braces so that the code makes sense.
 
Back
Top Bottom