Hi,
I would like to monitor a text file. Currently I use the following code:
The text file is modified 5 or 6 times in a minute or two, and I would like to be notified only after the last file modification (the attached code notifies me after the first modification). How could I do that? (The text file modification procedure: the file is being changed many times - about 5 or 6 times - in a minute, after that minute the file is unchanged in the next several hours).
I would like to monitor a text file. Currently I use the following code:
Code:
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
'Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
' ("SELECT * FROM __InstanceModificationEvent WITHIN 1 WHERE " _
' & "TargetInstance ISA 'CIM_DataFile' and " _
' & "TargetInstance.Name='U:\\Programming\\Hydra\\Label\\LogFile01.txt'")
strPnrPath01 = "U:\\Programming\\Test05\\Test01.txt"
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceOperationEvent WITHIN 20 WHERE " _
& "TargetInstance ISA 'CIM_DataFile' and " _
& "TargetInstance.Name='" & strPnrPath01 & "'")
Do
Set objLatestEvent = colMonitoredEvents.NextEvent
'WScript.Echo "File: " & objLatestEvent.TargetInstance.Name
'WScript.Echo "New size: " & objLatestEvent.TargetInstance.FileSize
'Wscript.Echo "Old size: " & objLatestEvent.PreviousInstance.FileSize
Select Case objLatestEvent.Path_.Class
'Case "__InstanceCreationEvent"
' WScript.Echo Now & vbTab & MyFile & " was created" & vbCrlf
'Case "__InstanceDeletionEvent"
' WScript.Echo Now & vbTab & MyFile & " was deleted" & vbCrLf
Case "__InstanceModificationEvent"
If objLatestEvent.TargetInstance.LastModified <> objLatestEvent.PreviousInstance.LastModified Then
'WScript.Echo Now & vbTab & MyFile & " was modified" & vbCrLf
MsgBox "File was modified."
End If
End Select
Loop
Set objWMIService = Nothing
Set colMonitoredEvents = Nothing
Set objLatestEvent = Nothing
The text file is modified 5 or 6 times in a minute or two, and I would like to be notified only after the last file modification (the attached code notifies me after the first modification). How could I do that? (The text file modification procedure: the file is being changed many times - about 5 or 6 times - in a minute, after that minute the file is unchanged in the next several hours).