Get Current Day Latest File from Folder

Hi Everyone ,

My Output file name looks like Output_yyyy_MM_dd_hh_mm_ss. And suppose if i have below files in the folder and my current day is October 28th 2021. I want the Current Days file which is latest. I have tried the below expression but it only looks latest file considering the creation time. I also want to consider the Current Day in below expression. Can you anyone help me with this? I am considering this case because there might be a day where an output file for that particular day might not be there/generated in the folder and it will take latest file considering the creation date from the below expression which is wrong.

str_filepath = Directory.GetFiles(yourfolder_path,"*.xlsx").OrderByDescending(Function(d) New FileInfo(d).CreationTime).Tolist(0)

Output_2021_10_28_07_50_10.xlsx
Output_2021_10_28_06_45_08.xlsx
Output_2021_10_27_03_23_02.xlsx
Output_2021_10_27_08_54_35.xlsx
Output_2021_10_27_10_45_50.xlsx

Thanks

split this data by _ and create a date string, ddMMyyyy

then convert that into date datatype
using

DateTime.ParseExact(row(“strDateString”).Tostring.Substring(0,10),”ddMMyyyy”,System.Globalization.CultureInfo.InvariantCulture)

make sure you have System.Globalization namespace imported

once done you’ll have all the file’s date and you can get the latest file using comparison

for comparison, there’s a solution in the below thread already

Hope this helps!

Hi @rahulsharma

Thanks for the reply.

I don’t want to compare all the files in the folder. I just want to using something like DateTime.Now and get latest file for the Current Day considering with creation time.Can you help me with expression/syntax for this

in that case, you can find all possible solutions in the below thread

You can use the below to first filter out only files created today, then sort by creation time, and then take the first in the list:

directory.getFiles(“yourfilepath”).where(Function(f) file.GetCreationTime(f).ToShortDateString = today.ToShortDateString).OrderByDescending(function(f) file.GetCreationTime(f)).First

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