Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: search folder and delete certain files with perl

by TJPride (Pilgrim)
on Dec 07, 2011 at 21:01 UTC ( #942326=note: print w/ replies, xml ) Need Help??


in reply to search folder and delete certain files with perl

Why bother with a module when stat is available?

use strict; use warnings; my ($path, $cutoff, $handle, $file, $c); $path = '/var/opt/xim/syslogs/'; $cutoff = time() - 3600*12; ### 12 hours ago $c = 0; ### Fail if you don't have read permimssions opendir ($handle, $path) || die; while ($file = readdir($handle)) { ### File name must start with pattern next if $file !~ /^Syslogd/; ### Check modified time in seconds since epoch next if (stat $file)[9] > $cutoff; ### Error if you don't have write permissions if (unlink "$path$file") { print "$file removed.\n"; $c++; } else { print "$file could not be removed.\n"; } } print "$c total files removed.\n";


Comment on Re: search folder and delete certain files with perl
Download Code
Re^2: search folder and delete certain files with perl
by CountZero (Bishop) on Dec 07, 2011 at 21:26 UTC
    Because it is so much more compact:
    use Modern::Perl; use File::Find::Rule; my $directory = '/var/opt/xim/syslogs/'; my $age = time - 12*60*60; unlink for File::Find::Rule->file() ->name('Syslogd*') ->mtime("<$age") ->maxdepth( 1 ) ->in($directory);

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      Maybe slightly, I could have compacted my example quite a bit. But I personally prefer doing at least simple things on my own rather than relying on a black box that may or may not work as intended. Maybe this is an extremely reliable module that everyone uses, but how do I know?
        You can never be certain, but verily I say, I have made more errors in home brewn code than in code I loaded from CPAN.

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        Why bother with a module when stat is available?

        Also because CountZero's example is more legible which makes it more elegant and more maintainable. I have to focus my attention more to read your code, and I would have to consult perldoc -f stat if I had any trouble with remembering what the hell falls at [9]; which I would. Positional arguments/values are good for computers but my human brain tunes out after two of them.

        Maybe this is an extremely reliable module that everyone uses, but how do I know?

        Try it and exercise good judgement. Read the numerous five star reviews of it on CPAN ratings. See how often it's been used in answers here by beautiful and talented monkses.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://942326]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (9)
As of 2014-08-01 06:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (257 votes), past polls