in reply to Re: no size with stat and lstat in windows
in thread no size with stat and lstat in windows

I'm running this code

use strict; my $dir = "c:\\params"; opendir DIR, $dir or die $!; foreach my $file (readdir(DIR)){ unless(-d $file){ print $file; my @stats = lstat ($file); print "\t\$stat = $stats[7]\n"; } }

And get this

AMRO $stat = DATE CATEGORY $stat = NEON $stat = PARAMIN.pm $stat = 1137 SMSL $stat = SMSREPSMEMIDCTOTS10.TXT $stat = SMSREPSMEMIDCTOTS11.TXT $stat = SMSREPSMEMIDCTOTS12.TXT $stat = SMSREPSMEMIDCTOTS13.TXT $stat = SMSREPSMEMIDCTOTS14.TXT $stat = tp1.pl $stat = 247

Which has 2 errors no stats for the TXT files and NEON and SMSL are directories but . and .. are filtered out.

Anyone seem this at all

Replies are listed 'Best First'.
Re^3: no size with stat and lstat in windows
by shenme (Priest) on Sep 20, 2004 at 19:41 UTC
    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.

      Thanks to both of you for your help I see the problem now and have reworked the code accordingly.