Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

modified date and size of a file 1

Status
Not open for further replies.

imad77

Instructor
Oct 18, 2008
97
CA
Hi,

I tried to print a file name and its modified date and size.
I have 2 issues for some files when I tried to print their modified date: I get "Dec 5:" instead "5 Dec 13:01" including the time. But for other files I get the right result as "11 Dec 13:01".
The second issue is related to the size I get 0 and all of files are not empty.

Can someone help me to fix this issue?

Thanks a lot,

Imad77

============

print qq~
<table border="1" cellpadding="4">
~;

my $filefull=$path."/".$file;
$m = (stat "$filefull")[9];
my $size10 = (stat "$filefull")[7];

$j=(scalar localtime($m));
my ($day, $month, $day, $time, $year) = split(/ /, $j);

my ($t1,$t2,$t3)=split(/:/, $time);
my $md = $day.' '.$month.' '.$t1.':'.$t2;

print qq~ <tr><td><a $file</a></td>
~;
print qq~ <td> $size10</td>
~;
print qq~ <td> $md</td></tr>
~;
print qq~
</table>
~;

 
There could be a problem with this line:
Code:
my ($day, $month, $day, $time, $year) = split(/ /, $j);
You are using $day twice.

I also prefer to use POSIX and then get my date string like this:
Code:
use POSIX;
my $md = strftime("%d %b %H:%M", localtime($m));
Just a preference though
 
Hi,
It should be $dayname instead $day:

my ($dayname, $month, $day, $time, $year) = split(/ /, $j);

I tried your suggestion and I get "31 Dec 19:00" for all files. Have you any idea what the reason that I get this issue?

Thanks a lot
 
Hi ,

When I run this script fro a specified file, I get the good results, but when I use it via website Perl/CGI I get size=0 and modified date= "31 Dec 19:00":

===============================
#!/usr/bin/perl
use POSIX;

$path="/home/test";
$file="prod.psc";
my $filefull=$path."/".$file;
$m = (stat "$filefull")[9];
my $s= (stat "$filefull")[7];

$j=(scalar localtime($m));
my ($day, $month, $day, $time, $year) = split(/ /, $j);

my ($t1,$t2,$t3)=split(/:/, $time);
my $md = $day.' '.$month.' '.$t1.':'.$t2;
my $md = strftime("%d ii %b %H:%M", localtime($m));
print "name : $file \n";
print "date : $md \n";
print "size : $s \n";
===========

 
instead of:

Code:
my ($dayname, $month, $day, $time, $year) = split(/ /, $j);

try:

Code:
my ($dayname, $month, $day, $time, $year) = split(/\s+/, $j);

Your split pattern is a single space, but if I remember correctly the scalar value of localtime can have more than one space between some of the date/time values. Anyway, try it and see if it helps.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Does the user that the http server is using have permissions on the files or the directory that the files are in? Do all of the files get the exact same time and size=0?
 
Hi Kevin,

It works fine now with your suggestion for the modified date.
But the result of size = 0. I think the 7th value in the scalar is the size
my $s= (stat "$filefull")[7];

I think something is wrong in my declaration or packages.
Is it related to a special package? or another way to extract this value?

Thanks a lot.

Imad77
 
The 7th index of the stat array is the file size. Try printing that value before foramtting and see what it is:

Code:
$m = (stat "$filefull")[9];
my $s= (stat "$filefull")[7] || 'no value returned from stat';
print $s,"\n";

You should really do it in one call to stat:

Code:
my ($m,$s) = (stat "$filefull")[9,7];

And you should defintely remove the quotes from the scalar:

Code:
my ($m,$s) = (stat $filefull)[9,7];

There is almost never a need to put quotes around a scalar unless you are making a new variable/string out of it, which you're not.

You can also try ry this:

Code:
$path="/home/test";
$file="prod.psc";
my $filefull=$path."/".$file;
open(TEST, $filefull) or die "Can't open $filefull: $!";
close TEST;

And see if die returns any error.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Hi Kevin,

When I run these commands,

==========
my $s= (stat "$filefull")[7] || 'no value returned from stat'
==========

I get this error message for all files:
no value returned from stat

But I get the good modified date.

If I try these commands as you suggested:

my ($m,$s) = (stat "$filefull")[9,7];
open(TEST, $filefull) or die "Can't open $filefull: $!";
close TEST;

I don't get any error message but I get 0 for the size.

I don't know why the size experience this kind of error.


 
Try adding this line:

Code:
if ( ! -e $filefull ) {
  print "$filefull does not exist\n";
}
if ( ! -r $filefull ) {
  print "$filefull is not readable\n";
}
 
Hi,

I found my issue and resolved thanks a lot.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top