Finding the Last Modified Date on a File
This question came up on my local CFUG mailing list yesterday:
how can I get the time last updated of the two documents? I know you can do this with uploaded files using FILE.TimeLastModified. Is there anyway to do it with existing files? I know I could do a hack with CFDIRECTORY where I loop through the files in the directory looking for the one I'm interested in and then getting the dateLastModified on that, but I'm thinking there has to be a more direct way.
You can use java's File class to get the lastModified timestamp:
<cfset myFile = CreateObject("java", "java.io.File")>
<cfset myFile.init("/path/to/file")>
<cfset last_modified = myFile.lastModified()>
Related Entries
- What Version of Java is ColdFusion Using? - February 25, 2010
- Serializing CFC's in ColdFusion 8 - August 6, 2007
- ColdFusion on the TIOBE index - March 18, 2006
- Null Java References in CF 6 vs 7 - January 10, 2006
- DNS Query with ColdFusion - October 27, 2005
Trackbacks
Trackback Address: 620/F7BDBBBEFF470F500A9538D65905E82F
Comments
On 01/11/2007 at 2:33:30 PM EST Sam Farmer wrote:
1
Thats a great solution.
There is also the filter attribute to cfdirectory that would negate the need to loop over and look for the file. Still I think the Java File solution is better.
On 01/11/2007 at 3:45:50 PM EST Elliott wrote:
2
If you don't want to be dependent on Java you can use <cfdirectory>, specify a filter that is the file name and a query of queries on the result to match the actual file (fix due to wildcards in file name).
No need to loop. :)
Its slower, but more portable.
On 01/11/2007 at 3:59:22 PM EST Sam wrote:
3
Well, you don't exactly have to loop through it. Can't you just query it?
On 06/15/2007 at 8:53:24 AM EDT Navin wrote:
4
Hi
I want to display last modified date in HTML page.
On 07/26/2007 at 11:00:14 AM EDT Patrick Whittingham wrote:
5
Do I use the absolute path (ie., d:/..a.js) and I'm getting a number (ie., 1178038419822 ). Why?
On 02/20/2008 at 7:48:06 PM EST bruce wrote:
6
you may try this: File file = new File(filename); long time = file.System.currentTimeMillis(); boolean success = file.setLastModified(newModifiedTime); if (!success) {...} // operation failed.
On 04/17/2008 at 2:44:20 PM EDT Kevin penny wrote:
7
Documentation says: Returns the time that the file denoted by this abstract pathname was last modified.
Returns: A long value representing the time the file was last modified, measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970), or 0L if the file does not exist or if an I/O error occurs
I suppose you'd have to compare it at the ms level in that case -
<cfset y = datediff('s','00:00:00 January 1, 1970',now())*1000/>
On 04/17/2008 at 3:32:12 PM EDT Kevin Penny wrote:
8
Actually, this works better as it takes into account your local timezone difference between your server and GMT.
<cfset y = datediff('s',LSParseDateTime('January 1, 1970 00:00:00 AM GMT'),now())*1000/>
This will convert the Base Jan 1970 time into MS where you can then use simple math to do your comparisons at the MilliSeconds level.
Post a Comment
Recent Entries
- Cache Template in Request Setting Explained
- What Version of Java is ColdFusion Using?
- ColdFusion 9 Performance Brief from Adobe
- Request Filtering in IIS 7 Howto
- J2EE Session Cookies on ColdFusion / JRun
- Hands on ColdFusion Security Training
- ColdFusion 9 Solr Vulnerability - Are you at Risk?
- FCKEditor Year 2010 Bug for Firefox 3.6 with ColdFusion
There is also the filter attribute to cfdirectory that would negate the need to loop over and look for the file. Still I think the Java File solution is better.
No need to loop. :)
Its slower, but more portable.
I want to display last modified date in HTML page.
Returns: A long value representing the time the file was last modified, measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970), or 0L if the file does not exist or if an I/O error occurs
I suppose you'd have to compare it at the ms level in that case -
<cfset y = datediff('s','00:00:00 January 1, 1970',now())*1000/>
<cfset y = datediff('s',LSParseDateTime('January 1, 1970 00:00:00 AM GMT'),now())*1000/>
This will convert the Base Jan 1970 time into MS where you can then use simple math to do your comparisons at the MilliSeconds level.



add to del.icio.us



