How to delete old directories with perl script?

How to delete old directories with perl script?

am 24.04.2008 05:58:20 von 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_--

Re: How to delete old directories with perl script?

am 24.04.2008 06:37:48 von 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)
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/

Re: How to delete old directories with perl script?

am 24.04.2008 06:42:10 von chas.owens

On Wed, Apr 23, 2008 at 11:58 PM, Siegfried Heintze (Aditi)
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/

Re: How to delete old directories with perl script?

am 24.04.2008 06:45:46 von chas.owens

On Thu, Apr 24, 2008 at 12:37 AM, Li, Jialin 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/

Re: How to delete old directories with perl script?

am 24.04.2008 08:50:57 von 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/

Re: How to delete old directories with perl script?

am 24.04.2008 18:17:18 von 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/