I am a beginner, trying to learn Visual Basic .NET
I have a top level directory which contains log files. Here is an example of one of the files, file name generation method:
Private Property fileDate As String = (Date.Today.ToString("yyyy-MM-dd") & "-" & TimeOfDay.ToString("HH-mm-ss"))
After the file is actually generated, the final file names look like this: 2015-09-22-17-37-16-MyAppName.log
I want to be able to get all files in the logs directory, and delete any files that are older than x amount of days. I want to keep logs newer than 7 days from the current day the program runs. I can't think of any way to do this without a ton of inefficient code..
I've tried experimenting to learn more about FileIO.FileSystem.GetFiles.. but only came up with this so far:
Dim curDate As Date = Date.Today
Dim subDate As Date = curDate.AddDays(-7)
Dim newDate As String = subDate.ToString("yyyy-MM-dd")
For Each fileFound As String In FileIO.FileSystem.GetFiles("logs",
FileIO.SearchOption.SearchTopLevelOnly,
{newDate & "*"})
Console.WriteLine("FOUND FILE")
Console.WriteLine(fileFound)
Next
But of course, that only finds log files that are named the date of 7 days ago from the current date..
It also seems as if I need to get all files from the logs directory into an array and then remove any files that are newer than 7 days from the array. Then finally delete all files that remain in the array? But how?
Can anyone help? Thanks much..
Date
and then compare that to your threshold date. – jmcilhinneyDateTime.AddDays
method? – jmcilhinney