Link to home
Start Free TrialLog in
Avatar of howruaz9
howruaz9Flag for Canada

asked on

how to monitor the files change in subdirectory?

This code is very good. It can monitor directory change and  the files change in a directory.

Only one issue is it can't monitor the files change in subdirectory. You can see a subdirectory change, but you can't tell which file change in that subdirectory.

Any good idea about this issue?

I really appreciate your help!
use File::Monitor;
use File::Monitor::Object;
 
my $monitor = File::Monitor->new();
 
chdir 'c:\\is';
$temp = 'c:\\dell\test';
 
#watch the files in the directory for changes
foreach (grep {!/^\./} glob("$temp/*")) {
$monitor->watch("$_", sub {
    my ($name, $event, $change) = @_;
    print "$name has been changed.\n";
    });
}
 
#Watch the directory for changes
$monitor->watch( {
        name        => "$temp",
        recurse     => 1,
        callback    => \&Test,
    } );
 
$monitor->scan;
 
for ($i=0; $i < 60; $i++)
{
      my @changes = $monitor->scan;  
      sleep 3;
}
 
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;
}

Open in new window

Avatar of Fairlight2cx
Fairlight2cx
Flag of United States of America image

If you're determined to use this module, I'd probably adjust a few things.

1) Turn off recurse.
2) Do a recursive directory drill-down to collect your directory paths.
3) Set up an instance of the module on each of the harvested paths.  Store each object in an array element.
Avatar of howruaz9

ASKER


Thank you very much Fairlight2cx.

But I'm a beginner of Perl. This script is what I got from this website and just use it.

Would you please tell me more detail about what you said?

Thanks again for your time and patience.


SOLUTION
Avatar of Fairlight2cx
Fairlight2cx
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Fairlight2cx, Thanks so much.

I have tried. But if failed. Nothing showed up about file when that file changed in subdirectory.

I wish I could create another module from scratch. but ...

Anyway thanks very much for your help.

 
Avatar of Adam314
Adam314

It doesn't look like File::Monitor will handle the level of detail you need. Here is some code that should work.
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use File::Find;
 
 
##### Your directory to check
my $temp = 'c:\\dell\test';
$temp = '/home/adam/tmp_ee';
 
##### Variable to store data
my %Files;
 
##### Perform initial scan
find(\&Found_Init, $temp);
 
 
##### Search for changes
scan();
 
sub Found_Init {
	my @stat = stat($File::Find::name);
	$Files{$File::Find::name} = [0, -d _, -f _, @stat];
}
 
sub scan {
	$Files{$_}->[0] = 0 foreach (keys %Files);
	find(\&Found_Diff, $temp);
	foreach (keys %Files) {
		next if $Files{$_}->[0];
		print "Removed: $_\n";
	}
}
	
sub Found_Diff {
	my @stat = stat($File::Find::name);
	my $newdata = [0, -d _, -f _, @stat];
	my $newdatav = [1, -d _, -f _, @stat];
	
	if(!exists($Files{$File::Find::name})) {
		print "New file: $File::Find::name\n";
	}
	elsif(join(' ', @$newdata) ne join(' ', @{$Files{$File::Find::name}})) {
		print "Changed: $File::Find::name\n";
	}
	
	$Files{$File::Find::name} = $newdatav;
}

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks so much Adam314!  This script is very good.

One small issue is it reported "Removed" every time after scan(); while"New file" and "Changed" only one time.

I want to it report in same way, report just one time after "Changed", "New file", "Removed" or every time it scan.

I have tried many time, but failed. Adam314 Would you please help me again to fix this issue?

I really appreciate your help.

1.bmp
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Adam314, your script is exactly what I need.

Thank you very much for your time and help, really appreciated!