using File::stat

Hello everyone,

I need some help on a perl script. The script is to open a dir and print out the date of last modification on all files. I'm been trying this code but it doesn't work.

use File::stat;

open (D,"$ARGV[0]") or die "Can't open\n";

while (defined ($file = readdir D))
{
  next if ! -T $file;
  $last_mod = (stat($file))[9] or die"Can't stat\n";
  print"$file:\t$last_mod\n";
}

keep getting "Can't stat"!!! What did I do wrong? Appreciate any help...

Use opendir not open, stat needs the full pathname not just the filename. Use scalar and localtime on mtime:

#!/usr/bin/perl -w
use File::stat;
$dirname = $ARGV[0];
opendir (D, $dirname) or die "Can't open\n";
while (defined($filename = readdir D ))
{
    next if ! -T $filename;
    $fullname=$dirname."/".$filename;
    $st = stat($fullname) or die "Can't stat\n";
    printf "%s\t%s\n", $filename,  scalar localtime $st->mtime;
}

thx a million C, really appreciated. Just one more question:

$dirname."/".$filename => what does this do? Making a path?

---------- Post updated at 05:01 PM ---------- Previous update was at 04:31 PM ----------

Just ran the code again, the problem is it will print all files with the same date... I will try to trouble-shoot it... any suggestion is greatly appreciated...

Yes, it just concatenates dirname "/" and filename to generate a pathname

Edit - Try this instead:

print "$filename:\t" . localtime($st->mtime) . "\n";