On Wed, Apr 23, 2008 at 11:58 PM, Siegfried Heintze (Aditi)
<[EMAIL PROTECTED]> wrote:
> I have directory names in the format of "yyyy-mm-dd_hh-mm-ss". Sometimes I 
> want to delete all the non-empty directories that are from last week or 
> earlier. Other times I want to delete all the directories that are over a 
> week old.
>
>  Is there a script out there already that does this?
>
>  Assuming the answer is no, I started writing a script. Unfortunately, the 
> following does not produce an error and does not work.
>
>  $ perl -MFile::Tasks -e 
> '(File::Tasks->new)->remove_dir("2008-03-11_15-42-58/");
>
>  So how would you enumerate the directories, find the ones that are old 
> (according to the above criteria) and delete them and their contents? What 
> packages would you use?
snip

Well, the glob* function combined with the grep** and -d*** functions
get you part way there and the rmtree function from File::Path****
does the rest:

#iterate over all directories that are not empty
for my $dir (grep { -d and <$dir/*> } <*>) {
    #skip any directories that don't match our pattern
    next unless my $date = $dir =~
/^([0-9]{4}-[0-9]{2}-[0-9]{2})_[0-9]{2}-[0-9]{2}-[0-9]{2}$/;
    #blow away directory if it is older than the target date
    rmtree [$dir], 0, 0 if $date lt $target_date;
}

* http://perldoc.perl.org/functions/glob.html
** http://perldoc.perl.org/functions/grep.html
*** http://perldoc.perl.org/functions/-X.html
**** http://perldoc.perl.org/File/Path.html

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to