Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re^3: File::Monitor problem with batch files

by Khen1950fx (Canon)
on Nov 18, 2010 at 02:20 UTC ( #872113=note: print w/replies, xml ) Need Help??

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!

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2021-10-25 01:32 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (89 votes). Check out past polls.