Jerry Stark
Jerry Stark

Reputation: 29

Using Perl stat function on Windows

I am trying to use Perl on windows. I am think I am having a problem either with syntax or with access to CPAN information. What information I can find gives snippets on how to use the functions and when I try to use the examples I get errors.

I do not know how to get to CPAN. I tried to install it and it failed twice on GCC and dmake. I do not know if that would help or not. I used http://www.cpan.org/modules/INSTALL.html cpan App::cpanminus

I have a filename with the path ex: c:\users\me\directory\\*.* This file contains a list of path+filename records for which I need the time and size of the file. When I read a record I can verify that the path+filename is correct.

I have had some help doing this in a batch file. I was using %~t1 and %~z1 as shown here:

pass the full path to the batch file then use

if "%~t1" == "" (
    echo "                      AN ERROR OCURRED  FOR THIS FILE/n"  
) else (   
    echo fpath =%~1 
    echo time = %~t1   
    echo size = %~z1   
)

When I tried to bring this over to Perl I can not get it to work. I have run out of hints and ideas to try.

I have tried to use a similar IF ( "%~t1" == "" ) {} but I get (missing operator before t1?).

I tried to use my $str1 = %~t1 and got "str1" not allowed while "strict subs" in use.

I tried to use $info->$ctime and got requires explicit package name.

I tried my $modtime = (stat($dirHandle))[9] with print "mod time = $mod_time \n"; and got no such class mod_time and syntax error.

I tried using the following I got from a perl programing documentation file for perl 5.20.1

($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
     $atime,$mtime,$ctime,$blksize,$blocks)
         = stat($filename);

I could not see how that would work and it did not. I got a lot of requires explicit package name

I tried this from the same reference

use File::stat;
$sb = stat($filename);
printf "File is %s, size is %s, perm %04o, mtime %s\n",
       $filename, $sb->size, $sb->mode & 07777,
       scalar localtime $sb->mtime;

I can not remember all the errors

Every time I see File::stat or Path::Class::File I do not know how to get any of these.

The perl monks are a little over my head as far as getting answers. I get lost in all the what ifs.

Thanks for any help you can give me.

Upvotes: 0

Views: 2150

Answers (1)

ikegami
ikegami

Reputation: 385754

Sounds like you're asking how to get the size and the last-modified time of a file.

Using stat:

use POSIX qw( strftime );

my $qfn = 'c:\\users\\me\\directory\\file';

my ($size, $mtime) = (stat($qfn))[7, 9]
   or die("Can't stat \"$qfn\": $!\n");

printf("File %s: size=%s modified=%s\n",
   $qfn,
   $size,
   strftime("%Y-%m-%d %H:%M:%S", localtime($mtime)),
);

Using File::stat, which replaces stat with a function with a friendlier interface:

use File::stat qw( stat );
use POSIX      qw( strftime );

my $qfn = 'c:\\users\\me\\directory\\file';

my $stat = stat($qfn)
   or die("Can't stat \"$qfn\": $!\n");

printf("File %s: size=%s modified=%s\n",
   $qfn,
   $stat->size,
   strftime("%Y-%m-%d %H:%M:%S", localtime($stat->mtime)),
);

Both modules mentioned come with Perl. No need to install them.

Upvotes: 3

Related Questions