[Win32utils-devel] Filesystem monitoring and ExecNotificationQuery

Heesob Park phasis at gmail.com
Tue Jan 25 00:32:36 EST 2011


2011/1/25 Daniel Berger <djberg96 at gmail.com>:
> Hi,
> I thought I'd attempt a pure Ruby version of win32-changejournal. I came
> across this post:
> http://www.codeproject.com/Articles/42212/WMI-and-File-System-Monitoring.asp
> x
> Within it he uses ExecNotificationQuery. However, I can't make it work. I
> tried this snippet:
> Any ideas?
It seems that folder's separator must be "\\\\".
Here is a working sample:

# test.rb
require 'win32ole'
require 'socket'

host = Socket.gethostname
con = "winmgmts://#{host}/root/cimv2"
wmi = WIN32OLE.connect(con)
drive  = "C:"
folder = "\\\\Users\\\\phasis"

sql = %Q{
 select *
 from __InstanceOperationEvent
 within 2
 where TargetInstance isa 'CIM_DataFile'
 and TargetInstance.Drive='#{drive}'
 and TargetInstance.Path='#{folder}\\\\'
events = wmi.ExecNotificationQuery(sql)
	event = events.NextEvent
	case event.Path_.Class
	when "__InstanceCreationEvent"
		puts "A new file was just created: #{event.TargetInstance.FileName}"
	when "__InstanceModificationEvent"
		puts "A file was just modified: #{event.TargetInstance.FileName}"
	when "__InstanceDeletionEvent"
		puts "A file was just deleted: #{event.TargetInstance.FileName}"
# end test.rb

Park Heesob

More information about the win32utils-devel mailing list