As others pointed out, it's good form to use YYYYMMDD. But in case you can't:

#!/usr/bin/perl -w use strict; my $DIR = '.'; use File::Find qw( find ); use File::Spec::Functions qw( catfile ); use Time::Local qw( timelocal ); my $todaysfile = catfile($DIR, time_to_filename(time)); if (! -e $todaysfile) { if (my $oldfile = find_latest_file($DIR)) { email_to_a_specific_person($oldfile); } } open FILE, ">>$todaysfile" or die qq{Can't open "$todaysfile": $!}; print FILE "Hello, World!\n"; close FILE; exit 0; sub find_latest_file { my ($dir) = @_; my ($bestfile, $latest) = (undef, 0); find sub { my $time = filename_to_time($_) or return; if ($time > $latest) { ($bestfile, $latest) = ($File::Find::name, $time); } }, $dir; return $bestfile; } sub filename_to_time { my ($filename) = @_; # Adjust if necessary my ($m, $d, $y) = $filename =~ /(\d\d)(\d\d)(\d\d)/ or return; return timelocal(0, 0, 0, $d, $m - 1, $y); } sub time_to_filename { my ($time) = @_; # Adjust if necessary my @localtime = localtime $time; my ($d, $m, $y) = @localtime[3, 4, 5]; my $filename = sprintf("%02d%02d%02d.txt", $m+1, $d, $y%100); return $filename; } sub email_to_a_specific_person { my ($file) = @_; print "Sending $file . . .\n"; }

If you are planning on running more than one copy of the program at once, then this code will sometimes fail, and you will need to investigate file locking.

I should've used strftime() from POSIX and could've used reduce() from Scalar::Util. Note that find_latest_file() runs in O(n), which might make a difference (versus an n log n sort) after a few years.


In reply to Re: Find a file by TilRMan
in thread Find a file by Midnite

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.