【问题标题】:Delete all files, that are named by date (yyyy-MM-dd), older than x days删除所有按日期命名的文件 (yyyy-MM-dd),早于 x 天
【发布时间】:2015-09-25 04:02:19
【问题描述】:

我是初学者,正在努力学习 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 天的文件。然后最后删除所有保留在数组中的文件?但是怎么做呢?

有人可以帮忙吗?非常感谢..

【问题讨论】:

  • 逻辑将获取所有文件名,将每个文件中的日期解析为Date,然后将其与您的阈值日期进行比较。
  • 你能提供一个样品吗.. 我在使用下面的样品时遇到了问题.....
  • 对于找到的每个文件,我似乎无法弄清楚如何提出:如果 fileFound 日期早于阈值日期(- x 天数),则删除文件...
  • 您是否有理由要在文件名中使用日期而不是文件创建日期?例如:msdn.microsoft.com/en-us/library/…
  • 你用过DateTime.AddDays方法吗?

标签: vb.net date logging delete-file


【解决方案1】:

这是我的尝试。我希望它适用于你的目标(它适用于我)。

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}" 会调节它。但是,您可以为此找到更好的正则表达式。

【讨论】:

  • 非常感谢,达罗!!我将在今晚晚些时候尝试实现这一点。我希望它有效!我以前从未见过正则表达式。
  • 您最好多看看它,因为当您管理一些字符串时,它们是编程的基本工具。查看this 了解基本信息(并自行调查更多)并使用this tool 有很多有用的正则表达式。如果此答案对您有用,请标记为“已接受”。
  • 添加为答案!谢谢你的支持。我会更多地研究正则表达式
【解决方案2】:

感谢达罗在下面的帖子,我能够想出一个答案!

这其实很简单。我让自己变得比实际情况更难。

这是我的解决方案(基于达罗的回答——我无法开始工作!!——对不起达罗)

请记住,这很可能是非常低效的代码、编码实践等,我将在今晚晚些时候清理和整理,但这里是:

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,我才刚刚开始探索字符串和正则表达式的东西,以及开始探索如何处理日期。

谢谢大家!

山姆

【讨论】:

    猜你喜欢
    • 2020-03-11
    • 2016-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-29
    • 1970-01-01
    相关资源
    最近更新 更多