Delete Files Based on No of days

I would like to delete files from the archive based on a retention period that is specified .
for example if retention period is 5 delete all files from the archive from passed five days.

deletefiles

@FMK So you want to Delete all files present in a Folder from the last five days?

yes based on the value that i specify so if i specify 5 it deletes last five days files.
I have tried but no solution .

@FMK Can you Check this Code if this gets all the files from the Last 5 Days, we can then manipulate it to make it as a user choice.

which code do you mean

@FMK My Apologies , I thought i had pasted the code :sweat_smile:
Directory.GetFiles(“yourFolderPath”).Where(Function(x)CDate(new System.IO.FileInfo(x).CreatedDate)>Now.AddDays(-5)).ToArray

1 Like

I tried this but didnt work , it moved all files even created today

@FMK Can you show me the List of Files with Created time, that you want to move and the ones you don’t want to move ?

@FMK When you say last 5 Days, you don’t want the files Created for the Current Day to be Deleted ?

yes thats right

@FMK Then I guess this Should work :

Directory.GetFiles(“yourFolderPath”).Where(Function(x)CDate(new System.IO.FileInfo(x).CreatedDate)>Now.AddDays(-5) and CDate(new System.IO.FileInfo(x).CreatedDate)<Now).ToArray

Try this and let me know if it meets your requirement.

3 Likes

this worked , thank you very much

1 Like

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