Need to copy oldest file of particular month to a destination

Hi Everyone,

My requirement is to copy oldest file from particular folder to a destination
for eg : folder has these file where files are in dd-MMM-yyyy format

1.19-Jan-2023
2.19-Mar-2022
3.19-May-2022
4.12-May-2022
5.03-Jun-2022

I am using the below expression to get

Datetime.TryParseExact(path.GetFileNameWithoutExtension(file), “dd-”+date.now.AddMonths(in_Month).ToString(“MMM”)+“-”+now.AddMonths(in_Month).year.ToString,nothing, nothing, nothing)).ToArray.AsEnumerable().OrderBy(function(x) cdate(Path.GetFileNameWithoutExtension(x))).first

where in_Month is an int32 argument if i pass 0 means its jul and -2 means its May

the expression is working fine for all the months but not working for May (i.,e -2) and Mar (i.,e -4)

attaching link of the previous topic

Any suggestions @jack.chan @ppr @Yoichi

Thanks in advance

Hey!

I’m not aware of LinQ…

But we can try like this…

Assign String Folderpath = "Desktop\files\"
Assign arr(string) ArrFiles = Directory.getfiles(Folderpath,"*.xlsx")

For each

Assign String FileName = path.GetFileNameWithoutExtention(item)

If condition

FileName<Now.ToString("dd-MM-yyyy")

Then block you’ll get the less than system date files

In else block will get the > system date

Regards,
NaNi

@THIRU_NANI thanks , but the need to compare the dates based on the file names in “dd-MMM-yyyy”) and pick up the oldest file of that month and year

for eg : 1. 03-May-2022
2. 02-May-2022
3. 03-May-2023

and if bot is running on may 2022 , bot should pick up 02-May-2022 file which is oldest

Hi,

How about the following?

files =System.IO.Directory.GetFiles(yourPath)

Then

files.OrderBy(Function(f) DateTime.Parse(System.Text.RegularExpressions.Regex.Match(System.IO.Path.GetFileNameWithoutExtension(f),"\d{2}-[A-Za-z]{3}-\d{4}$").Value)).First

Regards,

@yoichi I am getting this error
Get oldest File of the month: String was not recognized as a valid DateTime.

Morever if i am running the bot on may 2022(which i have to pass dynamically)
bot should pick the oldest file of the passed month and year

files.AsEnumerable().Where(function(file) Datetime.TryParseExact(path.GetFileNameWithoutExtension(file), “dd-”+date.now.AddMonths(in_Month).ToString(“MMM”)+“-”+now.AddMonths(in_Month).year.ToString,nothing, nothing, nothing)).ToArray.AsEnumerable().OrderBy(function(x) cdate(Path.GetFileNameWithoutExtension(x))).first

i was using this above expression but its not working for may (-2) for may 2022 is passed for in_Month in that expression

Hi,

How about the following?

files.Where(Function(f) System.Text.RegularExpressions.Regex.IsMatch(System.IO.Path.GetFileNameWithoutExtension(f),"\d{2}-"+now.AddMonths(in_Month).ToString("MMM")+"-"+now.AddMonths(in_Month).ToString("yyyy")+"$")).OrderBy(Function(f) DateTime.Parse(System.Text.RegularExpressions.Regex.Match(System.IO.Path.GetFileNameWithoutExtension(f),"\d{2}-[A-Za-z]{3}-\d{4}$").Value)).First

Regards,

@Yoichi this one is working sir but one restriction is 03-May-2022 if its Capital M it works
but for small ‘m’ 03-may-2022 it doesn’t work

Hi,

but for small ‘m’ 03-may-2022 it doesn’t work

The following expression will work. Can you try this?

files.Where(Function(f) System.Text.RegularExpressions.Regex.IsMatch(System.IO.Path.GetFileNameWithoutExtension(f),"\d{2}-"+now.AddMonths(in_Month).ToString("MMM")+"-"+now.AddMonths(in_Month).ToString("yyyy")+"$",System.Text.RegularExpressions.RegexOptions.IgnoreCase)).OrderBy(Function(f) DateTime.Parse(System.Text.RegularExpressions.Regex.Match(System.IO.Path.GetFileNameWithoutExtension(f),"\d{2}-[A-Za-z]{3}-\d{4}$").Value)).First

Regards,

@Yoichi its working fine , thankyou sir!!

1 Like

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