How to pull all files within a folder updated in the last set period of time (Day,Week,Month, or Year)

Hello All,

I have been tasked with finding a way to create copies of all new files in specific folders that have had changes occur in the last month. I was able to find some code that allows you to pull the most recent files from a directory, but is there any way to pull all files that have been edited since a month ago for example?

Thank you,
Stefanos

Hello,

I have not used this custom component myself but you might want to look into this one.

Supposedly it pulls all the meta data from files and returns them in a dictionary. I would assume that this would pull the “date modified” meta tag if using it on a file.

Your first problem is determining what the business/requestor means when they say “1 month”. Does that mean 30 days? 28 days? Same day of month in the previous month? If the last option, what if the previous months has less days than the current month (e.g. if it’s the 31st, how do you handle months with only 30 days). For my example below I’ll assume previous 30 days.

You’ll have to do it one folder at a time (unless it’s all subdirectories and you’re ok searching all subdirectories). This can be done by passing it into an array and doing a for each folder in array of folders:

Directory.GetFiles(YourDirectory).AsEnumerable().Where(function (x) File.GetLastWriteTime(x).Date >= today.adddays(-30))

This uses the directory.getfiles() method which will return an ienumerable of strings that are the full file paths of all files in YourDirectory that was modified in the last 30 days. NOTE: I haven’t tested so there might be some syntax error, but you get the idea.

2 Likes

Thank you Dave, this worked perfectly for me. I should be able to handle the backups, the portion that was throwing me the most off was the previous X amount of days. I tested this and it worked exactly as I hoped

1 Like

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