'随时监视文件的建立与删除
Function monfile
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colMonitoredfolder = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceOperationEvent within 1 where " _
& "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
& "TargetInstance.GroupComponent= " _
& "'Win32_Directory.Name=""c:\\\\aa""'")
Do
Set objEvent = colMonitoredfolder.NextEvent
Set oFile = objWMIService.get(objEvent.TargetInstance.PartComponent)
Select Case objEvent.Path_.Class
Case "__InstanceCreationEvent"
MsgBox "大小为"&oFile.FileSize&"的文件"&oFile.name&"建立"
Case "__InstanceDeletionEvent"
MsgBox "大小为"&oFile.FileSize&"的文件"&oFile.name&"删除"
End Select
Loop
End Function
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceOperationEvent WITHIN 3 WHERE " _
& "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
& "TargetInstance.GroupComponent= " _
& "'Win32_Directory.Name=""c:\\\\aa""'")
Do While True
Set objEventObject = colMonitoredEvents.NextEvent()
Select Case objEventObject.Path_.Class
Case "__InstanceCreationEvent"
Wscript.Echo "A new file was just created: " & _
objEventObject.TargetInstance.PartComponent
Case "__InstanceDeletionEvent"
Wscript.Echo "A file was just deleted: " & _
objEventObject.TargetInstance.PartComponent
End Select
Loop作者: huahua0919 时间: 2008-11-20 04:14 从Slore兄的代码看,应该是在get方法获取事件对象的文件属性时,如果文件创建则能够获取其大小等相关属性,而删除则无法获取其相关属性,所以关键问题处在get方法上。
先创建文件------get方法获取对象文件属性值--------返回成功
先删除文件------get方法获取对象文件属性值--------返回失败(此时文件已经被删除)作者: fzhang 时间: 2008-11-20 19:04
Quote:
Originally posted by huahua0919 at 2008-11-20 04:14:
从Slore兄的代码看,应该是在get方法获取事件对象的文件属性时,如果文件创建则能够获取其大小等相关属性,而删除则无法获取其相关属性,所以关 ...