Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Win32::ChangeNotify question

by softworkz (Monk)
on Nov 11, 2003 at 13:02 UTC ( [id://306175]=perlquestion: print w/replies, xml ) Need Help??

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

Hello monks.. Can someone help me with the Win32::ChangeNotify module, I don't understand the docs on how to use it... What I mainly don't understand is how to get the LAST_WRITE time of a file. This program will be used strictly on windoze 2000 and above. Thanks

Replies are listed 'Best First'.
Re: ChangeNotify question
by BrowserUk (Patriarch) on Nov 11, 2003 at 14:01 UTC

    First, as pointed out below, the module is called Win32::ChangeNotify.

    Second, ChangeNotify doesn't give you the time of LAST_WRITE. It will inform you when a file or directory or it's attributes, within a subdirectory or subtree changes. You can filter the changes that it notifies you of to only include LAST_WRITE. The notification itself does not tell you which file changed, only that a file changed. It then becomes up to you to discover which one did and act upon it.

    This require you to record the state of that part of the file system you are interested in, within your program, and creating one or more ChangeNotify objects to monitor it, and the calling $ChangeNotifyObj->wait( timeout ); periodically to be informed if someting has changed. You then need to re-scan the directory or subtree in question and detect what changed by comparing it with your stored record.

    Effectively, all the ChangeNotify object does is save you from having to scan the directory until you know that there is a reason to do so.

    A little sample code may help.

    #! perl -slw use strict; use Win32::ChangeNotify; our $PATH ||= '.'; our $S = defined $S ? 1 : 0; my $notify = Win32::ChangeNotify->new( $PATH, $S, 'FILE_NAME' ); my %last; @last{ glob $PATH . '/*' } = (); while( 1 ) { print('Nothing changed'), next unless $notify->wait( 10_000 ); # Check every 10 seconds $notify->reset; print 'Something changed'; my @files = glob $PATH . '/*'; if( @files < scalar keys %last ) { delete @last{ @files }; print 'These files where deleted: '; print for keys %last; } elsif( @files > scalar keys %last ) { my %temp; @temp{ @files } = (); delete @temp{ keys %last }; print 'These files where created: '; print for keys %temp; } else { print "A non-deletion or creation change occured"; } undef %last; @last{ @files } = (); } __END__ P:\test>changeNotify Nothing changed Something changed These files where created: ./junk Nothing changed Nothing changed Terminating on signal SIGINT(2)

    Note: The above sample session, was monitoring the current directory, and I created a new file called junk.

    Note also that as coded, this is looking for 'FILE_NAME' changes... this will inform you of creation, deletion and renamed files in the watched directory. You'll need to read the pod and change the filter argument to 'LAST_WRITE. to detect those. You will also need to record the last write times (see stat and file test operators specifically -M -A & -C for further information on how to obtain this).


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    Hooray!
    Wanted!

      Thanks BrowserUK
Re: ChangeNotify question
by Anonymous Monk on Nov 11, 2003 at 13:25 UTC
    There is no module called "ChangeNotify"
    A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (7)
As of 2024-04-23 12:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found