How to Delete the file older than two days (in name)

Hi to all,
I have a trivial problem, but I need help to solve it.

At the end of my flow, my bot creates an Output file with this name:
“output_ + Now.tostring (” dd.MM.yyyy “). xlsx.”

Every day the bot creates a file of this type, increasing the number of files in the folder.
I need to create a rule that deletes files older than 2 days.

I cannot rely on the creation date, because the folder contains other files that must not be deleted, so it must take the date from the name.

Can anyone help me with this?
Thanks a lot… :expressionless:


Can you try the following?

arrFile=System.IO.Directory.GetFiles(yourPath,"*.xlsx").Where(Function(f) System.Text.RegularExpressions.Regex.IsMatch(System.IO.Path.GetFileNameWithoutExtension(f),"^output_\d{2}\.\d{2}\.\d{4}$") AndAlso DateTime.ParseExact(System.IO.Path.GetFileNameWithoutExtension(f).Substring(7),"dd.MM.yyyy",System.Globalization.CultureInfo.InvariantCulture)<DateTime.Today.AddDays(-2)).ToArray

Note: For test, use MessageBox or WriteLine activity instead of DeleteFile, first.


Hi Yoichi,
I created some .xlsx files for testing, and tried to replicate your flow.

I get this error:
“message”: “Assign: String was not recognized as a valid DateTime.”,

System.IO.Directory.GetFiles(Path,"*.xlsx").Where(Function(f) System.Text.RegularExpressions.Regex.IsMatch(System.IO.Path.GetFileNameWithoutExtension(f),"^Output_Credit_\d{2}.\d{2}.\d{4}$") AndAlso DateTime.ParseExact(System.IO.Path.GetFileNameWithoutExtension(f).Substring(7),“dd.MM.yyyy”,System.Globalization.CultureInfo.InvariantCulture)<DateTime.Today.AddDays(-2)).ToArray


Can you try to modify Substring(7) to SubString(14) because prefix is changed from “Output_” to “Output_Credit_”?
If prefix will be change in the future, it might be better to use regex instead of substring.


It Works!

Sorry, i’m also weak with Regular expressions… :expressionless:
The solution is perfect.
Delete all files older than XX days. Top!!
Thanks Yoichi!

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