Bookmarks

Yahoo Gmail Google Facebook Delicious Twitter Reddit Stumpleupon Myspace Digg

Search queries

WWWXXX DACB, wwwxxx,nvif, why atm producer might be held liable for economic injury, wwwxxxy=ServiceLogin, w2ksp4.exe, WwwxxXdbf, procmail "FROM_MAILER" patch, Use of assignment to $[ is deprecated at /usr/local/sbin/apxs line 86. , wwwxxx vim, mysql closing table and opening table

Links

XODOX
Impressum

#1: How to delete old directories with perl script?

Posted on 2008-04-24 05:58:20 by a-siehei

--_000_EB070C90E3B48C4F97C7AC252BACE9E71B45CDF08CNAEXMSGC125 re_
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

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 ear=
lier. 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 f=
ollowing does not produce an error and does not work.

$ perl -MFile::Tasks -e '(File::Tasks->new)->remove_dir("2008-03-11_15-42-5=
8/");

So how would you enumerate the directories, find the ones that are old (acc=
ording to the above criteria) and delete them and their contents? What pack=
ages would you use?
Thanks,
Siegfried





--_000_EB070C90E3B48C4F97C7AC252BACE9E71B45CDF08CNAEXMSGC125 re_--

Report this message

#2: Re: How to delete old directories with perl script?

Posted on 2008-04-24 06:37:48 by jialin

you can use File::Find module to traverse the directory.

I think this module is simulating the unix find command.


On Wed, Apr 23, 2008 at 10:58 PM, Siegfried Heintze (Aditi)
<a-siehei@microsoft.com> 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?
> Thanks,
> Siegfried
>
>
>
>
>

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Report this message

#3: Re: How to delete old directories with perl script?

Posted on 2008-04-24 06:42:10 by chas.owens

On Wed, Apr 23, 2008 at 11:58 PM, Siegfried Heintze (Aditi)
<a-siehei@microsoft.com> 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: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Report this message

#4: Re: How to delete old directories with perl script?

Posted on 2008-04-24 06:45:46 by chas.owens

On Thu, Apr 24, 2008 at 12:37 AM, Li, Jialin <jialinli1981@gmail.com> wrote:
> you can use File::Find module to traverse the directory.
>
> I think this module is simulating the unix find command.
snip

File::Find is a good module, but in this case we are working with
files that are all in the same directory, so it is overkill.
File::Find is a good choice when we want to work with entire
hierarchies of directories.

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

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Report this message

#5: Re: How to delete old directories with perl script?

Posted on 2008-04-24 08:50:57 by Gunnar Hjalmarsson

Siegfried Heintze (Aditi) wrote:
> 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/");

File::Path is probably a better choice than File::Tasks.

perl -MFile::Path -e 'rmtree("2008-03-11_15-42-58")'

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Report this message

#6: Re: How to delete old directories with perl script?

Posted on 2008-04-24 18:17:18 by rvtol+news

"Siegfried Heintze (Aditi)" schreef:

> 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.

Consider IO::All.

--
Affijn, Ruud

"Gewoon is een tijger."

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Report this message