Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

File test to see creation time rather than last modified time

by walkingthecow (Friar)
on Feb 05, 2009 at 16:32 UTC ( #741594=perlquestion: print w/ replies, xml ) Need Help??
walkingthecow has asked for the wisdom of the Perl Monks concerning the following question:

if (-M FILE > 5) { ... }

Alright, so I know how to test to see when a file was last modified; now I am trying to figure out how to test to see when a file was created. Why? See below for a reason. Maybe there is a different approach to solve this problem...

I have a script that allows the person using it to create a file that stores data (text). However, after about a week this data is usually out-of-date (expired), therefore I do not want the user to use this data file any longer. Everytime the script runs, it will write to this file, so using the -M flag will not work since the file will always be up-to-date. So, how can I test when the script first created the file? I guess I could put a timestamp on the file, and then test that way, but can anyone think of any other ways to do this?

Comment on File test to see creation time rather than last modified time
Re: File test to see creation time rather than last modified time
by almut (Canon) on Feb 05, 2009 at 16:50 UTC

    You could use stat():

    if ( (time - (stat FILE)[10]) / (60*60*24) > 5 ) { ... }

    Actually, as -M is "Script start time minus file modification time, in days", you'd have to store the return value of time() right at the beginning of the script, to get the exact equivalent for ctime... though I suppose the difference would be negligible.   (Update: actually (RTFM :) — use "perldoc -f -X" to get the docs for the filetests...) there even seems to be an option -C which does just that...)

    Alternatively (Update2: and preferably — see kenneth/MidLifeXis's comments below), reset the modification time to what it was before, every time you make a modification...

Re: File test to see creation time rather than last modified time
by kennethk (Monsignor) on Feb 05, 2009 at 16:57 UTC
    This is platform dependent. On M$, you can use stat but not on Mac or *NIX. *NIX does not store the information at all (see the stat(2) man pages). Details on implementation of the Perl function are in perlport. Your best bet if you are not on M$ is using utime to reset the modification time, as per almut's comment.
Re: File test to see creation time rather than last modified time
by MidLifeXis (Prior) on Feb 05, 2009 at 17:38 UTC

    Under *nix, ctime is not creation time, but actually the last time the inode for the file was changed (from man stat - Last file status change time). MTime is the last modification time. As was said by kennethk in Re: File test to see creation time rather than last modified time, *nix does not record creation time of a document. An inode may change at some unexpected times (for example, when the mtime is changed).

    ls also lists the -c option as

    Use time of last modification of the inode (file created, mode changed, etc.) for sorting (-t) or printing (-l (ell))

    I have seen the c in ctime being misunderstood for the 15-20 years I have been using *nix. In my tenure, this misunderstanding has been the cause of many mistakes on interpreting data from the filesystem.

    --MidLifeXis

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://741594]
Approved by almut
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (14)
As of 2014-10-23 15:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (125 votes), past polls