Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Perl Beginners > Re: How to dele...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 4 of 6 Topic 10983 of 11401
Post > Topic >>

Re: How to delete old directories with perl script?

by chas.owens@[EMAIL PROTECTED] (Chas. Owens) Apr 24, 2008 at 12:42 AM

On Wed, Apr 23, 2008 at 11:58 PM, Siegfried Heintze (Aditi)
<a-siehei@[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 im****tant skill a programmer can have is the ability to read.
 




 6 Posts in Topic:
How to delete old directories with perl script?
a-siehei@[EMAIL PROTECTED  2008-04-23 20:58:20 
Re: How to delete old directories with perl script?
jialinli1981@[EMAIL PROTE  2008-04-23 23:37:48 
Re: How to delete old directories with perl script?
chas.owens@[EMAIL PROTECT  2008-04-24 00:45:46 
Re: How to delete old directories with perl script?
chas.owens@[EMAIL PROTECT  2008-04-24 00:42:10 
Re: How to delete old directories with perl script?
noreply@[EMAIL PROTECTED]  2008-04-24 08:50:57 
Re: How to delete old directories with perl script?
rvtol+news@[EMAIL PROTECT  2008-04-24 18:17:18 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sun Jul 6 6:59:08 CDT 2008.