Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: monitoring directory for new subdirectories

by graff (Chancellor)
on Mar 28, 2009 at 15:53 UTC ( [id://753865]=note: print w/replies, xml ) Need Help??


in reply to monitoring directory for new subdirectories

If the plan is to run a script at some interval (e.g. via your crontab), and you just want to monitor a single directory (not recursively watching all existing subdirectories), something like this would be pretty quick:
use Croak qw/croak/; # (update; don't forget this line, if making a m +odule) sub check_new_paths { my ( $chkpath, $reflist ) = @_; # path to monitor, list of prev. + contents my %old_list; if ( -f $reflist ) { open( LST, "<", $reflist ) or croak( "Can't read $reflist: $!" + ); %old_list = map { chomp; $_ => undef } <LST>; close LST; } if ( ! -d $chkpath ) { croak( "$chkpath is not a directory" ); } my @new_list = grep { -d } <$chkpath/*>; # NB: file glob does not catch "$chkpath/.any-dot-initial-dirnam +e" my @added_list; if ( keys %old_list ) { for my $p ( sort @new_list ) { push @added_list, $p unless exists( $old_list{$p} ); } } else { warn sprintf( "Creating new list %s with %d items\n", $reflist, scalar @new_list ); } open( LST, ">", $reflist ) or croak( "Can't write $reflist: $!" ); print LST "$_\n" for ( @new_list ); close LST; return @added_list; }
If you need to recursively check subdirs within the target path, you'll want to check out one of the various "File::Find" modules -- or just do something like:
my @new_list = split /\x0/, `find $chkpath -type d -print0`;
(which will also give you subdir names that begin with "."). -- Update: I'm sure you can get File::Glob to list dot-initial file names, too, but "that is left as an exercise..." ;)

But if you can get Linux::Inotify to work (shouldn't be too hard), it'll probably include better, more general tools that you'll want to use.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (2)
As of 2026-01-18 17:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (122 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.