From: Brian Wallace Date: 2009-04-22T03:11:34+09:00 Subject: Re: directory watcher, trying to match filename to directory name. --00151750e0b4ab96d20468149387 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Hi Tim, Thanks for the reply, since your the developer of the gem it is really nice that you've taken the time to respond to my issue. You've made one of my _favorite_ ruby gems so far... Your example helps me a LOT , especially since I'm horrible at regular expressions :) .. I needed to show the application this morning, so I frantically wrote an alternative - which may be ugly, but works pretty well so far.. Basically, I grab all of the subdirectories of the Directory Watcher directory, and then create an array of glob patterns with the directory names, and filename ie: @dir = Dir.open(@dirwatch) parsed_dir = @dir.entries.grep(/^[^.]/).map parsed_glob = parsed_dir.collect {|d| d = "*#{d}/#{d}*"} I then just pass the parsed_glob array to the directory watcher ... With that said - and since I am so horrible with regular expressions ... Does anyone know the RegExp that will match directory names ONLY ? (excluding '.' and '..') The one I have above appears to match anything except '.' , and '..' Thanks, Brian On Tue, Apr 21, 2009 at 11:35 AM, Tim Pease wrote: > On Tue, Apr 21, 2009 at 6:51 AM, Brian Wallace > wrote: > > Thanks David, > > > > Yes I've read every bit of documentation for Directory Watcher ... I > guess > > my problem is more of a generic ruby programming problem. > > > > I'm not sure how to dynamically change the glob pattern ...Since its set > > during the creation of the dw object .. Of course I can change it, within > > the add_observer block - with a symbol.. But the problem I'm having is > > getting the subdirectory name for matching... > > > > ie: C:\Send has 3 subdirectories > > > > C:\Send\Bob > > C:\Send\Kevin > > C:\Send\James > > > > I want to only match a filename that has "Bob, Kevin, or James" inside > its > > respective subdirectory. > > > > DirectoryWatcher is not able to do what you need. It only supports > glob patterns for matching files, and what you are in need of is > regular expressions. The best solution is to implement the regular > expression matching inside your observer. > > def observer_method( *args ) > args.each do |event| > next unless event.type == :added > next unless event.path =~ %r/^#{@directory}\/([^\/]+)\/.*\1.*/ > # put your code here > end > end > > Also, you have a sleep method in your observer block. I would > recommend using the "stable" event if you want to make sure that the > file is no longer being modified before operating on it. > > Blessings, > TwP > > --00151750e0b4ab96d20468149387--