Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Listing old files

by Ratazong (Monsignor)
on May 09, 2011 at 06:16 UTC ( #903705=note: print w/replies, xml ) Need Help??


in reply to Listing old files

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.

Replies are listed 'Best First'.
Re^2: Listing old files
by Anonymous Monk on May 09, 2011 at 09:41 UTC
    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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2018-07-22 16:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (454 votes). Check out past polls.

    Notices?