On Oct 11, 2004, at 4:15 AM, nkb wrote:
> I placed the following codes at the bottom of the file  
> DirectoryWatch.rb and ran it from my command line in WindowsXp.  
> Nothing seems to be happening even when I change/add/modify files.  
> What am I missing? Thanks.

The main problem is that while my library starts a new thread, you need  
your code to keep running for that thread to keep running. Your code  
would run and then immediately finish. A working example is below; note  
in particular the unending while loop, which keeps the main program  
running while the DirectoryWatcher thread runs.

An ancillary problem is that my library was not closing the file  
references it created and passed around, which meant that on Windows  
you couldn't modify or delete the files once it had scanned them once.  
I've fixed this problem in version 0.9.6:  
http://phrogz.net/RubyLibs/DirectoryWatcher.rb

Here's a program that I tested to work on Windows; I started it  
running, saw it add the files in the 'watch_dir' subdirectory. I opened  
a separate command window and updated and renamed files, and removed  
them. The stdout output of the program follows its source code:

-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_- 
_-_-_-_
file: dw_test.rb
-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_- 
_-_-_-_
#!/usr/local/bin/ruby

require 'DirectoryWatcher'
require 'singleton'

class App
   include Singleton

   @@plugin_dir = 'watch_dir'
   @@rescan_delay = 5

   def initialize
     @watcher = Dir::DirectoryWatcher.new( @@plugin_dir, @@rescan_delay )
     @watcher.on_add = Proc.new{ |the_file, stats_hash|
       stats_hash[:special_number] = rand(100)
       puts "#{the_file.path} has been added; it's been assigned special  
number ##{stats_hash[:special_number]}"
     }
     @watcher.on_modify = Proc.new{ |the_file, stats_hash|
       puts "#{the_file.path} (##{stats_hash[:special_number]}) just  
changed."
     }
     @watcher.on_remove = Proc.new{ |stats_hash|
       puts "It's time to remove ##{stats_hash[:special_number]}"
     }
   end

   def run
     @watcher.start_watching
     while true
       sleep 60
     end
   end
end

$app = App.instance
$app.run

-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_- 
_-_-_-_
Output
-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_- 
_-_-_-_
A:\dwtest> ruby dw_test.rb
watch_dir/foo.txt has been added; it's been assigned special number #76
watch_dir/foo.bar has been added; it's been assigned special number #66
It's time to remove #76
watch_dir/foo.txt has been added; it's been assigned special number #71
watch_dir/foo.txt (#71) just changed.
It's time to remove #71


--
(-, /\ \/ / /\/