监视非常消耗资源 必须要这么做吗? --------------------------------'建立一个记录最后文件建立时间的变量 Dim dLast As Date Dim dLast_tmp As Date '监控的文件夹 Dim sfld As String '复制目标路径 Dim tfld As String '用timer控制监视 Sub timer1_timer() '用Fso取得文件列表(包括name,DateCreated) Dim fso, f, f1, fc Set fso = CreateObject("Scripting.FileSystemObject") Set f = fso.GetFolder("c:\") Set fc = f.Files For Each f1 In fc Debug.Print f1.Path If f1.DateCreated > dlaset Then FileCopy f1.Path, tfld &"\" & f1.name If dLast_tmp < f1.DateCreated Then dLast_tmp = f1.DateCreated End If Next dLast = dLast_tmp End Sub 原理就是这样的 但是 上面的代码有很大的问题 1 新文件产生的速度如何?如果是同时产生很多个文件 那么复制文件的速度很可能赶不上文件产生的速度 那么就麻烦大了. 2 有1而产生的:timer的inerval应该多大? 3 文件只增加不删除 那么即使你用超大型机 也有崩溃的那一天。
If f1.DateCreated > dlaset Then --------------------------------] If f1.DateCreated > dlast Then
--------------------------------'建立一个记录最后文件建立时间的变量
Dim dLast As Date
Dim dLast_tmp As Date
'监控的文件夹
Dim sfld As String
'复制目标路径
Dim tfld As String
'用timer控制监视
Sub timer1_timer()
'用Fso取得文件列表(包括name,DateCreated)
Dim fso, f, f1, fc
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder("c:\")
Set fc = f.Files
For Each f1 In fc
Debug.Print f1.Path
If f1.DateCreated > dlaset Then
FileCopy f1.Path, tfld &"\" & f1.name
If dLast_tmp < f1.DateCreated Then dLast_tmp = f1.DateCreated
End If
Next
dLast = dLast_tmp
End Sub
原理就是这样的
但是 上面的代码有很大的问题
1 新文件产生的速度如何?如果是同时产生很多个文件 那么复制文件的速度很可能赶不上文件产生的速度
那么就麻烦大了.
2 有1而产生的:timer的inerval应该多大?
3 文件只增加不删除 那么即使你用超大型机 也有崩溃的那一天。
--------------------------------]
If f1.DateCreated > dlast Then
我测试过 当然我的cpu比较垃圾 是p4 2.8的
去掉Debug.Print f1.Path
100个文件3M左右的文件
interval=100 cpu使用率大概在30%左右
interval=200 cpu使用率大概在20%左右
interval=500 cpu使用率大概在10%左右