Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: File::Monitor problem with batch files

by Khen1950fx (Canon)
on Nov 17, 2010 at 23:37 UTC ( #872092=note: print w/ replies, xml ) Need Help??


in reply to File::Monitor problem with batch files

I tried it like this:

#!/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; my @changes = $wFileMonitor->scan; foreach my $change(@changes) { if ($change->is_mtime) { my $mtime = $change->is_mtine; my $old_mtime = $change->old_mtime; my $new_mtime = $change->new_mtime; print "$mtime has changed mtime from $old_mtime" . "to $new_mtime\n"; } }


Comment on Re: File::Monitor problem with batch files
Download Code
Re^2: File::Monitor problem with batch files
by xbmy (Friar) on Nov 18, 2010 at 00:41 UTC

    Your code just stop immediately after running, I think it need at least one loop as while (1){ ..... } or something, but after i added the while loop, it still not work for me, it looks like the code i have posted. Thanks anyway!

      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.

        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!

        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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (9)
As of 2014-07-30 06:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (229 votes), past polls