Did anyone get DirectoryWatch working?

Hi.
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.

device_manager = Dir::DirectoryWatcher.new( 'c:/temp', 2 )
device_manager.name_regexp = /^[^.].*\.rb$/

device_manager.on_add = Proc.new{ |the_file, stats_hash|
puts "Hey, just found #{the_file.inspect}!"
        # Store something custom
        stats_hash[:blorgle] = the_file.foo
     }

     device_manager.on_modify = Proc.new{ |the_file, stats_hash|
        puts "Hey, #{the_file.inspect} just changed."
     }

     device_manager.on_remove = Proc.new{ |stats_hash|
        puts "Whoa, the following file just disappeared:"
        stats_hash.each_pair{ |k,v|
           puts "#{k} : #{v}"
        }
    }

     device_manager.start_watching()

nkb wrote:

Hi.
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.

I've never tried it... Gavin can help you. Not sure what time zone he
is in.

Wonder if it's a Windows incompatibility? Does it say what platform he
developed on? I'd guess it was Linux.

Hal

Hrm, dunno; didn't do any Windows testing on it. (I developed on MacOS X.)

I'm bolting off to work right now, but will take a look at it on a Windows box in about 10 hours and see if I can figure out what's up.

···

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:

···

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.

-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
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

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

nkb <nkb@pacific.net.sg> wrote in message news:<416A5D4B.5040806@pacific.net.sg>...

Hi.
I placed the following codes at the bottom of the file DirectoryWatch.rb
and ran it from my command line in WindowsXp.

<snip>

If you don't care about cross platform support, there's also win32-changenotify.

Regards,

Dan