Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Using File::Find to delete files and directories older than 30 days

Reply
Thread Tools

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

 
 
SteveO
Guest
Posts: n/a
 
      03-02-2006
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:rune = 1 if $_ eq "Purchasing - Do Not Remove";
return $File::Find:rune = 1 if $_ eq "BUDGET";
# only on files older than 30 days
if ( ( -M $_ ) > 30 ) {
@args = ("del", "/F", "/Q", "$_");
system @args;
}
}

 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      03-03-2006
SteveO <> wrote:

> 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";
}
}
-------------------------


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
 
Keith Weller
Guest
Posts: n/a
 
      03-03-2006
Tad McClellan <> trolled:
> SteveO <> wrote:


> > I am a Perl beginner


> 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
 
Reply With Quote
 
Josef Moellers
Guest
Posts: n/a
 
      03-03-2006
Keith Weller wrote:
> Tad McClellan <> trolled:
>
>>SteveO <> wrote:

>
>
>>>I am a Perl beginner

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


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.

--
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett

 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      03-03-2006
Josef Moellers <> wrote in news:du9dek
$k93$:

> Keith Weller wrote:
>> Tad McClellan <> trolled:
>>
>>>SteveO <> wrote:

>>
>>
>>>>I am a Perl beginner

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

>>
>>
>> Posting guidelines? This is not a moderated group.


....

> 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

--
A. Sinan Unur <>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html

 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      03-03-2006
Tad McClellan wrote:
> SteveO <> wrote:
> > #! 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.


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

 
Reply With Quote
 
Ronald Matthews
Guest
Posts: n/a
 
      03-03-2006
Josef Moellers <> trolled:

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


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
 
Reply With Quote
 
A Sinan Unur
Guest
Posts: n/a
 
      03-03-2006
A. Sinan Unur <> trolled:

> 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
 
Reply With Quote
 
Josef Moellers
Guest
Posts: n/a
 
      03-06-2006
A Sinan Unur wrote:
> A. Sinan Unur <> trolled:
>
>
>>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?


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.

--
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett

 
Reply With Quote
 
SteveO
Guest
Posts: n/a
 
      03-06-2006
Thanks Tad, I think. Didn't realize that asking a relatively simple
question would result in such a fuss.

Thanks Again,
Steve

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete files from FTP Server older then 7 days. Using ftputil andftplib. pilgrim773 Python 2 05-19-2010 06:41 PM
trouble with this code in deleting files older than 60 days George Lewycky Perl Misc 4 07-25-2005 12:45 PM
Getting all directories/files from current directory and using -d flag for the directories Adam Petrie Perl Misc 8 10-11-2004 01:28 PM
Delete all files older than 1 day in directory Andrew Thompson Perl Misc 4 01-13-2004 01:33 AM
Newby Question: Remove files older than 7 days from a directory kbass Python 2 11-13-2003 07:52 PM



Advertisments