Re: How to get the File creation date
by MidLifeXis (Monsignor) on Aug 17, 2010 at 15:16 UTC
|
The "creation" time is a misnomer. The ctime field is actually the modification time of the inode (http://www.unix.com/tips-tutorials/20526-mtime-ctime-atime.html), not the creation time. The inode can be modified by more than just creation of the item (chmod / chown, for example).
In short, this is not, to my knowledge, possible with the ctime field. It may be accurate in many situations, but will fail in just as many.
| [reply] |
|
|
Different file systems have different ways of dealing with "creation time". Normal Unix file systems do not have a concept of creation vs modification time at all. You can only get "last modified" time. Windows NTFS does have the concept of: creation time, last accessed time, and last modification time. see:
Windows file times.
There is an API to modify an NTFS's file "creation time" - so it could be that although Windows has the "creation time" concept, it is not guaranteed that the time you get is really the "creation time" as anybody with the appropriate permissions could have modified that time! Whoa!
Access to the Windows "creation time" requires the Win API and is not part of "standard Perl".
So: some file systems track "creation time". Unix file systems are not one of them. Normally the "last modified" time is sufficient.
And that's a good thing as that is all Perl allows you to get to with the standard built-in functions. But as a "nit" here, I point out that some filesystems do track creation time.
I think that the OP would do well by measuring the shortest of these parameters:
-M Modification age (measured in days)
-A Access age (measured in days)
If you haven't read a file or modified it within X days, then I would say that it is a "candidate for deletion". Under this kind of a test, I don't see how when the file was originally created makes a difference. If it not being used or modified, then who cares? Implement something like the Windows Trash Folder. Instead of deleting a file outright, move it there and wait for Y days to see if anybody really misses it! If you don't get any complaints, then do a final deletion.
| [reply] |
|
|
Different file systems have different ways of dealing with "creation time". Normal Unix file systems do not have a concept of creation vs modification time at all. You can only get "last modified" time. Windows NTFS does have the concept of: creation time, last accessed time, and last modification time. see (sic) ...
If I am reading your statement correctly, you are saying that the ctime, mtime, and atime do not exist under the historical unix file system inode. Is this correct?
So: some file systems track "creation time". Unix file systems are not one of them. Normally the "last modified" time is sufficient. And that's a good thing as that is all Perl allows you to get to with the standard built-in functions. But as a "nit" here, I point out that some filesystems do track creation time.
I can concur with that. Reference: "the ctime test may actually return the creation time". I would, however, ask what creation time signifies. What happens when a file is restored? Does the attribute follow on a copy? How about when the method of saving a file is "save new, rename old, rename new, remove old"?
I think that in the context we are discussing (OP's question), these concepts are for the most part equivalent.
Update: Cleaned up my thoughts and wording
| [reply] |
|
|
|
|
|
Re: How to get the File creation date
by choroba (Cardinal) on Aug 17, 2010 at 15:09 UTC
|
You have to add parenthesis to tell perl what the list is: $m = (stat $file)[9];
| [reply] [d/l] |
|
|
I done like this, but i get the answers is dome digit (i.e.) 198768745
how to convert this to date.
| [reply] |
|
|
What you have there is the number of seconds since midnight Jan 1 1970. This is known as epoch time. To convert you can use the localtime function, this returns, an array of the date elements by default, however if called in a scalar context it will return a time string, you can force scalar context with the scalar function ot by assigning to a scalar variable
Alternatively read my sig ;)
Incidental note, 198768745 represents Mon Apr 19 14:32:25 1976, so either
- You typed in some random digits rather than the content of (stat $file)[9]
- You're running a UnixTSS box
- Or the figure came from somewhere else
print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
| [reply] [d/l] [select] |
Re: How to get the File creation date
by jethro (Monsignor) on Aug 17, 2010 at 15:11 UTC
|
Please use <code> tags for code, otherwise it looks mighty strange.
What you have written is equivalent to
$m= stat ($file[9]);
What you probably wanted is
$m= (stat $file)[9];
| [reply] [d/l] [select] |
Re: How to get the File creation date
by dasgar (Priest) on Aug 17, 2010 at 15:13 UTC
|
Are you sure that you want "creation date"? You can get a file's last access date and last modified date. If the file has been modified multiple times, I personally am not aware of a method to retrieve the "creation date" of the file.
| [reply] |
|
|
Yes i want only file creation date. when the file is created.
| [reply] |
|
|
| [reply] |
Re: How to get the File creation date
by locked_user sundialsvc4 (Abbot) on Aug 19, 2010 at 04:05 UTC
|
I like to use CPAN modules, such as File::Util, to do things like this, because I am basically hoping to be able to slough off the dirty-work to somebody else. :-D
| |