# Ruby gem to watch file

**URL:** <https://rubytalk.org/t/ruby-gem-to-watch-file/49211>\
**Category:** ruby-talk\
**Created:** [21 September 2008 01:17 UTC](https://rubytalk.org/t/ruby-gem-to-watch-file/49211 "2008-09-21T01:17:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Junkone](https://avatars.discourse-cdn.com/v4/letter/j/e274bd/32.png) [@Junkone](https://rubytalk.org/u/Junkone)\
**Post date:** [21 September 2008 01:17 UTC](https://rubytalk.org/t/ruby-gem-to-watch-file/49211/1 "2008-09-21T01:17:06Z")

</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:** ![Tim\_Pease](https://avatars.discourse-cdn.com/v4/letter/t/3e96dc/32.png) [@Tim\_Pease](https://rubytalk.org/u/Tim_Pease)\
**Post date:** [21 September 2008 03:15 UTC](https://rubytalk.org/t/ruby-gem-to-watch-file/49211/2 "2008-09-21T03:15:36Z")

</div>

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

> **···**
>
> 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.

---

<div class="post-metadata">

**Author:** ![Junkone](https://avatars.discourse-cdn.com/v4/letter/j/e274bd/32.png) [@Junkone](https://rubytalk.org/u/Junkone)\
**Post date:** [22 September 2008 14:58 UTC](https://rubytalk.org/t/ruby-gem-to-watch-file/49211/3 "2008-09-22T14:58:09Z")

</div>

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'  
&nbsp;&nbsp;&nbsp;dw = DirectoryWatcher.new '.', :pre\_load =\> true  
&nbsp;&nbsp;&nbsp;dw.glob = "c:\\temp\\scans\\" # is the directory i want to watch.  
&nbsp;&nbsp;&nbsp;dw.add\_observer {|\*args| args.each {|event| puts event}}  
&nbsp;&nbsp;&nbsp;dw.start  
&nbsp;&nbsp;&nbsp;gets  
&nbsp;&nbsp;&nbsp;dw.stop

> **···**
>
> On Sep 20, 11:15 pm, Tim Pease \<tim.pe...@gmail.com\> wrote:
> 
> > 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:** ![Siep\_Korteling](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@Siep\_Korteling](https://rubytalk.org/u/Siep_Korteling)\
**Post date:** [22 September 2008 22:36 UTC](https://rubytalk.org/t/ruby-gem-to-watch-file/49211/4 "2008-09-22T22:36:32Z")

</div>

Junkone 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'  
&nbsp;&nbsp;&nbsp;dw = DirectoryWatcher.new 'D:/temp/'  
&nbsp;&nbsp;&nbsp;dw.interval=5  
&nbsp;&nbsp;&nbsp;dw.add\_observer {|\*args| args.each {|event| puts event}}  
&nbsp;&nbsp;&nbsp;dw.start  
&nbsp;&nbsp;&nbsp;gets  
&nbsp;&nbsp;&nbsp;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

> **···**
>
> > On Sep 20, 11:15�pm, Tim Pease \<tim.pe...@gmail.com\> wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Tim\_Pease](https://avatars.discourse-cdn.com/v4/letter/t/3e96dc/32.png) [@Tim\_Pease](https://rubytalk.org/u/Tim_Pease)\
**Post date:** [23 September 2008 15:53 UTC](https://rubytalk.org/t/ruby-gem-to-watch-file/49211/5 "2008-09-23T15:53:20Z")

</div>

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

> **···**
>
> On Sep 22, 2008, at 4:36 PM, Siep Korteling wrote:
> 
> > Junkone wrote:
> > 
> > > On Sep 20, 11:15�pm, Tim Pease \<tim.pe...@gmail.com\> 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'  
> > &nbsp;&nbsp;dw = DirectoryWatcher.new 'D:/temp/'  
> > &nbsp;&nbsp;dw.interval=5  
> > &nbsp;&nbsp;dw.add\_observer {|\*args| args.each {|event| puts event}}  
> > &nbsp;&nbsp;dw.start  
> > &nbsp;&nbsp;gets  
> > &nbsp;&nbsp;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?
