现有如下的需求:
比如在D盘根目录下有一个log文件夹,这个文件夹中存放的都是我的一个应用程序的日志文件,日志文件的格式为"模块名+日期",日期格式为yyyymmmdd的,这样的文件有很多。
现在要求是:定期自动删除文件,删除的规则比较简单(下面为规则),但是小弟是初学vb不久,不知道如何计算
规则:比如现在的log文件夹下的日志文件有下面这些,要求只保留最近7天的日志文件(日志文件并不一定是按日期连续的,可能某天并没有日志产生)
Test20080417.log
Test20080418.log
Test20080419.log
Test20080420.log
Test20080421.log
Test20080422.log
Test20080424.log
Test20080425.log
Test20080428.log
Test20080429.log
Test20080430.log
Test20080501.log
Test20080502.log
Test20080503.log
Test20080504.log
Test20080505.log
比如在D盘根目录下有一个log文件夹,这个文件夹中存放的都是我的一个应用程序的日志文件,日志文件的格式为"模块名+日期",日期格式为yyyymmmdd的,这样的文件有很多。
现在要求是:定期自动删除文件,删除的规则比较简单(下面为规则),但是小弟是初学vb不久,不知道如何计算
规则:比如现在的log文件夹下的日志文件有下面这些,要求只保留最近7天的日志文件(日志文件并不一定是按日期连续的,可能某天并没有日志产生)
Test20080417.log
Test20080418.log
Test20080419.log
Test20080420.log
Test20080421.log
Test20080422.log
Test20080424.log
Test20080425.log
Test20080428.log
Test20080429.log
Test20080430.log
Test20080501.log
Test20080502.log
Test20080503.log
Test20080504.log
Test20080505.log
7天之前就是:#2008-5-5# - 7 = #2008-04-28#
那么只要符合一下命名的文件都需要删除:(文件名 Like "Test########.log") And (文件名 <= "Test20080428.log")
appdisk = IIf(Right(App.Path, 1) = "\", App.Path, App.Path & "\")
Randomize
For i = 1 To 20
RndNum = Int(Rnd * 31) + 1 '这里只是举例别管该月有几天
List1.AddItem "test" & Mid(Date$, 1, 4) & Mid(Date$, 6, 2) & Format(CStr(RndNum), "00") & ".log"
Next i
End SubPrivate Sub Command1_Click()
For i = List1.ListCount - 8 To 0 Step -1
'kill appdisk & list1.list(i) '删除文件
List1.RemoveItem i
Next i
End Sub
Dim dt As Date, sFileName As String, sOldFileName As String
dt = Date - 7
sOldFileName = "Test" & Format$(dt, "yyyymmdd") & ".log"
sFileName = Dir("D:\Log\*.log")
While LenB(sFileName) <> 0
If (sFileName Like "Test########.log") And (sFileName <= sOldFileName) Then
Kill "D:\Log\" & sFileName
End If
sFileName = Dir()
Wend
End Sub
If (Asc(Right$(strPath, 1)) <> 92) Then strPath = strPath & "\"
lDelDate = Int(Now()) - 6
strFile = Dir$(strPath & "*.log")
Do While (Len(strFile) > 0)
strDate = Left$(Right$(strFile, 12), 8)
If (Len(strDate) = 8) Then
If (Val(strDate) > 0) Then
lFileDate = CDate(Format$(strDate, "0000-00-00"))
If (lFileDate < lDelDate) Then
Kill strPath & strFile
End If
End If
End If
strFile = Dir$()
LoopEnd Sub
一个建议:
1.确定保留的七个文件(这个试试上面各位的方案吧)
2.把这七个文件改名,最好是改后缀,例如改为Test20080417.bak之类。。
3.删除文件夹内所有后缀为log的文件,dos行,fso行,wmi也行方法随你喜欢例如用dos就一句del d:\log\*.log
4.把刚才那7个文件名改回去。
另外,还有个问题希望探讨一下,因为这些模块是vb6的一些ocx或dll文件,日志输出的function也是在这些模块里面,这些模块都是被.net那边调起来的,这样的一个删除操作的话,我应该在什么样的时机来进行?怎样才是一个高效简洁的做法?
使用List排序最方便了,因为你的文件命名方式很规范, 非常好的习惯, 当然不用List1排序用数组排序也行,
但是因你的数据量庞大的话,使用List1排序效率会较高.'添加 Command1 List1 (List1的sorted设为True)Option Explicit
Dim i&, Appdisk$, Fnm$Private Sub Form_Load()
Appdisk = IIf(Right(App.Path, 1) = "\", App.Path, App.Path & "\")
Fnm = Dir(Appdisk & "*.log")
List1.Visible = False '暂时隐藏的话效率较高
Do While Len(Fnm) > 0
List1.AddItem Appdisk & Fnm
Fnm = Dir
Loop
List1.Visible = True
End SubPrivate Sub Command1_Click()
If List1.ListCount > 7 Then '保留7笔所以有大于7笔才执行删除
For i = List1.ListCount - 8 To 0 Step -1
Kill List1.List(i) '删除文件
List1.RemoveItem i
Next i
MsgBox "清除完成"
End If
End Sub