Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

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); }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://804757]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2018-01-20 11:29 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (226 votes). Check out past polls.