Answer the question
In order to leave comments, you need to log in
How to match filenames with the same dates?
GIVEN: 3 string variables with dates, 3 string variables with file name
-Example:
// переменные с датой
string dateOne = "09.08.2018 13:04:00"
string dateTwo = "11.05.2018 11:03:00"
string dateThree = "16.07.2018 13:28:00"
// переменные с названием файла
string nameOne = "nameFile_1301_1602__2018_08_09__13_04_07_990"
string nameTwo = "nameFile_1302_1602__2018_05_11__11_03_40_910"
string nameThree = "nameFile_1302_1602__2018_07_16__13_28_12_950"
string otvet = "Дата: " + dateOne + "Файл с этой датой: " + nameOne;
Answer the question
In order to leave comments, you need to log in
static void Main(string[] args)
{
string[] arrDate = { "09.08.2018 13:04:07"
, "11.05.2018 11:03:00"
, "16.07.2018 13:28:12" };
string[] arrFileName = { "nameFile_1301_1602__2018_08_09__13_04_07_990"
, "nameFile_1302_1602__2018_05_11__11_03_40_910"
, "nameFile_1302_1602__2018_07_16__13_28_12_950" };
foreach (var strDate in arrDate)
{
var pattern = GetSearchPattern(strDate);
if (pattern!=null)
{
var fileName = arrFileName.FirstOrDefault(f=>f.Contains(pattern));
if (fileName == null) continue;
string otvet = "Дата: " + strDate + "Файл с этой датой: " + fileName;
Console.WriteLine(otvet);
}
}
}
static string GetSearchPattern(string strDate)
{
DateTime date = new DateTime();
if (!DateTime.TryParse(strDate, out date)) return null;
return date.ToString("__yyyy_MM_dd__HH_mm_ss_");
}
I would decide rudely and in the forehead.
Convert dates to date type and format the date to the form "yyyy_MM_dd__HH_mm".
And then look for a substring in the file name
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question