Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Some File::Find woes.

by Tommy (Chaplain)
on Jan 13, 2004 at 00:52 UTC ( [id://320828]=note: print w/replies, xml ) Need Help??


in reply to Some File::Find woes.

You don't need to use File::Find for this at all. Further, you don't need to use absolute paths, and you don't need to convert "/" to "\". Windows understands "/" in file paths for a long time now.

#!/usr/bin/perl -w # this code only partially tested my($dir) = $ARGV[0] ||''; my($maxage) = $ARGV[1] ||0; $maxage = int $maxage; die "Can't operate without a target directory spec. Op aborted.\n" unless length $dir; # dir could be named "0" die qq[No such directory "$dir"\n] unless -e $dir; die "Need max allowed age spec for files. Op aborted.\n" unless $maxage; # zero values and non-numeric values not accepted. local *PDFDIR; opendir(PDFDIR, $dir) or die $!; my(@files) = grep(/\.pdf$/, readdir(PDFDIR)); closedir(PDFDIR) or warn $!; print qq[Nothing to do. No files present in "dir".\n] and exit unless + @files; foreach (@files) { print qq[Skipping directory "$dir/$_"\n] if -d $_; unlink $dir . '/' . $_ if int(-M $dir . '/' . $_) > $maxage or die qq[Can't unlink "$dir/$_"! $!]; print qq[Deleted "$dir/$_"\n]; } print "Done.\n\n" and exit;
--
Tommy Butler, a.k.a. TOMMY

Replies are listed 'Best First'.
Re: Re: Some File::Find woes.
by dominix (Deacon) on Jan 13, 2004 at 11:04 UTC
    if you plan a non-File::Find you'd better make your procedure a recursiv one.
    my($dir) = $ARGV[0] ||''; my($maxage) = $ARGV[1] ||0; $maxage = int $maxage; die "Can't operate without a target directory spec. Op aborted.\n" unless length $dir; # dir could be named "0" die qq[No such directory "$dir"\n] unless -e $dir; die "Need max allowed age spec for files. Op aborted.\n" unless $maxage; # zero values and non-numeric values not accepted. sub gothere { my ($ddir) = @_; $ddir=~ s|$|/|; local *PDFDIR; opendir(PDFDIR, $ddir) or die $!; my(@files) = map {$ddir . $_} readdir(PDFDIR); closedir(PDFDIR) or warn $!; print qq[Nothing to do. No files present in "dir".\n] and exit unl +ess @files; foreach my $f (@files) { next if $f =~ /^\.+$/; #don't want . .. gothere($f) if -d $f; if ((int(-M $f) > $maxage) && (-f _) && $f=~/\.pdf$/i) {unlink $f or die qq[Can't unlink "$f"! $!];} print qq[Deleted "$f"\n]; } } gothere($dir); print "Done.\n\n" and exit;
    warning untested.
    --
    dominix
Re: Some File::Find woes.
by cLive ;-) (Prior) on Jan 13, 2004 at 05:03 UTC

    Adding a note, of course, that you are assuming the directories they want to search do not contain sub-directories that need searching - which, for this many files though, is very likely to be the case.

    .02

    cLive ;-)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-24 02:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found