思路和原理是:
使用方法获取文件的创建时间
然后逐个检查,如果创建时间是指定时间之前,则进行删除
以下是代码,
在窗口添加一个按钮,运行后点击按钮1,可以实现第一个功能,删除30天前的文件
(上一楼好像有Bug,改了一下)
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type
Private Const GENERIC_WRITE = &H40000000
Private Const OPEN_EXISTING = 3
Private Const FILE_SHARE_READ = &H1
Private Const FILE_SHARE_WRITE = &H2
'取得文件句柄需要用到的 API
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
'GainFileTime 需要用到的 API
Private Declare Function GetFileTime Lib "kernel32" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long
Private Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long
Private Declare Function FileTimeToLocalFileTime Lib "kernel32" (lpFileTime As FILETIME, lpLocalFileTime As FILETIME) As Long
Public Function getsec(str As Variant, sec As Variant, secchr As Variant) As String
'获取分段字符串 ,str 为指定字符串 ,sec表示取第几段,secchr表示分隔符
On Error Resume Next
If sec = 1 Then
getsec = Mid(str, 1, getchrid(str, 1, secchr) - 1)
Exit Function
End If
If sec > 1 Then
n = (Len(str) - Len(Replace(str, secchr, ""))) / Len(secchr)
If n - sec = -1 Then
getsec = Mid(str, getchrid(str, sec - 1, secchr) + Len(secchr), Len(str))
Exit Function
End If
If n - sec < -1 Then
getsec = ""
Exit Function
End If
getsec = Mid(str, getchrid(str, sec - 1, secchr) + Len(secchr), getchrid(str, sec, secchr) - (getchrid(str, sec - 1, secchr) + Len(secchr)))
Exit Function
End If
End Function
Public Function getchrid(str As Variant, chrid As Variant, secchr As Variant) As Variant
'获取分隔符位置 ,str 为指定字符串,chrid表示第几次出现的位置,secchr表示分隔符
If chrid = 1 Then
getchrid = InStr(str, secchr)
Exit Function
End If
If chrid = 2 Then
getchrid = InStr(InStr(str, secchr) + Len(secchr), str, secchr) '如果分隔符本身不止1个字符,此处应是 + len(secchr)
Exit Function
End If
If chrid >= 3 Then
getchrid = InStr(str, secchr)
For I = 0 To chrid - 2
getchrid = InStr(getchrid + Len(secchr), str, secchr)
Next
Exit Function
End If
End Function
Public Function GainFileTime(ByVal FileName As String, Optional ByVal TimeItem As Integer = 0) As Date
Dim lngHandle As Long, I As Integer
Dim nFileTime(2) As FILETIME, lFileTime(2) As FILETIME
Dim SysTime As SYSTEMTIME
'验证文件是否存在,如果不存在则退出此函数
If Dir(FileName) = "" Then Exit Function
'打开文件获得文件句柄
lngHandle = CreateFile(FileName, GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0)
'获取该文件的创建 / 访问 / 修改的日期 / 时间属性
GetFileTime lngHandle, nFileTime(0), nFileTime(1), nFileTime(2)
'关闭该句柄
CloseHandle lngHandle
'将文件时间转换为本地文件时间
For I = 0 To 2
FileTimeToLocalFileTime nFileTime(I), lFileTime(I)
Next
'将文件时间转换为系统文件时间
FileTimeToSystemTime lFileTime(TimeItem), SysTime
'将 SYSTEMTIME 类型中的各项元素组合起来赋值给函数
GainFileTime = CDate(SysTime.wYear & "-" & SysTime.wMonth & "-" & SysTime.wDay & " " _
& SysTime.wHour & ":" & SysTime.wMinute & ":" & SysTime.wSecond)
End Function
Function ISflie30ago(sflie As String) As Boolean
'检查一个文件是否过去了30天,如果是则返回true
adate = GainFileTime(sflie, 2)
adate1 = getsec(adate, 1, " ")
adate2 = getsec(Date, 1, " ")
ag = CDate(adate2) - CDate(adate1)
If ag > 30 Then
ISflie30ago = True
Exit Function
Else
ISflie30ago = False
Exit Function
End If
End Function
Private Sub Command1_Click()
On Error GoTo err2:
If MsgBox("确认要删除指定文件夹下的历史文件吗?", vbYesNo, "请确认删除操作") = vbNo Then Exit Sub
Dir "C:\ABC\"
'dir后面的路径可以改
Do
aflie = CStr(Dir())
If ISflie30ago(CStr(aflie)) = True Then
Kill aflie
End If
DoEvents
Loop
err2:
If ISflie30ago(Dir("C:\ABC\")) = True Then
Kill Dir("C:\ABC\")
End If
End Sub
2021年08月21日 03点08分
4
ag = CDate(adate2) - CDate(adate1) 这句提示类型不匹配
2021年08月23日 02点08分
实时错误 13
2021年08月23日 02点08分