Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- intInterval = "1"
- strDrive = "C:"
- strFolder = "\Project\"
- strComputer = "."
- intTmrVal = 0
- ' Connect to WMI
- Set objWMIService = GetObject( "winmgmts:" & "{impersonationLevel=impersonate}!\" & strComputer & "rootcimv2" )
- ' The query string
- strQuery = _
- "Select * From __InstanceOperationEvent" _
- & " Within " & intInterval _
- & " Where Targetinstance Isa 'CIM_DataFile'" _
- & " And TargetInstance.Name='C:\Project\test.txt'"
- ' & " And TargetInstance.Drive='" & strDrive & "'"_
- ' & " And TargetInstance.Path='" & strFolder & "'"
- ' Execute the query
- Set colEvents = objWMIService. ExecNotificationQuery (strQuery)
- ' The loop
- Do
- ' Wait for the next event
- ' Get SWbemEventSource object
- ' Get SWbemObject for the target instance
- Set objEvent = colEvents.NextEvent()
- Set objTargetInst = objEvent.TargetInstance
- ' Check the class name for SWbemEventSource
- ' It cane be one of the following:
- ' - __InstanceCreationEvent
- ' - __INstanceDeletionEvent
- ' - __InstanceModificationEvent
- Select Case objEvent.Path_.Class
- ' If it is file creation or deletion event
- ' just echo the file name
- Case "__InstanceCreationEvent"
- WScript.Echo "Created: " & objTargetInst.Name
- Case "__InstanceDeletionEvent"
- WScript.Echo "Deleted: " & objTargetInst.Name
- ' If it is file modification event,
- ' compare property values of the target and previous
- ' instance and echo the properties that have changed
- Case "__InstanceModificationEvent"
- Set objPrevInst = objEvent.PreviousInstance
- For Each objProperty In objTargetInst.Properties_
- If objProperty.Value <> _
- objPrevInst.Properties_(objProperty.Name) Then
- WScript.Echo "Changed: " _
- & objTargetInst.Name
- WScript.Echo "Property: " _
- & objProperty.Name
- WScript.Echo "Previous value: " _
- & objPrevInst.Properties_(objProperty.Name)
- WScript.Echo "New value: " _
- & objProperty.Value
- ' WScript.Echo
- End If
- Next
- End Select
- 'Count how many times it has been modified // just playing with a counter
- If objEvent.TargetInstance.LastModified <> objEvent.PreviousInstance.LastModified Then
- intTmrVal=intTmrVal+1
- WScript.Echo "Changed: " & intTmrVal & " times"
- WScript.Echo
- End If
- Loop
Add Comment
Please, Sign In to add comment