in reply to Why is Stat() Not Stating All Files?

As the FM says:
If you're planning to filetest the return values out of a "readdir", you'd better prepend the directory in question. Otherwise, because we didn't "chdir" there, it would have +been testing the wrong file. opendir(DIR, $some_dir) || die "can't opendir $some_di +r: $!"; @dots = grep { /^\./ && -f "$some_dir/$_" } readdir(DI +R); closedir DIR;

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Replies are listed 'Best First'.
Re^2: Why is Stat() Not Stating All Files?
by awohld (Hermit) on May 02, 2005 at 06:06 UTC
    Okay, I changed my code:

    #!/usr/bin/perl my $dirtoread="/home/mydir/public_html/t/d/"; opendir(IDIR, $dirtoread) || die "Error Opening Directory"; print "Content-type: text/html\n\n"; my @dots = grep { /^\./ && -f "$dirtoread/$_" } readdir(IDIR); foreach $file (@dots) { ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$bl +ksize,$blocks) = stat($file); print <<EOF; File: $file<br> dev: $dev<br> ino: $ino<br> mode: $mode<br> nlink: $nlink<br> uid: $uid<br> gid: $gid<br> rdev: $rdev<br> size: $size<br> atime: $atime<br> mtime: $mtime<br> ctime: $ctime<br> blksize: $blksize<br> blocks: $blocks<br><br><br> EOF } closedir(IDIR);

    But it's not printing anything?
    Did I apply the code correctly?

    I don't think I did but I'm not really sure what the deal is!
      Did I apply the code correctly?
      No. You're still testing the wrong file, you still need to prepend the directory in question, only now you filter out directories from the results of readdir (you're left with files only).

      MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
      I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
      ** The third rule of perl club is a statement of fact: pod is sexy.

        Ahh I got it, I chaged stat($file) to stat($dir.$file).

        Any idea how I can tell directories from files? I doesn't look like stat will tell that from what I can see.