Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Finding Oldest File in a Directory

by sk (Curate)
on Oct 04, 2005 at 21:09 UTC ( #497409=note: print w/ replies, xml ) Need Help??


in reply to Finding Oldest File in a Directory

Update: Please note that this solution searches for the files recursively. Thanks to aristotle for the clarification

Use File::Find

Untested

#!/usr/bin/perl -w use File::Find; use strict; my $dir = '/path/to/dir/'; my $oldestfile = 0; my $filename = "No BOT REPORT: ERROR"; finddepth(\&oldfile, $dir); print "oldest file = " , $filename,$/; sub oldfile { return unless (/BOT/); my $mtime = -M $_; if ($mtime > $oldestfile) { $filename = $File::Find::name; $oldestfile = $mtime; } }

BTW i think you are not getting the files using the readdir due to the context

from perldoc -f readdir

readdir DIRHANDLE Returns the next directory entry for a directory opened by "opendir". +If used in list context, returns all the rest of the entries in the d +irectory. If there are no more entries, returns an undefined value i +n scalar context or a null list in list context.


Comment on Re: Finding Oldest File in a Directory
Select or Download Code
Re^2: Finding Oldest File in a Directory
by Aristotle (Chancellor) on Oct 08, 2005 at 19:21 UTC

    He didn’t say he wants to recurse, nor does his example code indicate anything like that. Why are you using File::Find?

    Makeshifts last the longest.

      It has been a while since i posted this - I think i looked at the OP's readdir() and somehow felt he is doing some kind of recursion as for just checking the oldest file in a directory can be accomplished using glob or just use the Plain Old Shell and call the script with a * (check for -d etc.) or ls -lrt

      That said, I agree File::Find might not be the best way to solve this problem but it can be used to achieve what the OP wants as GrandFather has shown or just eval finddepth and die on finding a directory as finddepth works bottoms-up.

      I just find it much easier to use File::Find and it is very easy to expand (if recursion is ever required).

      I am not worried too much about performance in this case even if it is 4X times slower. How many times do i have to remove the oldest file? Am i working with 1000s of files? etc. The OP has not mentioned anything about perormance so i am presuming that arg is not very important.

      I definitely DO NOT disagree with your comment(s). It is my mistake not noticing that OP did not ask for recursion, I will update my node saying it does recursion and it is upto the OP to choose it or not.

      cheers

      SK

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (10)
As of 2014-07-29 10:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (214 votes), past polls