monitor files in a directory using perl (Win32)
Friday, May 28, 2010 9:33:14 AM
#! 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 );
# 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.
$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
REFERENCE: how to permanently monitor a directory
http://www.perlmonks.org/?node_id=283849
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 );
# 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.
$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
REFERENCE: how to permanently monitor a directory
http://www.perlmonks.org/?node_id=283849






