CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2004
    Location
    Pell City, Alabama
    Posts
    126

    Perl: Filetime of most recent file ?

    I have a certain directory, and I want to find the most recently modified file in that directory and extract the time that it was last modified in Perl.

    Can you suggest how I can go about doing this ?

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: Perl: Filetime of most recent file ?

    Well, you will have to loop through all of them and compare them. The following code will get you the last modified date.

    Code:
    open(HANDLE, $filename);
    localtime((stat HANDLE)[9]);
    ...
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3

    Re: Perl: Filetime of most recent file ?

    Or including the loop (I was writing the full version as PeejAvery responded...)

    This won't work in all cases, but should get you started... This is also designed to handle multiple files with the same modified time.

    Code:
    use strict;
    
    my @abbr = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
    my $directory = $ARGV[0] || "./"; #Get the directory from the argument line or use the local directory
    
    opendir(DIR, $directory) || die "Can't open directory $directory: $!\n";
    
    my %latestmodifiedfiles = undef;
    my $latestfiletime = 0;
    while (defined(my $file = readdir(DIR)))
    {
    # Get file stats
       my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks) = stat($file);
    # Check if the modified time is null, if so skip...
       if ($mtime ne "")
       {
       # Check if the modified time is greater than the last modified time
          if ($mtime > $latestfiletime)
          {
             # if so, clear the hash and add the new filename to it - set the latest file time equal to said value.
             %latestmodifiedfiles = undef;
             $latestmodifiedfiles{$file} = "1";
             $latestfiletime = $mtime;
          }
       # Check if the times are equal - if so, add filename to the hash
          elsif ($mtime == $latestfiletime)
          {
             $latestmodifiedfiles{$file} = "1";
          }
       }
    # Otherwise, skip...
    }
    
    closedir(DIR);
    
    # Now iterate through the files in the hash and display the file time
    
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
                                                    localtime($latestfiletime);
    
    foreach my $file (keys %latestmodifiedfiles)
    {
       if ($file ne "." && $file ne "")
       {
          print "$file: " . sprintf("%02d:%02d:%02d", $hour,$min,$sec) . " $abbr[$mon] $mday " . sprintf("20%02d", $year % 100) . "\n";
       }
    }

  4. #4
    Join Date
    May 2004
    Location
    Pell City, Alabama
    Posts
    126

    Re: Perl: Filetime of most recent file ?

    thanks guys thats very helpful

    do you know if the file modified time on the containing directory will also be the same as the most recently modified file's modified time ?

    Just wondering if it would be simpiler to get the modified time from the directory instead ?

    so in other words would something like this work ?

    Code:
    open(DIR, $directory);
    $modified = localtime((stat DIR)[9]);
    just guessing from looking at your code
    Last edited by Mutilated1; October 27th, 2006 at 03:05 PM.

  5. #5

    Re: Perl: Filetime of most recent file ?

    That may work, I'm not really sure...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured