How to find a specific file with specific name?

Good evening friends, a question please

I need to find the file with last date and containing (TR):

JVL Report - Week 10, 04.03.2024 (TR).xlsb
JVL Report - Week 10, 06.03.2024 (TR).xlsb
JVL Report - Week 10, 04.03.2024.xlsb
JVL Report - Week 10, 06.03.2024.xlsb

I have been trying with :

“Directory.GetFiles(path_file, ).Where(Function(w)(Path.GetFileNameWithoutExtension(w).ToLower.Contains(“TR”))).ToArray()”

But something is not working for me

Thank you very much in advance

Hi @Lynx

Try this

latestFile = Directory.GetFiles(path_file).Where(Function(file) Path.GetFileNameWithoutExtension(file).ToLower.Contains("(tr)")) _
               .OrderByDescending(Function(file) DateTime.ParseExact(file.Substring(file.IndexOf(",") + 2, 10), "dd.MM.yyyy", System.Globalization.CultureInfo.InvariantCulture)) _
               .FirstOrDefault()

Regards,

1 Like

Hi @Lynx

Try this:

latestFile = Directory.GetFiles(path_file).Where(Function(w) Path.GetFileNameWithoutExtension(w).ToLower.Contains("(tr)")).OrderByDescending(Function(f) DateTime.ParseExact(System.Text.RegularExpressions.Regex.Match(Path.GetFileNameWithoutExtension(f), "\d{2}\.\d{2}\.\d{4}").Value, "dd.MM.yyyy", System.Globalization.CultureInfo.InvariantCulture)).FirstOrDefault()

Hope it helps!!

Hi @Lynx

Input:

latestFile = Directory.GetFiles(path_file).Where(Function(w) Path.GetFileNameWithoutExtension(w).ToLower.Contains("(tr)")).OrderByDescending(Function(f) DateTime.ParseExact(System.Text.RegularExpressions.Regex.Match(Path.GetFileNameWithoutExtension(f), "\d{2}\.\d{2}\.\d{4}").Value, "dd.MM.yyyy", System.Globalization.CultureInfo.InvariantCulture)).FirstOrDefault()

Output:

Hope it helps!!

1 Like

Thank you so much Parvathy!

1 Like

You’re welcome @Lynx

Happy Automation!!

It works perfectly, thanks a lot!

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.