0
votes

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..

2
The logic would to get all the file names, parse out the date in each one to a Date and then compare that to your threshold date.jmcilhinney
Can you please provide a sample.. I'm having trouble with the sample below.....swam
For each file found, I can't seem to figure out how to come up with: If fileFound date is older than theThreshold date (- x amount of days) than delete the file...swam
Is there a reason you want to use the date in the filename instead of the file creation date? For example: msdn.microsoft.com/en-us/library/…Derek
Have you used the DateTime.AddDays method?jmcilhinney

2 Answers

0
votes

This is my attempt. I hope it works for your goal (it works for me).

Dim curDate As Date = Date.Today.ToString("yyyy-MM-dd")
Dim folderPath As String = "Put your path here!"

For Each fileFound As String In Directory.GetFiles(folderPath)
    If Regex.IsMatch(fileFound, "\d{4}-\d{2}-\d{2}") Then
        Dim regex As Regex = New Regex("\d{4}-\d{2}-\d{2}")
        Dim matchFileDate As Match = regex.Match(fileFound)
        Dim fileDate As DateTime = DateTime.ParseExact(matchFileDate.Value, "yyyy-MM-dd", CultureInfo.InvariantCulture)
        Dim days As Integer = fileDate.Subtract(curDate).Days

        If days < -7 Then
            My.Computer.FileSystem.DeleteFile(fileFound)
        End If
    End If
Next

This will only work if you are working with this date format yyyy-mm-dd all the time in your file names. This is because the regex "\d{4}-\d{2}-\d{2}" will condition it. However, you could find better regex for this.

0
votes

Thanks to Daro's post below, I was able to come up with an answer!

This is actually pretty simple. I was making it harder on myself than what it actually had to be.

Here is my solution (based on Daro's answer -- which I couldn't get to work!! - Sorry Daro)

Please keep in mind that this is more than likely very inefficient code, coding practice, etc, which I will clean up and sort out later on this evening, but here it is:

Dim curDate As String = Date.Today.ToString("yyyy-MM-dd")
Dim folderPath As String = "logs"

For Each fileFound As String In FileIO.FileSystem.GetFiles(folderPath)

    If Regex.IsMatch(fileFound, "\d{4}-\d{2}-\d{2}") Then

        Dim regex As Regex = New Regex("\d{4}-\d{2}-\d{2}")
        Dim matchFileDate As Match = regex.Match(fileFound)
        Dim fileDate As DateTime = DateTime.ParseExact(matchFileDate.Value, "yyyy-MM-dd", CultureInfo.InvariantCulture)
        Dim expDate As Date = CDate(curDate)
        expDate = expDate.AddDays(-Me.dayCount)

        Console.WriteLine("File found, date: " & CDate(fileDate))
        Console.WriteLine("Current file expiration date: " & CDate(expDate))

        If CDate(fileDate) < CDate(expDate) Then
            Console.WriteLine("This file is older than the expiration date, and will be deleted!")
        End If

    End If

Next

The regular expression code could probably cleaned up, as well as other stuff.. but I'm just beginning to explore strings and regex stuff thanks to Daro, as well as beginning to explore how to work with dates.

Thanks guys!

Sam