Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: how to permanently monitor a directory

by BrowserUk (Patriarch)
on Aug 14, 2003 at 14:43 UTC ( [id://283893]=note: print w/replies, xml ) Need Help??


in reply to how to permanently monitor a directory

If your running Win32 then Win32::ChangeNotify makes this very easy.

#! perl -slw use strict; use Win32::ChangeNotify; my $path = 'p:\test'; my $notify = Win32::ChangeNotify->new( $path, 0, 'FILE_NAME' ); my %last; @last{ glob $path . '/*' } = (); while( 1 ) { print('Nothing changed'), next unless $notify->wait( 10_000 ); # Check every 10 seconds $notify->reset; print $/, 'Something changed'; my @files = glob $path . '/*'; if( @files < scalar keys %last ) { delete @last{ @files }; print 'These files where deleted: '; print for keys %last; print''; } elsif( @files > scalar keys %last ) { my %temp; @temp{ @files } = (); delete @temp{ keys %last }; print 'These files where created: '; print for keys %temp; print''; } else { print "A non-deletion or creation change occured"; } undef %last; @last{ @files } = (); } __END__ P:\test>changenotify Nothing changed Nothing changed Something changed These files where created: p:\test/fred Nothing changed Nothing changed Something changed These files where deleted: p:\test/fred Nothing changed Nothing changed Nothing changed Nothing changed

You'll need control-break rather than ^C to interupt this script.

To extending this to monitor the whole subtree, change the 0 in the new() call to 1, though you'll need some extra work to find out what changed where.

I'm not aware of a similar mechanism under *nix.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
If I understand your problem, I can solve it! Of course, the same can be said for you.

Replies are listed 'Best First'.
Re: Re: how to permanently monitor a directory
by Jenda (Abbot) on Aug 14, 2003 at 14:56 UTC

    The comment on the

    unless $notify->wait( 10_000 );
    line is misleading. The script doesn't check for changes every ten seconds. It waits for a change for 10 seconds, prints the "Nothing changed" message and goes back waiting. Any changes in the directories are noted immediately.

    Jenda
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
       -- Rick Osborne

    Edit by castaway: Closed small tag in signature

      Good point. That was badly worded.


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
      If I understand your problem, I can solve it! Of course, the same can be said for you.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://283893]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (1)
As of 2024-04-25 03:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found