Programming C# C++ (7) Delphi (617) Java (8) JavaScript (31) perl (9) mysql (3) perl CGI (3) php (4) VBScript (1) Visual Basic (1)
Exchange Links About this site Links to us 
|
How do I get a file's date / timestamp in perl?
6 comments. Current rating: (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:
|
anonymous
|
 |
|
|
Appear to work only for Local files? How about remote server files?
|
|
|
|
|
How do i edit the timestamps of files in sm_tomcat.log withouth affecting the server time?
|
|
anonymous from United States
|
 |
|
|
Thank You
|
2009-11-18, 06:42:19 (updated: 2009-11-18, 06:43:08) |
|
|
|
Doesn't appear to work. Cannot call method mtime on an undefined value
|
|
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';
|
|