Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Search for oldest file in directories and its subdirectories

by vitoco (Friar)
on Nov 03, 2009 at 19:44 UTC ( #804757=note: print w/ replies, xml ) Need Help??


in reply to Search for oldest file in directories and its subdirectories

I use the following as a starting code for any recursive file task:

#!perl -w use strict; $0 =~ s!^.*[\\/]([^\\/]+)$!$1!; die "Syntax: $0 [file|dir]...\n" unless defined $ARGV[0]; use Time::Local; # just for this example. my @pend = @ARGV; # pending list of files and dirs while (my $d = shift(@pend)) { if (-d $d) { dir($d); } else { file($d); } } exit; sub file { my ($f) = @_; # process a file, for example: my $m = (stat($f))[9]; print "$f\t$m\t" . date($m) . "\n"; } sub dir { my ($d) = @_; opendir(D, $d) || die; while (my $f = readdir(D)) { next if $f =~ /^\.\.?$/; # skip . and .. my $n = "$d/$f"; if (-d $n) { push(@pend, "$n"); # enqueue dir } else { file($n); # process file now!!! } } closedir(D); } sub date { my ($t) = @_; my ($ss,$mm,$hh,$dd,$nn,$yy) = (localtime($t))[0..5]; return sprintf("%04g-%02g-%02g %02g:%02g:%02g", ($yy<70?2000:1900)+$yy, $nn, $dd, $hh, $mm, $ss); }


Comment on Re: Search for oldest file in directories and its subdirectories
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2015-07-03 22:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (57 votes), past polls