Using File::Find to delete files and directories older than 30 days

S

SteveO

I am a Perl beginner and I hve writen this script to remove files older
than 30 days, it works well EXCEPT that it leave the empty directories
behind, can anyone help me look for empty directories and remove them
as well?
TIA,
Steve

#! Perl -w
use Strict;
use File::Find;
$tempdir = "D:\\shared dirs\\temp Public";
find(\&Wanted, $tempdir);
sub Wanted
{
#Do not scan Purchasing or Budget
return $File::Find::prune = 1 if $_ eq "Purchasing - Do Not Remove";
return $File::Find::prune = 1 if $_ eq "BUDGET";
# only on files older than 30 days
if ( ( -M $_ ) > 30 ) {
@args = ("del", "/F", "/Q", "$_");
system @args;
}
}
 
T

Tad McClellan

SteveO said:
I am a Perl beginner


Have you seen the Posting Guidelines that are posted here frequently?

to remove files older
than 30 days, it works well EXCEPT that it leave the empty directories
behind,
#! Perl -w
use Strict;
use File::Find;
$tempdir = "D:\\shared dirs\\temp Public";


That should fail to compile under strictures, you did not declare $tempdir.

use strict;

Case matters.

use warnings;

is better than the -w switch


If you use single quotes, then you won't have backslash the backslashes:

my $tempdir = 'D:\shared dirs\temp Public';

Or, better yet, use sensible slashes instead of silly slashes:

my $tempdir = 'D:/shared dirs/temp Public';

find(\&Wanted, $tempdir);


You don't need a temporary variable at all:

find(\&Wanted, 'D:/shared dirs/temp Public');

sub Wanted


Naming a subroutine that deletes files "wanted" is misleading,
seems like "unwanted" would be more accurate.

@args = ("del", "/F", "/Q", "$_");
^^^
^^^

perldoc -q vars

What's wrong with always quoting "$vars"?

system @args;


You can delete files and directories in native Perl:

perldoc -f unlink

perldoc -f rmdir


You need File::Find::finddepth instead of File::Find::find,
so that you can process the empty directory after removing
all of its files and subdirs.

So, something like this should get you started:

-------------------------
#!/usr/bin/perl
use warnings;
use strict;
use File::Find;

finddepth \&unwanted, 'playpen';

sub unwanted {
if ( -f ) {
unlink $_ or die "could not unlink '$File::Find::name' $!";
}
elsif ( -d ) {
rmdir $_ or die "could not rmdir '$File::Find::name' $!";
}
else {
warn "$File::Find::name is neither a plain file nor a directory\n";
}
}
 
K

Keith Weller

Have you seen the Posting Guidelines that are posted here frequently?

Posting guidelines? This is not a moderated group. People can post
as they please, although top-posting is certainly discouraged. But
you hardly need Posting Guidelines to communicate that.

You can take your posting guidelines, you anal little hamster, and
shove them where the sun don't shine. The only posting guidelines
anyone needs is in the man page or help file for their newsreader.

cordially, even to filth,

rm
 
J

Josef Moellers

Keith said:
Posting guidelines? This is not a moderated group. People can post
as they please,

Well, I for sure like posting guidelines, as they certainly could help
prevent the following:
You can take your posting guidelines, you anal little hamster, and
shove them where the sun don't shine. The only posting guidelines
anyone needs is in the man page or help file for their newsreader.

Feel free to post as you like.
However, If someone posts a question, (s)he might try to post this
question in a manner that might make this a successfull quest.
The posting guidelines are no law, they are _guidelines_ that help a
poster to get the most out of the newsgroup.
Nothing more.
Again, feel free to insult, but expect little help in that case.
 
A

A. Sinan Unur

....

Again, feel free to insult, but expect little help in that case.

I just wanted to make sure everyone realizes that these posts have fake
From: lines, and, in this particular case, it wasn't Keith Keller who
posted the complaint about the posting guidelines.

See also other recent posts which are made to look like they are coming
from Tad.

Sinan
 
P

Paul Lalli

Tad said:
That should fail to compile under strictures, you did not declare $tempdir.

And it would, if the OP was not using a broken operating system.
Windows will not complain about the lack of a Strict.pm file, as it
considers Strict.pm to be the same as strict.pm. It will even require
the code from strict.pm. But it will not carry out any of the
instructions in the strict pragma.
use strict;

Case matters.

Indeed.

Paul Lalli
 
R

Ronald Matthews

Feel free to post as you like.
Fine.

However, If someone posts a question, (s)he might try to post this
question in a manner that might make this a successfull quest.

If they post and someone has an answer to give, then they are free
to give it. Those who demand compliance with your "guidelines" have
no information to give that is of any value.
The posting guidelines are no law, they are _guidelines_ that help a
poster to get the most out of the newsgroup.

No. The _guidelines_ are an exercise in anal-retention demanded by
a tiny little clique in this forum who thinks they have the right to
tell others what to do.
Nothing more.

Nothing more.
Again, feel free to insult, but expect little help in that case.

Who's asking? And if I do ask, I won't be expecting any from you,
ok?

From what I understand, zoloft might help you out.

cordially, as always,

rm
 
A

A Sinan Unur

A. Sinan Unur said:
I just wanted to make sure everyone realizes that these posts have
fake From: lines, and, in this particular case, it wasn't Keith
Keller who posted the complaint about the posting guidelines.

No, it was Keith Weller. Having reading problems? If you hadn't
deleted the attribution line, you could have seen that for yourself.

Duh.
See also other recent posts which are made to look like they are
coming from Tad.

What about posts that look like they are coming from you?

If you're a "friend" of Keller's and McLellan's, then you're not
worth a response, are you?

Please put yourself in a killfile so I won't be able to read your
remarks.

Thanks, troll.

cordially, as always,

rm
 
J

Josef Moellers

A said:
No, it was Keith Weller. Having reading problems? If you hadn't
deleted the attribution line, you could have seen that for yourself.

Duh.




What about posts that look like they are coming from you?

What posts "look like they are coming from" Sinan?
There are posts that have a fake "From:" line in the header, where the
Sender's identity closely resembles Sinan's, but the content (and that's
what matters here) can be easily distinguished from a real post by Sinan.

I assume that you deliberately mis-quote Sinan's and other respected
persons' names as you might get into legal trouble by assuming their
identities. Here in Germany, we call that "Rufmord" and it's a federal
offence.
 
S

SteveO

Thanks Tad, I think. Didn't realize that asking a relatively simple
question would result in such a fuss.

Thanks Again,
Steve
 
S

SteveO

What is the "I think" qualifier for?
The I think qualifier was because I hadn't read the posting guidelines
yet and wasn't sure if you were slamming me. And due to volumous
replies off-topic.
Did what I posted help you?
Yes it helped. Although I already hacked around it using a separate
script to accomplish the directory purge. I will go back and clean up
when I get the chance.

Steve
 
T

Tad McClellan

[ attribution missing, please compose followups properly ]
The I think qualifier was because I hadn't read the posting guidelines
yet


That does not make my followup unworthy of thanks.

I just figured you'd want to know how best to get answers
to your Perl questions, so I mentioned those tips.

and wasn't sure if you were slamming me.


If I felt that slamming was called for, then I would have also
felt that helping to answer the question was uncalled for.

You can tell it wasn't a slam, because my post didn't stop right there. :)

And due to volumous
replies off-topic.


None of those were me, so the "I think" should not attach to me.

The thread was hijacked by a troll. Don't pay serious attention
to its hub-bub, that would be giving it what it wants.

Yes it helped.


All is well then.

Happy Perling!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top