Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

When I run the code below I expect to see one print statement every time a file is saved but instead I always see two or more print statements for each save. Does anyone know why this might happen and what I can do to get around it? i only need the last notification.
use Win32::AdvNotify; my $obj = new Win32::AdvNotify || die "$!\n"; my $thread1 = $obj->StartThread( Directory => "C:/spot/run", Filter => LAST_WRITE, WatchSubtree => Yes ) || die "$!\n"; $thread1->EnableWatch() || die "$!\n"; while($obj->Wait(INFINITE)){ while($obj->Read(\@data)){ for($i=0;$i<=$#data;$i++){ print "$data[$i]->{FileName}\n"; } } } $thread1->Terminate(); $obj->Free;

Replies are listed 'Best First'.
Re: AdvNotify X 2
by hossman (Prior) on Jul 10, 2003 at 05:57 UTC

    I've never heard of Win32::AdvNotify, And at first glance i can't find any docs or it (since I can't find it on cpan) but based on this page i found on google it looks like you might want also want to print the event's "Action" ... i'm not sure what the actions will say, but it seems like it might be worth looking at.

    I suspect that what is happening is that since you are monitoring an entire subtree, when you modify a file, you are getting an event for each of the directories above that as well (LAST_WRITE triggers an event for "Change in the modification date of a file or files")

Re: AdvNotify X 2
by Foggy Bottoms (Monk) on Aug 11, 2003 at 09:17 UTC
    Actually AdvNotify is so powerful and so well-written that it notifies for every little bit changed, ie if you rename a file it interprets it as a file rename and a folder change (the one that includes the file if I remember well...) hence you get several events back in return but it doesn't really matter - all you need to do is to write up another layer on top that'll help you identify what exactly happened. I'm on the verge of writing it myself and will be more than glad to send you a copy of my code ASAP... Let me know if you're interested...

    Heureux qui, comme Ulysse, a fait un beau voyage
    Ou comme celui-là qui conquit la Toison,
    Et puis est retourné plein d'usage et raison,
    Vivre entre ses parents le reste de son âge!

    J. du Bellay, poète angevin