# Ruby gem to watch file

**URL:** <https://www.ruby-forum.com/t/ruby-gem-to-watch-file/149127>\
**Category:** Ruby\
**Created:** [September 21, 2008, 3:50am UTC](https://www.ruby-forum.com/t/ruby-gem-to-watch-file/149127 "2008-09-21T03:50:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Junkone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Junkone](https://www.ruby-forum.com/u/Junkone)\
**Post date:** [September 21, 2008, 3:50am UTC](https://www.ruby-forum.com/t/ruby-gem-to-watch-file/149127/1 "2008-09-21T03:50:59Z")

</div>

is there any gem that will watch for a file and if the file exists, or  
changes, can trigger process or event. i am looking for something on  
the windows platform only.  
appreciate any help.

---

<div class="post-metadata">

**Author:** ![Junkone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Junkone](https://www.ruby-forum.com/u/Junkone)\
**Post date:** [September 21, 2008, 5:23am UTC](https://www.ruby-forum.com/t/ruby-gem-to-watch-file/149127/2 "2008-09-21T05:23:55Z")

</div>

On Sep 20, 2008, at 7:17 PM, Junkone wrote:

> is there any gem that will watch for a file and if the file exists, or  
> changes, can trigger process or event. i am looking for something on  
> the windows platform only.  
> appreciate any help.

Take a look at directory\_watcher. It works on all platforms.

[http://codeforpeople.rubyforge.org/directory\_watcher/](http://codeforpeople.rubyforge.org/directory_watcher/)

Blessings,  
TwP

---

<div class="post-metadata">

**Author:** ![Junkone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Junkone](https://www.ruby-forum.com/u/Junkone)\
**Post date:** [September 22, 2008, 5:57pm UTC](https://www.ruby-forum.com/t/ruby-gem-to-watch-file/149127/3 "2008-09-22T17:57:01Z")

</div>

On Sep 20, 11:15 pm, Tim P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Blessings,  
> TwP

i tried as you suggested and have a few q.  
i am trying to run a ruby prog to execute if there is any new file in  
a directory. i started out with the examples but am a little confused  
on how to useit.  
this prog seems to be looking at the current directory where it is  
running and not where i had specificed it to watch with dw.glob.

appreciate any help.

seede

require ‘directory\_watcher’  
dw = DirectoryWatcher.new ‘.’, :pre\_load =\> true  
dw.glob = “c:\temp\scans\” # is the directory i want to watch.  
dw.add\_observer {|\*args| args.each {|event| puts event}}  
dw.start  
gets  
dw.stop

---

<div class="post-metadata">

**Author:** ![Junkone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Junkone](https://www.ruby-forum.com/u/Junkone)\
**Post date:** [September 23, 2008, 12:44am UTC](https://www.ruby-forum.com/t/ruby-gem-to-watch-file/149127/4 "2008-09-23T00:44:45Z")

</div>

Junkone wrote:

> On Sep 20, 11:15ï¿½pm, Tim P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Blessings,  
> > TwP
> 
> i tried as you suggested and have a few q.  
> i am trying to run a ruby prog to execute if there is any new file in  
> a directory. i started out with the examples but am a little confused  
> on how to useit.  
> this prog seems to be looking at the current directory where it is  
> running and not where i had specificed it to watch with dw.glob.
> 
> appreciate any help.
> 
> seede

(…)  
Hm, I can’t get it to work either (on WinXP).

require ‘directory\_watcher’  
dw = DirectoryWatcher.new ‘D:/temp/’  
dw.interval=5  
dw.add\_observer {|\*args| args.each {|event| puts event}}  
dw.start  
gets  
dw.stop

It does display the files (“added ‘D:/temp/Pink\_Pajamas.amv’” etc), but  
after that you can delete, add and edit files without directory\_watcher  
noticing anything. Is it broken on windows or am I doing something  
wrong?

Regards,

Siep

---

<div class="post-metadata">

**Author:** ![Junkone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Junkone](https://www.ruby-forum.com/u/Junkone)\
**Post date:** [September 23, 2008, 5:54pm UTC](https://www.ruby-forum.com/t/ruby-gem-to-watch-file/149127/5 "2008-09-23T17:54:04Z")

</div>

On Sep 22, 2008, at 4:36 PM, Siep K. wrote:

> > this prog seems to be looking at the current directory where it is  
> > dw = DirectoryWatcher.new ‘D:/temp/’  
> > noticing anything. Is it broken on windows or am I doing something  
> > wrong?

The whole program is blocking on the “gets”.

On the windows platform, all threads block when reading from standard  
input. So the “gets” is causing all threads to wait including the  
directory watcher thread.

Try replacing the “gets” with a “sleep 30” or some other time interval.

Blessings,  
TwP
