VBA 定时删除半小时前的 TXT 文件
以下是一个 VBA 的示例代码,用于定时删除当前时间的前半个小时的 txt 文件:
Sub DeleteFiles()
Dim filePath As String
Dim fileDate As Date
Dim halfHourAgo As Date
' 设置文件路径
filePath = 'C:\Path\To\Files\*.txt'
' 获取当前时间和前半个小时的时间
fileDate = Now()
halfHourAgo = DateAdd('n', -30, fileDate)
' 查找并删除满足条件的文件
Dim file As Variant
file = Dir(filePath)
Do While file <> ''
If Right(file, 4) = '.txt' Then
If FileDateTime(filePath & '\' & file) < halfHourAgo Then
Kill filePath & '\' & file
End If
End If
file = Dir
Loop
End Sub
使用此代码,你需要根据实际情况更改 filePath 变量的值,将其设置为你要删除的 txt 文件所在的文件夹路径。代码将遍历该文件夹下的所有 txt 文件,如果文件的创建时间早于当前时间的前半个小时,则将其删除。
原文地址: https://www.cveoy.top/t/topic/bG6q 著作权归作者所有。请勿转载和采集!