Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Watching a driectory for change

by Anonymous Monk
on Jun 13, 2002 at 12:35 UTC ( #174164=perlquestion: print w/replies, xml ) Need Help??

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

Is it possible with Perl on Windows to monitor a directory, and when a file is put in that directory, do something with it? I am looking for a way to process the files as they appear in a directory, rather than check at certain time intervals (which is the way I do it now). Any suggestions where to get started would be helpful.

Replies are listed 'Best First'.
Re: Watching a directory for change
by particle (Vicar) on Jun 13, 2002 at 12:45 UTC
    you're looking for the Win32::ChangeNotify module. from the doc:

    NAME 
    Win32::ChangeNotify - Monitor events related to files and directories
    
    
    
    --------------------------------------------------------------------------------
    
    SYNOPSIS 
    	require Win32::ChangeNotify;
    
    
    	$notify = Win32::ChangeNotify->new($Path,$WatchSubTree,$Events);
    	$notify->wait or warn "Something failed: $!\n";
    	# There has been a change.
    
    
    
    
    --------------------------------------------------------------------------------
    
    DESCRIPTION 
    This module allows the user to use a Win32 change notification event object from 
    Perl. This allows the Perl program to monitor events relating to files and 
    directory trees. 
    
    The wait method and wait_all & wait_any functions are inherited from the 
    "Win32::IPC" module. 
    

    ~Particle *accelerates*

(wil) Re: Watching a driectory for change
by wil (Priest) on Jun 13, 2002 at 12:50 UTC
    How are you doing this at the moment? A code sample would help. Are you checking the directory manually to see if anything has changed? Or are you using a sript to poll the directory and compare it's contents with a cached list?

    It would also help to know how files are being 'put' in that directory? Do they get put in there by users, or are they auto-generated by scripts? If they're generated then maybe a quick hack to the generator-script could issue you with a notification.

    There is one module that I know of, available at CPAN, that might be of use to you here. Look into the Win32::ChangeNotify module which allows you to monitor events relating to files and directory trees.

    Try running it with the FILE_NAME flag set to only monitor creating/deleting/renaming of files in a given directory.

    Hope this helps

    - wil
Re: Watching a driectory for change
by Snuggle (Friar) on Jun 13, 2002 at 14:04 UTC
    This is an interesting problem, and some people have already mentioned Win32::ChangeModify. I am wondering if there is a module for *nix that does the same thing, or is there a way to query the operating system to have the same effect?

    Anyway, no drug, not even alcohol, causes the fundamental ills of society. If we're looking for the source of our troubles, we shouldn't test people for drugs, we should test them for stupidity, ignorance, greed and love of power.

    --P. J. O'Rourke
      I just noticed last night that there's a daemon in RH 7.3 that does file change notifications called sgi_fam. I haven't looked in to it, just saw it when I was cleaning up an install.

      Spring: Forces, Coiled Again!
      There's a kqueue facility in FreeBSD which could be used for such a task. Unfortunately, I see only a python interface wrapper for it :(
Re: Watching a driectory for change
by Anonymous Monk on Jun 13, 2002 at 13:56 UTC
    Thanks a lot guys, changenotify is great. How I was doing it at the moment was running a script that would check the directory to see if there was any content every 5 minutes and then take the files. The files were being put there by ftp. Thanks again, I think I am going to play with changenotify for a while
      If you actually want to get just the files that have changed, look into Win32::AdvNotify

      However be aware that it needs a console window to operate correctly.

      Regards

      Cat

Re: Watching a driectory for change
by ismail (Acolyte) on Jun 13, 2002 at 18:13 UTC
    Rolling your own is a nice exercise and all, but if you're on a production machine it's best to leave it to the pro's. Check out tripwire.org, which is an open-source project dedicated to this very task (with a versionable file system for backing up a file when it does become modified, etc). At the very least, it will give you good ideas on the various cases you will encounter. Good luck.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://174164]
Approved by claree0
Front-paged by wil
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2023-01-28 13:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?