November 1st, 2012, 05:19 AM
VBScript File monitor
I would like to monitor a text file. Currently I use the following vbscript 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).
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 & "'")
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
' WScript.Echo Now & vbTab & MyFile & " was created" & vbCrlf
' WScript.Echo Now & vbTab & MyFile & " was deleted" & vbCrLf
If objLatestEvent.TargetInstance.LastModified <> objLatestEvent.PreviousInstance.LastModified Then
'WScript.Echo Now & vbTab & MyFile & " was modified" & vbCrLf
MsgBox "File was modified."
Set objWMIService = Nothing
Set colMonitoredEvents = Nothing
Set objLatestEvent = Nothing
November 1st, 2012, 06:26 PM
The "Timer" function returns the number of seconds since midnight. After the first detection, you would put your script into a loop and check when the desired time has expired. InVB6 I would normally use a DoEvents function within that loop, but I don't believe that it works in VBScript, and I also believe that it is not necessary because VBScript is a Pseudo language. Make sure to account for a change that might occur at midnight.
November 30th, 2012, 04:17 AM
Which S/W is create with the help of Visual basic then this s/w setting change like size change according to used monitor.
Comments on this post
January 1st, 2013, 05:47 AM
why do you monitor file
what is the size and contents of the file
it may be just as easy to read it
here to help