# Win32-changenotify and modifying a file

**URL:** <https://www.ruby-forum.com/t/win32-changenotify-and-modifying-a-file/134832>\
**Category:** Ruby\
**Created:** [April 15, 2008, 2:46pm UTC](https://www.ruby-forum.com/t/win32-changenotify-and-modifying-a-file/134832 "2008-04-15T14:46:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tomek\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8797f3/32.png) [@Tomek\_P](https://www.ruby-forum.com/u/Tomek_P)\
**Post date:** [April 15, 2008, 2:46pm UTC](https://www.ruby-forum.com/t/win32-changenotify-and-modifying-a-file/134832/1 "2008-04-15T14:46:41Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Tomek\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8797f3/32.png) [@Tomek\_P](https://www.ruby-forum.com/u/Tomek_P)\
**Post date:** [April 15, 2008, 3:21pm UTC](https://www.ruby-forum.com/t/win32-changenotify-and-modifying-a-file/134832/2 "2008-04-15T15:21:54Z")

</div>

Hi,  
----- Original Message -----  
From: “Tomek P.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Newsgroups: comp.lang.ruby  
To: “ruby-talk ML” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Tuesday, April 15, 2008 9:45 PM  
Subject: win32-changenotify and modifying a file

> cn.wait do |events|
> 
> –  
> 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 H.

---

<div class="post-metadata">

**Author:** ![Tomek\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8797f3/32.png) [@Tomek\_P](https://www.ruby-forum.com/u/Tomek_P)\
**Post date:** [April 15, 2008, 3:42pm UTC](https://www.ruby-forum.com/t/win32-changenotify-and-modifying-a-file/134832/3 "2008-04-15T15:42:02Z")

</div>

Park H. wrote:

> ```
> 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?
