Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I know there has to be a simple way to do what I'm trying to accomplish but I haven't found it yet. I'm trying to take user input variables and use them to create an array of files based on the input. EX. User enters Month: 04 Day: 01 Year: 2002 then take those and find files with creation dates of 4/1/2002. I've tried using stat with -mtime but that returns all files with the date and newer than the selected. I just need the exact date. Thanks.

Replies are listed 'Best First'.
•Re: File Creation Dates
by merlyn (Sage) on Apr 11, 2002 at 14:57 UTC
    Well, you didn't say a platform, but if you're on Unix, no matter how you look at the date you're out of luck, because Unix doesn't maintain the "creation time" of files, regardless of some of the docs that erroneously say so. The ctime value is the "last inode change time", not creation time.

    -- Randal L. Schwartz, Perl hacker

Re: File Creation Dates
by Kanji (Parson) on Apr 11, 2002 at 15:37 UTC

    As merlyn notes, you can't get a files' creation date on Unix (and possibly other platforms), but often using a file's last modification date instead will work in many cases.

    If that's an acceptable alternative, then you can do something like...

    use POSIX qw/ strftime /; sub mdate { my($file,$format) = @_; # Unless otherwise specified, use # MM/DD/YYYY as date format. $format ||= '%m/%d/%Y'; my $date = ( stat($file) )[9]; return strftime( $format => localtime $date ); } my $dir = '/path/to/files'; my $date = '04/11/2002'; chdir $dir or die "Cannot chdir($dir): $!\n"; opendir DIR, '.' or die "Cannot opendir($dir): $!\n"; foreach my $file ( readdir DIR ) { print $file, "\n" if mdate($file) eq $date; }

        --k.


Re: File Creation Dates
by Fletch (Bishop) on Apr 11, 2002 at 14:50 UTC

    You need to figure out (probably using Date::Parse or the like) the epoch seconds for 0000 to 2359 on that date, then look for files where stat returns a value between them.