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

Using ActivePerl 623, I'd like to read one file from a directory, but use the file date to pick that file. Any ideas?
  • Comment on How do I pick one file from a directory based on the file date?

Replies are listed 'Best First'.
Re: How do I pick one file from a directory based on the file date?
by arturo (Vicar) on Apr 24, 2001 at 02:20 UTC

    File *what* date? Creation? Modification? Last Access time? (I'll confess to not recalling which of these the various Windows versions keep)

    Whichever applies to you, have a peek at perldoc -f stat and perldoc -f -X on your own system (or try stat and/ or perlfunc:_X on this site.

    Other functions you may find useful: grep, readdir.

    HTH

Re: How do I pick one file from a directory based on the file date?
by SgtClueLs (Novice) on Apr 25, 2001 at 19:55 UTC
    I ran into the same thing. What I had to do is run a system command for a dir to a txt file. Then open the txt file and look at the date. I also had to format localtime to a MSDOS readable date/time. For Instance I created a perl script to check Backup Exec log files based on date.

    Code to Acquire the NT MS-DOS Readable Date
    # Acquire the Local Time Array ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time +); # Convert less the "10" so they equal a two char date. $Month = $mon + "1"; if ($month < "10"){ $Month = '0' . "$Month"; } if ($mday < "10"){ $mday = '0' . "$mday"; } # Get the last two digits of a year, the way DOS sees file names. $RealYear = substr($year, 1); # This created $TimeStampDate into a format found in a DIR command in +DOS. Ex. 03/28/01. $TimeStampDate = "$Month". "/". "$mday" . "/" . "$RealYear";

    Sub to create the cmd file to run,a nd run it.
    sub CreateCmdFile { $FileName = 'D:\\Perl\\pl\\Check\\' . "$CurrentServerName" . ' +.cmd'; unlink($FileName); open(servercmd, "> $FileName"); print servercmd '@echo off' . "\n"; $VeritasLocation = '"\\\\' . "$CurrentServerName" . '\\C$\\Pro +gram Files\\Veritas\\Backup Exec\\NT\\Data\\*.txt"'; print servercmd 'dir ' . "$VeritasLocation" . ' > D:\\Perl\\pl +\\Check\\' . "$CurrentServerName" . '.txt'; close(servercmd); system("$FileName"); }

    Finally to check the .txt file vs. the date.
    sub FindMatchingDates { $ServerDirectoryFile = 'D:\\Perl\\pl\\Check\\' . "$CurrentServ +erName" . '.txt'; open(logfilelisting, "$ServerDirectoryFile") or die "Unable to + open file: $!"; @CheckForMatchingDate = <logfilelisting>; close(logfilelisting); foreach (@CheckForMatchingDate) { $SubOfCheckForMatchingDate = substr($_, 0, 8); if ($SubOfCheckForMatchingDate eq $TimeStampDate){ $AssignedCorrectLogFile = substr($_, 39); chomp($AssignedCorrectLogFile); push(@CorrectLogFiles, "$AssignedCorrectLogFil +e"); $LogFileFound = "1"; } } }

    So in this case it pushes the Matching File name to @CorrectLogFile which can be used later.
    Sgt