Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Script to find and delete files

by Delise1 (Initiate)
on Mar 03, 2011 at 16:40 UTC ( #891261=perlquestion: print w/replies, xml ) Need Help??
Delise1 has asked for the wisdom of the Perl Monks concerning the following question:

I need a script to find files formatted like (0055192_4-0055192-02_1217101849.para) that are older than 365 days and if an earlier occurrence of that file beginning with (0055192) exists do not delete and if does not, then delete file. I am looking for these file in a directory structure that has sub-directories. Appreciate the help...I am new to perl

Replies are listed 'Best First'.
Re: Script to find and delete files
by marto (Bishop) on Mar 03, 2011 at 16:44 UTC

    See the File::Find module, I believe the doucmentation contains an example which is very similar to your requirements.

Re: Script to find and delete files
by davido (Archbishop) on Mar 03, 2011 at 19:39 UTC

    File::Find will help you to find the files themselves based on criteria you define. That's already been suggested. I wanted to add one sanity-check piece of advice:

    Whatever you do, make sure that while you're in your testing phases the only action you take is to print a list of delete candidates, or to generate a log file of delete candidates. That way you will have every opportunity to check to make sure that the correct files are being identified before you release dogs. I imagine many a costly mistake has been made by writing a script to act in some irrevocable way on a large data set only to find that it acted in an unintended way.

    Now back to your regularly scheduled programming....


Re: Script to find and delete files
by JavaFan (Canon) on Mar 03, 2011 at 17:18 UTC
    What OS? Note that Unix file systems do not have a concept of creation time, so the question "is this file older than 365 days" isn't answerable.
Re: Script to find and delete files
by eff_i_g (Curate) on Mar 03, 2011 at 19:46 UTC
    I would do something like this:
    use warnings; use strict; use DateTime; use File::Find::Rule; my $time_zone = DateTime::TimeZone->new(name => 'local'); my $a_year_ago = DateTime->now(time_zone => $time_zone)->subtract(year +s => 1); File::Find::Rule->file ->name(qr/\A\d{7}_\d-\d{7}-\d{2}_\d{10}\.para\z/) ->mtime('<' . $a_year_ago->epoch) ->exec( sub { ### Your logic. } )->in(qw( /paths /to /search ));
Re: Script to find and delete files
by sundialsvc4 (Abbot) on Mar 03, 2011 at 20:49 UTC

    One good tip, at least for Windows boxes, is that you should accumulate a list of candidates that you want to delete, and only then go back and delete them.   In my experience, directory-searches are rather easily disrupted by other file-operations in Windows.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://891261]
Approved by marto
[stevieb]: Nick... can you include a comma-separated list to the operation, or a wildcard?

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2017-07-22 19:10 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (340 votes). Check out past polls.