Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Listing old files

by Anonymous Monk
on May 09, 2011 at 06:00 UTC ( [id://903701]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have this code to find the old files but what i need is to list the files at each directory level. Can you please guide me in getting this done.
use File::Find; @ARGV = ('.') unless @ARGV; my ($age, $name); sub oldest { return if defined $age && $age < (stat($_))[9]; $age = (stat(_))[9]; $name = $File::Find::name; } find(\&oldest, @ARGV); print "$name " . scalar(localtime($age)) . "\n";

Replies are listed 'Best First'.
Re: Listing old files
by Ratazong (Monsignor) on May 09, 2011 at 06:16 UTC

    Hi

    Do you know what your code does?

    find(\&oldest, @ARGV);
    Here you use File::find to go throgh a directory-structure - and call the function oldest on each file found.
    sub oldest { return if defined $age && $age < (stat($_))[9]; $age = (stat($_))[9]; $name = $File::Find::name; }
    In the function oldest you do some check on the file. As you see, the name of the file is passed to the function as $File::Find::name. (also in $_ ... but lets ignore that for now)

    So all you need to do is to replace the code inside the function oldest by some code that prints the filename instaed. I'm sure you can do this without any further hint ... :-)

    Rata

    update reading your question again, I am wondering if you want to find the oldest file in each subdirectory. For that, I would propose the following approach: replace your scalar variables $age and $name by a hashes. And use the directory-name as the hash-key. You will find the directory-name in $File::Find::dir.

      Yes what i need is to list oldest file in each sub directory and i tried as per your suggestion using hash but this is listing for all the files instead of oldest file
      $data{$File::Find::name} = scalar(localtime($age));

        not quite:

        The idea is to create one age and one name for each subdirectory. This grouping can be done by a hash. However you should use the "grouping-criteria" as hash-key. In your case it is $File::Find::dir. The rest of your logic can remain unchanged.

        use strict; use warnings; use File::Find; @ARGV = ('.') unless @ARGV; my %age; my %name; sub oldest { return if ((defined $age{$File::Find::dir}) && ($age{$File::Find::di +r} < (stat($_))[9])); $age{$File::Find::dir} = (stat(_))[9]; $name{$File::Find::dir} = $File::Find::name; } find(\&oldest, @ARGV); foreach my $n (keys(%age)) { print $name{$n}." " . scalar(localtime($age{$n})) . "\n"; }

        btw.: I'm unsure whether this is a homework-question or not ... if it is be sure that you totally understand the solution. I have seen many situations where a student couldn't explain some code supposedly written by himself. You don't want that happen to you! ;-)

        Rata
Re: Listing old files
by happy.barney (Friar) on May 09, 2011 at 07:17 UTC
    If I understood your question, you want to replace $age with $age->{$File::Find::dir}
Re: Listing old files
by anonymized user 468275 (Curate) on May 09, 2011 at 14:23 UTC
    Taking the requirement as stated literally (not using File::Find - it didn't earn its bread here)
    dir ( @ARGV ); sub dir { local $_; for my $d ( @_ ){ local $_; print "\nDirectory listing of $d:\n\n"; my @file = glob "$d/*"; for my $f ( @file ) { print $f . "\n"; } for my $f ( grep (( -d $_ ), @file )) { dir( $f ); } } }

    One world, one people

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-04-19 18:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found