How to delete files older than one day from multiple locations

Hello Everyone,

I am looking for a way to delete files that are older than one day from multiple locations (folder paths). I have tried several solutions from this forum, but none have worked effectively. Some methods either delete all files from the folder or don’t delete any at all.

I would greatly appreciate your assistance with this issue.

Thank you!

Hi,

The following sample may help you.

System.IO.Directory.GetFiles(currentText,"*.*",searchOption.AllDirectories).Where(Function(f) File.GetCreationTime(f)<targetDate).ToArray()

Regards,

2 Likes

Try using a combination of the “For Each” activity and a date filter to check the file creation dates. Make sure to set the correct file path for each location.

@Yoichi Thank you very much! I will give this a try and let you know the results.

@Yoichi I tried this and unfortunately it’s not working for me.

Hi @Robot2_Automation ,

Could you maybe let us know more on what was not working ? What were the files that you had tested and what were the creation dates of those files ?

@supermanPunch None of the files older than a day were deleted.

I need to delete all the files except the one created yesterday.

Did you tried with below condition. It is working fine for me

Cdate(CurrentFile.CreatedDate) < DateTime.Now.AddDays(-1)

Sharing the code snippet as well

I changed the targetDate to Now.AddDays(-2) and it working.

@sharazkm32 Appreciate your assistance. Thank you.

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