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