CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Reading Directories

 



benchivers
Novice

Apr 15, 2002, 10:56 AM

Post #1 of 5 (420 views)
Reading Directories Can't Post

I am able to read directories to retrieve file names, but I can not retrieve any data like:

Last Modification Date

File Size

Does anyone know how to retrieve this data from each file?

Here's my program code, so far:

opendir(DIR,"dir");

@dir = readdir(DIR);

chomp(@dir);

closedir(DIR);



foreach $file (@dir){

print "filename - $file\n";

print "file size - $filesize\n";

print "modification date - $mod_date\n";

}



any help would be most appreciated!

And I would prefer it, if the source code did not use the cgi.pm module! Thanks!

Regards,

Ben Chivers
Wheres the damn coffee? zzzZZZZZ!!!


Jasmine
Administrator

Apr 15, 2002, 11:09 AM

Post #2 of 5 (418 views)
Re: [benchivers] Reading Directories [In reply to] Can't Post

[url=http://www.perldoc.com/perl5.6.1/pod/func/stat.html]stat will get that info for you.


Code
foreach my $file (@dir){  

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

print "filename - $file\n";

print "file size - $size\n";

print "modification date - $mtime\n";

# or, a slice will keep it simple:
my ( $size, $mod_time ) = ( stat( $file ) )[7, 9];

}


Here's the order in which stat returns data:

Code
  0 dev      device number of filesystem 
1 ino inode number
2 mode file mode (type and permissions)
3 nlink number of (hard) links to the file
4 uid numeric user ID of file's owner
5 gid numeric group ID of file's owner
6 rdev the device identifier (special files only)
7 size total size of file, in bytes
8 atime last access time in seconds since the epoch
9 mtime last modify time in seconds since the epoch
10 ctime inode change time (NOT creation time!) in seconds since the epoch
11 blksize preferred block size for file system I/O
12 blocks actual number of blocks allocated



benchivers
Novice

Apr 15, 2002, 12:15 PM

Post #3 of 5 (411 views)
Re: [Jasmine] Reading Directories [In reply to] Can't Post

Thanks for your quick reply!

I have got everything working now, but how do you get the modified time in a readable format like:

Mon 8th April 2002

or

04/08/2002

Thanks Jasmine!

Regards,

Ben Chivers
Wheres the damn coffee? zzzZZZZZ!!!


d1zz13
User

Apr 16, 2002, 5:53 AM

Post #4 of 5 (404 views)
Re: [benchivers] Reading Directories [In reply to] Can't Post

You need to convert the time to either localtime or GMT.

If you kept the variables as Jasmine had them then this should work:

Code
$modifiedtime = localtime($mtime);  #This gives you localtime

or if you wanted GMT its as simple as:

Code
$modifiedtime = gmtime($mtime);  #This gives you GMT

I trust this helps.

Rich

Regards
Rich

If it aint broke then don't try to fix it

(This post was edited by d1zz13 on Apr 16, 2002, 6:00 AM)


yapp
User

Apr 16, 2002, 7:50 AM

Post #5 of 5 (400 views)
Re: [benchivers] Reading Directories [In reply to] Can't Post

Oh, and to get such the "04/08/2002" format:

the localtime() function returns an array with time values when you call it in a list context:


Code
my @timevalues = localtime($your_time);


See [url=http://www.perldoc.com/perl5.6.1/pod/func/localtime.html]perldoc -f localtime for more details about those values Smile

Yet Another Perl Programmer

_________________________________
~~> [url=http://www.codingdomain.com]www.codingdomain.com <~~
More then 3500 X-Forum [url=http://www.codingdomain.com/cgi-perl/downloads/x-forum]Downloads! Cool

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives