Guest User

Untitled

a guest
Feb 16th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. intInterval = "1"
  2. strDrive = "C:"
  3. strFolder = "\Project\"
  4. strComputer = "."
  5. intTmrVal = 0
  6.  
  7. ' Connect to WMI
  8.  
  9. Set objWMIService = GetObject( "winmgmts:" & "{impersonationLevel=impersonate}!\" & strComputer & "rootcimv2" )
  10.  
  11. ' The query string
  12.  
  13. strQuery = _
  14. "Select * From __InstanceOperationEvent" _
  15. & " Within " & intInterval _
  16. & " Where Targetinstance Isa 'CIM_DataFile'" _
  17. & " And TargetInstance.Name='C:\Project\test.txt'"
  18. ' & " And TargetInstance.Drive='" & strDrive & "'"_
  19. ' & " And TargetInstance.Path='" & strFolder & "'"
  20.  
  21. ' Execute the query
  22.  
  23. Set colEvents = objWMIService. ExecNotificationQuery (strQuery)
  24.  
  25. ' The loop
  26.  
  27. Do
  28. ' Wait for the next event
  29. ' Get SWbemEventSource object
  30. ' Get SWbemObject for the target instance
  31.  
  32. Set objEvent = colEvents.NextEvent()
  33. Set objTargetInst = objEvent.TargetInstance
  34.  
  35. ' Check the class name for SWbemEventSource
  36. ' It cane be one of the following:
  37. ' - __InstanceCreationEvent
  38. ' - __INstanceDeletionEvent
  39. ' - __InstanceModificationEvent
  40.  
  41. Select Case objEvent.Path_.Class
  42.  
  43. ' If it is file creation or deletion event
  44. ' just echo the file name
  45.  
  46. Case "__InstanceCreationEvent"
  47. WScript.Echo "Created: " & objTargetInst.Name
  48.  
  49. Case "__InstanceDeletionEvent"
  50. WScript.Echo "Deleted: " & objTargetInst.Name
  51.  
  52. ' If it is file modification event,
  53. ' compare property values of the target and previous
  54. ' instance and echo the properties that have changed
  55.  
  56. Case "__InstanceModificationEvent"
  57.  
  58. Set objPrevInst = objEvent.PreviousInstance
  59.  
  60. For Each objProperty In objTargetInst.Properties_
  61. If objProperty.Value <> _
  62. objPrevInst.Properties_(objProperty.Name) Then
  63. WScript.Echo "Changed: " _
  64. & objTargetInst.Name
  65. WScript.Echo "Property: " _
  66. & objProperty.Name
  67. WScript.Echo "Previous value: " _
  68. & objPrevInst.Properties_(objProperty.Name)
  69. WScript.Echo "New value: " _
  70. & objProperty.Value
  71. ' WScript.Echo
  72. End If
  73. Next
  74. End Select
  75.  
  76. 'Count how many times it has been modified // just playing with a counter
  77. If objEvent.TargetInstance.LastModified <> objEvent.PreviousInstance.LastModified Then
  78. intTmrVal=intTmrVal+1
  79. WScript.Echo "Changed: " & intTmrVal & " times"
  80. WScript.Echo
  81. End If
  82.  
  83. Loop
Add Comment
Please, Sign In to add comment