Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Win32::ChangeNotify question

by softworkz (Monk)
on Nov 11, 2003 at 13:02 UTC ( #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

Comment on Win32::ChangeNotify question
Re: ChangeNotify question
by Anonymous Monk on Nov 11, 2003 at 13:25 UTC
    There is no module called "ChangeNotify"
      I stand corrected
        That is Win32::ChangeNotify, not ChangeNotify.
        No there isn't. That code is clearly Win32::ChangeNotify; that's not the same as ChangeNotify, which happens to not exist. Posting the code is also completely unnecessary - it cluttered the node and served no purpose. We all know where CPAN is.

        Iif you're trying to "get the LAST_WRITE time of a file," then this may not be the right module for you. You use this module to check for changes to a file or group of files. You can't use this to just get a file's last modified time - you want stat for that. If you really do need this module, then you use it to set up a watcher that will get events when a file's last modified time changes. If you need to get the value of that time after it's been changed, you use stat.

        Are you sure it was a book? Are you sure it wasn't.....nothing?
Re: ChangeNotify question
by BrowserUk (Pope) 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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2014-12-23 00:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (133 votes), past polls