Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

File::Monitor multifunction PERL script

Posted on 2007-04-03
7
Medium Priority
?
2,991 Views
Last Modified: 2007-10-18
Follow up to previous question...

I got a great script from Clockwatcher and want to modify it so that it returns not just files added and deleted to the directory, but if existing files have changed since the initial scan.  I need the name of the file that changed as a return value,

Here is what I started with:

use File::Monitor;

my $monitor = File::Monitor->new();

$monitor->watch( {
        name        => 'c:/logs',
        callback    => \&SomethingHappened,
      files => 1
      }
    );
   

$monitor->scan();

for ($i=0; $i < 10; $i++)
{
      $monitor->scan();    
      sleep 10;
}

sub SomethingHappened
{
      my ($name, $event, $change) = @_;

      my @adds = $change->files_created;
      my @dels = $change->files_deleted;

      print "Added: ".join("\nAdded: ", @adds)."\n" if @adds;
      print "Removed: ".join("\nRemoved: ", @dels)."\n" if @dels;

}

I have tried variations of the line:
my @mods = $change(mtime);  but haven't had any luck.
0
Comment
Question by:itcs-css
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 84

Assisted Solution

by:ozo
ozo earned 600 total points
ID: 18846508
print "Time changed: $name\n" if $change->is_time;
0
 
LVL 1

Author Comment

by:itcs-css
ID: 18846739
Ozo...

The comment helps me start to understand how to call this function.  But adding this only works to add another print line for added or deleted files.

clockwatchers script is pretty specifically for watching one directory...what I need is a way to watch the files inside without knowing ahead of time which files are in there at the start of the monitor.
0
 
LVL 84

Expert Comment

by:ozo
ID: 18846916
If you are monitoring files addeed and removed, then you know at all times which files are  in there
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:itcs-css
ID: 18846993
Sorry for not being clear on this.

I need to watch for 2 separate events...

a file is added or removed from the directory (clockwatcher's cover this perfectly)

a file that existed when the monitor was set has been updated with a config change (the mtime change is what I think will work for this)

What I see when I add the line above only seems to take effect on the added or deleted files.  I think my logic here is off in reading the File::Monitor docs.  It seems like an either/or directory or files function and not both.

Am I wrong in that aspect?
0
 
LVL 17

Accepted Solution

by:
mjcoyne earned 900 total points
ID: 18847408
I don't have this module to check this out, but by reading the docs for File::Monitor::Delta (see http://search.cpan.org/~andya/File-Monitor-v0.0.5/lib/File/Monitor/Delta.pm), it looks as though if you add:

my @filestats = $change->is_metadata;

and:

print "Changed: ".join("\nChanged: ", @filestats)."\n" if @filestats;

you should be able to detect changes in the metadata of the file, which includes mtime, ctime, uid, gid and mode.
0
 
LVL 1

Author Comment

by:itcs-css
ID: 18850865
mjcoyne...

I tried a few variations on that theme, but I only ever get "Changed" reports when a new file is added or an existing file is deleted and not any changes when existing files are modified.

I suspect it has to do with the explicit files => 1 in the given script, but taking it out doesn't seem to matter.  There seems to be a missing example in the docs on how something in here works.  :(
0
 
LVL 1

Author Comment

by:itcs-css
ID: 18851736
It isn't pretty but this works by establishing multiple monitors that respond to the same looped scan...Now I just have to make it output the hash value in something human readable and I'll be on my way.

use File::Monitor;
use File::Monitor::Object;

my $monitor = File::Monitor->new();

chdir 'c:\\temp';
$temp = 'c:\\temp';

opendir DIR, $temp;
while ($file = readdir DIR) {
  next if $file =~ /^\./;
  push @files, $file;
}

#watch the files in the directory for changes
foreach (@files) {
$monitor->watch("$_", sub {
    my ($name, $event, $change) = @_;
    print "$name : $change \n";
    });
}

#Watch the directory for changes
$monitor->watch( {
        name        => "$temp",
        recurse     => 1,
        callback    => \&Test,
    } );

$monitor->scan;

for ($i=0; $i < 100; $i++)
{
      my @changes = $monitor->scan;  
      sleep 5;
}

sub Test
{
      my ($name, $event, $change) = @_;

      my @adds = $change->files_created;
      my @dels = $change->files_deleted;
     
      print "Added: ".join("\nAdded: ", @adds)."\n" if @adds;
      print "Removed: ".join("\nRemoved: ", @dels)."\n" if @dels;
}

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question