删除所有以日期 (yyyy-MM-dd) 命名且早于 x 天的文件
Delete all files, that are named by date (yyyy-MM-dd), older than x days
我是初学者,正在尝试学习 Visual Basic .NET
我有一个包含日志文件的顶级目录。这里以其中一个文件为例,文件名生成方法:
Private Property fileDate As String = (Date.Today.ToString("yyyy-MM-dd") & "-" & TimeOfDay.ToString("HH-mm-ss"))
文件实际生成后,最终的文件名是这样的:
2015-09-22-17-37-16-MyAppName.log
我希望能够获取日志目录中的所有文件,并删除所有超过 x 天的文件。我想保留从程序运行当天算起的 7 天以上的日志。如果没有大量低效代码,我想不出任何方法来做到这一点..
我尝试尝试了解更多关于 FileIO.FileSystem.GetFiles.. 但到目前为止只想到了这个:
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
当然,这只会找到从当前日期起 7 天前的日志文件..
似乎我还需要将日志目录中的所有文件放入一个数组,然后从该数组中删除所有超过 7 天的文件。然后最后删除数组中保留的所有文件?但是怎么办?
有人可以帮忙吗?非常感谢..
这是我的尝试。我希望它对您的目标有用(对我有用)。
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
只有当您在文件名中始终使用这种日期格式 yyyy-mm-dd
时,这才会起作用。这是因为正则表达式 "\d{4}-\d{2}-\d{2}"
会调节它。但是,您可以为此找到更好的正则表达式。
多亏了下面 Daro 的 post,我才能够得出答案!
这其实很简单。我让自己变得比实际必须的更难。
这是我的解决方案(基于 Daro 的回答——我无法开始工作!!-对不起 Daro)
请记住,这很可能是非常低效的代码、编码实践等,我将在今晚晚些时候清理和整理,但这里是:
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
正则表达式代码以及其他内容可能会被清理干净。但多亏了 Daro,我才刚刚开始探索字符串和正则表达式内容,并开始探索如何使用日期。
谢谢大家!
山姆
我是初学者,正在尝试学习 Visual Basic .NET
我有一个包含日志文件的顶级目录。这里以其中一个文件为例,文件名生成方法:
Private Property fileDate As String = (Date.Today.ToString("yyyy-MM-dd") & "-" & TimeOfDay.ToString("HH-mm-ss"))
文件实际生成后,最终的文件名是这样的: 2015-09-22-17-37-16-MyAppName.log
我希望能够获取日志目录中的所有文件,并删除所有超过 x 天的文件。我想保留从程序运行当天算起的 7 天以上的日志。如果没有大量低效代码,我想不出任何方法来做到这一点..
我尝试尝试了解更多关于 FileIO.FileSystem.GetFiles.. 但到目前为止只想到了这个:
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
当然,这只会找到从当前日期起 7 天前的日志文件..
似乎我还需要将日志目录中的所有文件放入一个数组,然后从该数组中删除所有超过 7 天的文件。然后最后删除数组中保留的所有文件?但是怎么办?
有人可以帮忙吗?非常感谢..
这是我的尝试。我希望它对您的目标有用(对我有用)。
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
只有当您在文件名中始终使用这种日期格式 yyyy-mm-dd
时,这才会起作用。这是因为正则表达式 "\d{4}-\d{2}-\d{2}"
会调节它。但是,您可以为此找到更好的正则表达式。
多亏了下面 Daro 的 post,我才能够得出答案!
这其实很简单。我让自己变得比实际必须的更难。
这是我的解决方案(基于 Daro 的回答——我无法开始工作!!-对不起 Daro)
请记住,这很可能是非常低效的代码、编码实践等,我将在今晚晚些时候清理和整理,但这里是:
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
正则表达式代码以及其他内容可能会被清理干净。但多亏了 Daro,我才刚刚开始探索字符串和正则表达式内容,并开始探索如何使用日期。
谢谢大家!
山姆