DelphiFAQ Home Search:

How do I get a file's date / timestamp in perl?

 

comments9 comments. Current rating: 5 stars (3 votes). Leave comments and/ or rate it.

Question:

How do I get a file's date / timestamp in perl?

Answer:

For clean code, it is recommended to use File::stat module which is part of the standard distribution in version 5.004 and later.

Use method mtime() to get the last modification time. This is the time the file itself was modified last.

Instead you can also use stat.ctime which is the time directory information about the file was changed, not the file itself.

stat.atime is the last time the file was accessed.

use File::stat;
use Time::localtime;

$datetime_string = ctime(stat($file)->mtime);

print "file $file was updated at $datetime_string\n";

Comments:

2007-03-28, 05:48:13
anonymous  
rating
Appear to work only for Local files? How about remote server files?
2009-04-20, 08:45:43
anonymous  
How do i edit the timestamps of files in sm_tomcat.log withouth affecting the server time?
2009-05-22, 17:35:21
anonymous from United States  
rating
Thank You
2009-11-18, 06:42:19   (updated: 2009-11-18, 06:43:08)
anonymous  
Doesn't appear to work. Cannot call method mtime on an undefined value
2010-01-21, 18:04:14
anonymous from United Kingdom  
You need to define $file:

   

use File::stat;
use Time::localtime;

$file = '/etc/issue';
$datetime_string = ctime(stat($file)->mtime);

print 'file $file was updated at $datetime_string\n';


2012-08-07, 09:48:16
anonymous from Ireland  
For above code to work you need double brackets..


use File::stat;
use Time::localtime;

$file = '/etc/issue';
$datetime_string = ctime(stat($file)->mtime);

print 'file $file was updated at $datetime_string\n';


2012-11-12, 04:03:03
anonymous from United States  
2012-11-12, 05:09:36
coach001 from United States  

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: