Win32-changenotify and modifying a file

Hey!

I'm working with win32-changenofity library to listen for any changes in a directory. I'm interested in added, deleted and modified files. That's my code:

···

#-------
filter = ChangeNotify::FILE_NAME | ChangeNotify::DIR_NAME
abs_path = File.expand_path(dir)
ChangeNotify.new(abs_path, true, filter) do |cn|
   cn.wait do |events|
     events.each do |event|
       puts(event.action, event.file_name)
     end
   end
end
#--------

The problem is when I modify file on disk, it's not reported. When I use additional filters like SIZE or LAST_WRITE I get changes reported few times. Is there some combination which will report file change only once?

--
Oinopion
http://hauru.eu

Hi,

···

----- Original Message -----
From: "Tomek Paczkowski" <oinopion@gmail.com>
Newsgroups: comp.lang.ruby
To: "ruby-talk ML" <ruby-talk@ruby-lang.org>
Sent: Tuesday, April 15, 2008 9:45 PM
Subject: win32-changenotify and modifying a file

   Hey!

I'm working with win32-changenofity library to listen for any changes in
a directory. I'm interested in added, deleted and modified files. That's
my code:

#-------
filter = ChangeNotify::FILE_NAME | ChangeNotify::DIR_NAME
abs_path = File.expand_path(dir)
ChangeNotify.new(abs_path, true, filter) do |cn|
  cn.wait do |events|
    events.each do |event|
      puts(event.action, event.file_name)
    end
  end
end
#--------

The problem is when I modify file on disk, it's not reported. When I use
additional filters like SIZE or LAST_WRITE I get changes reported few
times. Is there some combination which will report file change only once?

--

It's a feature of ChangeNotify.
You could filter duplicate event like this:

filter = ChangeNotify::FILE_NAME | ChangeNotify::DIR_NAME | ChangeNotify::LAST_WRITE
abs_path = File.expand_path(dir)
pre_event = nil
ChangeNotify.new(abs_path, true, filter) do |cn|
   cn.wait do |events|
     events.each do |event|
       next if [event.action,event.file_name] == pre_event
       pre_event = [event.action,event.file_name]
       puts(event.action, event.file_name)
     end
   end
end

Regards,

Park Heesob

Park Heesob wrote:

It's a feature of ChangeNotify.
You could filter duplicate event like this:

filter = ChangeNotify::FILE_NAME | ChangeNotify::DIR_NAME | ChangeNotify::LAST_WRITE
abs_path = File.expand_path(dir)
pre_event = nil
ChangeNotify.new(abs_path, true, filter) do |cn|
   cn.wait do |events|
     events.each do |event| next if [event.action,event.file_name] == pre_event
       pre_event = [event.action,event.file_name]
       puts(event.action, event.file_name)
     end
   end
end

And what if I modify same file two times in a row? I could count events, as there are 4 for modification. Wouldn't it be better to use changejournal and filter in only "close" actions?

···

--
Oinopion
http://hauru.eu