Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Get newest file

by Rich36 (Chaplain)
on Oct 27, 2002 at 20:09 UTC ( #208372=note: print w/replies, xml ) Need Help??


in reply to Get newest file

You can use the module File::Find to traverse the directory structure and use stat to find the age of the file. The example below sets up a hash data structure that contains the name of the file and the last accessed modified time. The application then compares the values in the hash and prints out the newest file.

#!/usr/bin/perl use strict; use File::Find; use vars qw/%files/; sub findNewestFiles { my $element = $File::Find::name; return if (!-f $element); $files{$element} = (stat($element))[9]; } ####################################################### # MAIN ####################################################### my $dir = '/home/users/rich36'; find(\&findNewestFiles, $dir); my $newestfile; my $time = 0; while(my ($k, $v) = each(%files)) { if ($v > $time) { $newestfile = $k; $time = $v; } } $time = localtime($time); print "The newest file is $newestfile : $time\n"; exit;

«Rich36»

Replies are listed 'Best First'.
Re: Re: Get newest file
by Rich36 (Chaplain) on Oct 27, 2002 at 20:31 UTC

    Actually, after considering Zaxo and tadman's responses, you might be better off altering the code above to find the directory with the most recent modified time (Zaxo's suggestion), then use glob on that directory (tadman's suggestion) to find the most recent file. That might be more efficient given the number of files and directories that you are searching through because you would only be storing directories in the hash and the application would not be calling stat on all those files.


    «Rich36»
      Half of the files are in two directories, with more than 200000 each. With going for the directory with the newest modified time, that would save me from checking more than 65% of the total...
      as a work-around, I could just ask the user about the version he installed...
      AFAIK the freedb has no other method included to check the db-version...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://208372]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2021-04-14 19:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?