VB 代码:删除前一小时至两小时内修改的 TXT 文件
以下是使用 VB 代码删除前一小时至两小时内修改的 TXT 文件的示例:
Imports System.IO
Module Module1
Sub Main()
Dim currentDateTime As DateTime = DateTime.Now
Dim folderPath As String = 'C:\path\to\folder' ' 替换为你的文件夹路径
' 计算前两个小时和前一个小时的时间
Dim twoHoursAgo As DateTime = currentDateTime.AddHours(-2)
Dim oneHourAgo As DateTime = currentDateTime.AddHours(-1)
' 获取文件夹中的所有txt文件
Dim files As String() = Directory.GetFiles(folderPath, "*.txt")
For Each file As String In files
Dim fileInfo As New FileInfo(file)
' 检查文件的最后修改时间
If fileInfo.LastWriteTime >= twoHoursAgo AndAlso fileInfo.LastWriteTime < oneHourAgo Then
' 删除文件
File.Delete(file)
Console.WriteLine('Deleted file: ' & file)
End If
Next
Console.WriteLine('Deletion completed.')
Console.ReadLine()
End Sub
End Module
请注意,您需要将 folderPath 变量替换为您要删除文件的文件夹路径。这段代码会遍历该文件夹中的所有 TXT 文件,并删除最后修改时间在前两个小时到前一个小时之间的文件。
原文地址: https://www.cveoy.top/t/topic/bEDR 著作权归作者所有。请勿转载和采集!