Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
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 meditating upon the Monastery: (10)
As of 2014-12-22 15:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (119 votes), past polls