http://qs321.pair.com?node_id=872113


in reply to Re^2: File::Monitor problem with batch files
in thread File::Monitor problem with batch files

It actually was working just as it should work. I called scan twice, so it shut down. I don't think that you need a while loop. For example, you can turn on the monitor, and it will dutifully observe what it's supposed to watch:
#!/usr/bin/perl use strict; use warnings; use File::Monitor; my @Files = qw(1.doc 2.doc 3.xls); my $wFileMonitor = File::Monitor->new(); foreach my $wFile(@Files){ $wFileMonitor->watch($wFile); } $wFileMonitor->scan;
Now the monitor is on. It will stay on until you call the second scan---then it reports the changes and shuts off.

Replies are listed 'Best First'.
Re^4: File::Monitor problem with batch files
by xbmy (Friar) on Nov 18, 2010 at 14:53 UTC

    Sounds good. Thanks!

    But what i want is the "monitor always on" during the period of which i compose my files, the code monitor my "saving" action and then copy the new modified file to the newpath automatically.

    Your code does work well after i added a while loop, but just for the first file in the "@Files" list, after i modified and saved the second and third file, there was no response.

    May be i didn't totally understand you, please don't hesitate to let me know!

Re^4: File::Monitor problem with batch files
by xbmy (Friar) on Nov 18, 2010 at 19:33 UTC

    Finally, it worked! I tried like this use the file::monitor::delta package:

    use File::Monitor; use File::Copy::Recursive qw(fcopy rcopy dircopy fmove rmove dirmove); my $monitor = File::Monitor->new(); # Watch some files for my $file (qw( 1.doc 2.xls)) { $monitor->watch( $file ); } # First scan just finds out about the monitored files. No changes # will be reported. #$object->scan; while (1) { $monitor->scan; sleep 10; # After the first scan we get a list of File::Monitor::Delta objec +ts # that describe any changes my @changes = $monitor->scan; for my $change (@changes) { # Call methods on File::Monitor::Delta to discover what change +d if ($change->is_mtime) { my $name = $change->name; my $old_mtime = $change->old_mtime; my $new_mtime = $change->new_mtime; print "$name changed at $new_mtime\n"; fcopy ("$name","p:\\phdpaper"); #backup automatically for +the file which just has been modified } } }

    Thank you for your help!