Cannon
Cannon

Reputation: 2783

Regex to identify file pattern

I have a folder with files coming in as

fileName(prefix) + " " + todayDate.ToString("yyyyMMdd") + ".csv";

For simple file my regex was FilePrefix + @"(.*)\.csv"

How to have a search pattern to get file for only today's date ?

Upvotes: 0

Views: 118

Answers (2)

dav_i
dav_i

Reputation: 28097

Just do something like this

List<string> fileNames = // wherever you're getting these

var ending = DateTime.Now.ToString("yyyyMMdd") + ".csv";

var filteredFileNames = fileNames.Where(x => x.EndsWith(ending));

Upvotes: 0

Sean U
Sean U

Reputation: 6850

It sounds like if you're just searching for today's date, then you should already know exactly what file name you're looking for. So there's no need for a regex - just look for files where

var expectedName = FilePrefix + " " + DateTime.Now.ToString("yyyyMMdd") + ".csv"; 

String.Compare(fileName, expectedName, StringComparison.OrdinalIgnoreCase) == 0

Upvotes: 3

Related Questions