How to delete old File

Older than two days:

oldFilesToDelete = (From f In Directory.GetFiles("C:\Temp\old")
Let strDate = System.Text.RegularExpressions.Regex.Match(Path.GetFileName(f), "\d{2}\.\d{2}\.\d{4}").ToString
Where Not String.IsNullOrWhiteSpace(strDate) AndAlso DateTime.ParseExact(strDate, "dd.MM.yyyy",system.Globalization.CultureInfo.InvariantCulture) < Today.AddDays(-2)
Select f
).ToArray

oldFilesToDelete is an array of string. Change -2 to -1 if you want to delete files older than one day.

The result is a list of files (with full path) that you can loop through and delete. Files without a date in their name will be ignored.

image

image

image

1 Like