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

Monks, In an attempt to monitor a filesystem, what would be the best way to monitor an NT filesystem and denote changes to a log file? Below is a stab.. Thanks
use Win32::ChangeNotify; $SCRIPT_PATH= "c:\\scripts"; $SCRIPT = "c:\\scripts\scripts_file"; Win32::ChangeNotify::FindFirst( $Monitor, $SCRIPT_PATH, 1, FILE_NOTIFY_CHANGE_ATTRIBUTES || FILE_NOTIFY_CHANGE_SIZE) || die "Cannot create a change notification object. Error:" . Win32::FormatMessage( Win32::GetLastError() ) . "\n"; print"\n"; open(SFILE,">>$SCRIPT") || die "Cannot open includes for writing:$!"; while( $Monitor->FindNext() ) { print "Monitoring noted filesystems....\n"; $Monitor->Wait( INFINITE ); print "\t A change to $SCRIPT_PATH\n"; } $Monitor->Close(); close(SFILE);

Replies are listed 'Best First'.
Re: Monitoring filesystem
by $code or die (Deacon) on Feb 22, 2001 at 02:30 UTC
Re: Monitoring filesystem
by enoch (Chaplain) on Feb 22, 2001 at 02:36 UTC
    That works. The only comment I have is on:
    while( $Monitor->FindNext() )
    I looked at the documentation for the FindNext() call, but it did not specify a return value. So, I am not too sure about what it returns.
    ...
    Okay, I just looked at the ChangeNotify.pm, the sub FindNext goes something like:
    sub FindNext { &reset }
    So, I would imagine your while will run just fine. But, to be on the safe side, I'd do an explicit infinite loop.
    while( 1 ) { print "Monitoring noted filesystems....\n"; $Monitor->FindNext(); $Monitor->Wait( INFINITE ); print "\t A change to $SCRIPT_PATH\n"; }
    But, that is just being nit-picky. Looks good to me.

    Jeremy