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

Re: Finding oldest file in directory

by ikegami (Pope)
on Oct 18, 2004 at 21:08 UTC ( #400321=note: print w/ replies, xml ) Need Help??


in reply to Finding oldest file in directory

You mentioned portability was a requirement, so you should use File::Spec to build paths. "/" is not the file seperator on Macs, for example.

I have a script that needs to determine the oldest file in a particular directory

If all you're concerned about is which file is the oldest, there's no need to sort:

sub get_oldest { my ($dir) = @_; my $oldest; my $oldest_time; my $file_spec = File::Spec->catfile($dir, '*.pl'); foreach (glob $file_spec") { my $time = (stat $_)[10]; if (!$oldest_time || $time < $oldest_time) { $oldest = $_; $oldest_time = $time; } } return $oldest; }

I don't know how efficient glob is. You can get rid of it:

use DirHandle (); use File::Spec (); sub get_oldest { my ($dir) = @_; my $oldest; my $oldest_time; my $dh = DirHandle->new($dir); while (defined($_ = $dh->read())) { next unless (/\.pl$/i); my $full_path = File::Spec->catfile($dir, $_); my $time = (stat $full_path)[10]; if (!$oldest_time || $time < $oldest_time) { $oldest = $_; $oldest_time = $time; } } return $oldest; }


Comment on Re: Finding oldest file in directory
Select or Download Code
Re^2: Finding oldest file in directory
by mattr (Curate) on Oct 20, 2004 at 15:05 UTC
    Hi, I'm lazy but since I wrote a program called filexer to sync uploads I'll just make a couple of nitpicking suggestions.

    If you are dealing with remote mounting of windows shares do a lot of testing. In particular permissions and you-can't-get-there-from-here took a lot of my time.

    I used a cygwin binary at one point to solve a problem windows wasn't helping me with. It was a while ago and I don't have the code online right now, but I'm pretty sure I used cygwin's touch command.

    Granularity < 1 second might flub it.

    Illegal characters esp. colons, questionmarks, non-western encodings, filename lengths, etc. if you are actually copying across net like I did. Likewise if so, then security issues possibly.

    maybe loading interpreter and scanning program are going to take a while too. Consider running under mod_perl (even if just under cgi emulation) and calling once evvery 10 seconds via a crontab? I don't want to think of any perl program being launched on my system from scratch every 10 seconds.. that is, can you just keep the thing running all the time instead of quitting it after 10 seconds. Much better then I think.

    Have fun!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2014-07-25 04:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (167 votes), past polls