It's always good to include (a small bit of) the code you are having a problem with. Thus we can see you missed a bit in the description of readdir(). readdir() only gives you the next name out of the directory, which is thus only the filename, and not including the path to the directory. In steves example notice that he used directory '.' so there wasn't a path to worry about.

Anyway, check this modified version of your code:

use File::Spec; my $dir = "c:\\Perl"; opendir DIR, $dir or die $!; foreach my $file (readdir(DIR)){ my $path_to_file = File::Spec->catfile( $dir, $file ); printf " I see file '%s' path '%s'\n", $file, $path_to_file; next unless -e $path_to_file; unless(-d $path_to_file){ print $path_to_file; my @stats = lstat ($path_to_file); print "\t\$stat = $stats[7]\n"; } }
Note that the full path to the file to use with -d , -e , or lstat() is the combination of the directory path plus the filename returned from readdir().

I used the File::Spec module to combine these because I'd rather let it worry about combining directory paths and filenames and getting the '/' or '\' separators correct.


In reply to Re^3: no size with stat and lstat in windows by shenme
in thread no size with stat and lstat in windows by Scarborough

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.