Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File monitor

Status
Not open for further replies.

kadara

Programmer
Mar 2, 2012
26
0
0
DE
Hi,
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).
 
There really isn't a way to accurately msgbox you when the "last" modification has been done because the number of modifications to a file vary durings a varying amount of time.

Although, you could use a timing mechanism to emulate your request. Everytime a modification is encountered, set a var to now. At the end of the loop, check to see how long it's been since the last mod. If greater than x seconds, it may be safe to assume it was the last mod and the user should be notified.

Code:
...
Do
	Set objLatestEvent = colMonitoredEvents.NextEvent
	Select Case objLatestEvent.Path_.Class
		Case "__InstanceModificationEvent"
			If objLatestEvent.TargetInstance.LastModified <> objLatestEvent.PreviousInstance.LastModified Then
				dtmTimer = now
				blnNotify = true
			End If
	End Select

	if (blnNotify) then
		if (datediff("s", dtmTimer, now) > 30) then
			msgbox "The last file modification happened"
			blnNotify = false
		end if
	end if
Loop
...

-Geates
 
@Any: What is the benefit gained from using [tt]objWMIService.ExecNotificationQuery[/tt] over an FSO object?

-Geates
 
Mostly a dramatially lower CPU loading than when polling via FSO. There are a bunch of other advantages once you start wanting to monitor multiple files or folders, or group similar events together, or would like the notifications raised as events
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top