I
I
iXelper2018-08-24 10:16:49
C++ / C#
iXelper, 2018-08-24 10:16:49

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"

TASK: Match the dates with the file name (the file name contains the date and time "...2018_08_09__13_04...").
Dates and file name in variables CHANGE! they are not permanent.
QUESTION: how and in what way it can be implemented?
in the course of checking the conditions, it SHOULD BE THIS:
string otvet = "Дата: " + dateOne + "Файл с этой датой: " + nameOne;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
lvv85, 2018-08-24
@iXelper

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_");
        }

D
Dmitry Kovalsky, 2018-08-24
@dmitryKovalskiy

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 question

Ask a Question

731 491 924 answers to any question