For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > March 2008 > Problem with STAT under Windows









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Problem with STAT under Windows
Anders

2008-03-31, 8:29 am

I tryed some testkod below, i like Perl to give med TIME for every
file,
But all variable just get blank, (i got filename).

# Reads all files to a list
sub readFileList {
my @result;

local $dev;
local $ino;
local $mode;
local$nlink;
local $uid;
local $gid;
local $rdev;
local $size;
local $atime;
local $mtime;
local $ctime;
local $blksize;
local $blocks;

local @dir_contents;
local $dir_to_open="f:\\backupsv";
opendir(DIR,$dir_to_open) || die("Cannot open directory !\n");
@dir_contents= readdir(DIR); # Get contents of directory
closedir(DIR); # Close the directory

# Now loop through array and print file names
foreach $file (@dir_contents) {
if( substr($file,-3,3) eq "TXT" ) { # $file =~ /\*.txt/
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,
$size,$atime,$mtime,
$ctime,$blksize,$blocks) = stat($file);
# getRefDate();
print "File is $file\n";
# push @result,$file;
print "size $size \n";
print "atime $atime \n";
print "mtime $mtime \n";
print "ctime $ctime \n";
exit(0);
}
}
return @result;

}

Gunnar Hjalmarsson

2008-03-31, 7:51 pm

anders wrote:
> I tryed some testkod below, i like Perl to give med TIME for every
> file,
> But all variable just get blank, (i got filename).


You need to either alter the current directory using chdir(), or pass
the full path to stat().

You may want to try this code:

use strict;
use warnings;
use Data::Dumper;

my $dir_to_open = 'f:/backupsv';
my $files = readFileList($dir_to_open);
print Dumper $files;

sub readFileList {
my $dir = shift;
opendir my $dh, $dir or die $!;
my @files = grep /\.txt/i, readdir $dh;
my %result;
foreach my $file ( @files ) {
@{ $result{$file} }{ qw/size atime mtime ctime/ } =
( stat "$dir/$file" )[7..10];
}
return \%result;
}

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com