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

Discussion in 'Perl Misc' started by SteveO, Mar 2, 2006.

  1. SteveO

    SteveO Guest

    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;
    }
    }
    SteveO, Mar 2, 2006
    #1
    1. Advertising

  2. 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
    Tad McClellan, Mar 3, 2006
    #2
    1. Advertising

  3. SteveO

    Keith Weller Guest

    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
    Keith Weller, Mar 3, 2006
    #3
  4. Re: Using File::Find to delete files and directories older than 30days

    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
    Josef Moellers, Mar 3, 2006
    #4
  5. Josef Moellers <> wrote in news:du9dek
    $k93$-siemens.com:

    > 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/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Mar 3, 2006
    #5
  6. SteveO

    Paul Lalli Guest

    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
    Paul Lalli, Mar 3, 2006
    #6
  7. 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
    Ronald Matthews, Mar 3, 2006
    #7
  8. SteveO

    A Sinan Unur Guest

    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
    A Sinan Unur, Mar 3, 2006
    #8
  9. Re: Using File::Find to delete files and directories older than 30days

    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
    Josef Moellers, Mar 6, 2006
    #9
  10. SteveO

    SteveO Guest

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

    Thanks Again,
    Steve
    SteveO, Mar 6, 2006
    #10
  11. SteveO <> wrote:

    > Thanks Tad, I think.



    What is the "I think" qualifier for?

    Did what I posted help you?


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Mar 6, 2006
    #11
  12. SteveO

    SteveO Guest

    >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
    SteveO, Mar 7, 2006
    #12
  13. SteveO <> wrote:

    [ attribution missing, please compose followups properly ]

    >>What is the "I think" qualifier for?


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


    >>Did what I posted help you?


    > Yes it helped.



    All is well then.

    Happy Perling!


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Mar 7, 2006
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kbass
    Replies:
    2
    Views:
    1,757
    Graham Fawcett
    Nov 13, 2003
  2. Replies:
    5
    Views:
    454
    Bo Yang
    Nov 2, 2006
  3. pilgrim773
    Replies:
    2
    Views:
    1,821
    Giampaolo Rodolà
    May 19, 2010
  4. Andrew Thompson

    Delete all files older than 1 day in directory

    Andrew Thompson, Jan 12, 2004, in forum: Perl Misc
    Replies:
    4
    Views:
    243
    Andrew Thompson
    Jan 13, 2004
  5. George Lewycky
    Replies:
    4
    Views:
    232
    Anno Siegel
    Jul 25, 2005
Loading...

Share This Page