Daniel Berger
2011-Jan-25 02:20 UTC
[Win32utils-devel] Filesystem monitoring and ExecNotificationQuery
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: # test.rb require ''win32ole'' require ''socket'' host = Socket.gethostname con = "winmgmts://#{host}/root/cimv2" wmi = WIN32OLE.connect(con) drive = "C:" folder = "\\Users\\djberge" sql = %Q{ select * from __InstanceOperationEvent within 2 where TargetInstance isa ''CIM_DataFile'' and TargetInstance.Drive=''#{drive}'' and TargetInstance.Path=''#{folder}'' } events = wmi.ExecNotificationQuery(sql) # end test.rb Here were the results: c:\Users\djberge >ruby test.rb test.rb:20:in `method_missing'': ExecNotificationQuery (WIN32OLERuntimeError) OLE error code:80041058 in SWbemServicesEx Unparsable query. HRESULT error code:0x80020009 Exception occurred. from test.rb:20 Tried it with JRuby, too: c:\Users\djberge >jruby test.rb Dispatch.java:-2:in `invokev'': org.racob.com.ComFailException: Invoke of: ExecNotificationQuery Source: SWbemServicesEx Description: Unparsable query. from Dispatch.java:243:in `invokev'' from Dispatch.java:187:in `callN'' from RubyWIN32OLE.java:203:in `invokeMethodOrGet'' from RubyWIN32OLE.java:112:in `method_missing'' from org/jruby/ext/win32ole/RubyWIN32OLE$i_method_0_0$RUBYINVOKER$method_missing. gen:65535:in `call'' from JavaMethod.java:642:in `call'' from RuntimeHelpers.java:401:in `call'' from DynamicMethod.java:190:in `call'' from CachingCallSite.java:375:in `callMethodMissing'' from CachingCallSite.java:306:in `cacheAndCall'' from CachingCallSite.java:148:in `call'' from test.rb:20:in `__file__'' from test.rb:-1:in `load'' from Ruby.java:690:in `runScript'' from Ruby.java:573:in `runNormally'' from Ruby.java:416:in `runFromMain'' from Main.java:286:in `run'' from Main.java:128:in `run'' from Main.java:97:in `main'' Any ideas? Regards, Dan
Heesob Park
2011-Jan-25 05:32 UTC
[Win32utils-devel] Filesystem monitoring and ExecNotificationQuery
Hi, 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) while 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 end # end test.rb Regards, Park Heesob
Daniel Berger
2011-Jan-26 11:28 UTC
[Win32utils-devel] Filesystem monitoring and ExecNotificationQuery
On Mon, Jan 24, 2011 at 10:32 PM, Heesob Park <phasis at gmail.com> wrote:> Hi, > > 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) > while > ? ? ? ?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 > end > # end test.rb > > > Regards, > Park HeesobExcellent, thank you. Regards, Dan