Getting a file's timestamp using ColdFusion

Finding a file's date/time is now quite easy in ColdFusion by simply making use of the underlying Java File and Date objects. A bit of code ...

<cfset filePath = "data\myfile.txt">
<cfset fileObj = createObject("java","java.io.File").init(expandPath(filePath))>
<cfset fileDate = createObject("java","java.util.Date").init(fileObj.lastModified())>

At this point the fileDate variable contains a normal ColdFusion date.

Couple of notes:

The java.io.File object gets information about the file, but does not read the file into memory. That's good.

The lastModified() method here returns a number (a 'long'), which is exactly what the java.util.Date object needs to create the Date object.

Remember not to use the variable name 'file' in your code. This appears to be a reserved name.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
19 Feb 2008 02:34AM
Martin Parry said:
Martin Parry's Gravatar Many thanks - This saves doing a CFDirectory with a filter applied then faffing about with LSDateformat
12 Mar 2008 08:31PM
Suman Kar said:
Suman Kar's Gravatar You saved my life! I spent the last 4 hours trying to debug what went wrong only to land here and know 'file' is a reserved word. Thank you!
19 Sep 2008 09:49AM
DCF said:
DCF's Gravatar I noticed that when I display the time stamp, its not correct. For example, my file was last modified on 09/18/2008, but the fileDate when displayed, shows:
{ts '1969-12-31 19:00:00'}

Is it just me?
21 Sep 2008 03:34AM
Kevan Stannard's Gravatar Not sure what may be happening there. You might like to post a message to CF-Talk ( http://www.houseoffusion.com/groups/cf-talk ) and see if anyone has some thoughts.
24 Sep 2008 06:27AM
Chris said:
Chris's Gravatar Thanks. This simple feature was surprisingly hard to find. Even the few people who posted about the java.io.File class usually forgot to include mention java.util.Date, which is essential since CF generally doesn't know anything about Unix timestamps.
26 Sep 2008 06:03AM
Rob said:
Rob's Gravatar I have run into the same 1969 problem as well. Clearly it's not calculating the date properly using this method, but I cannot find an alternative, nothing on HoF so far either. Those that got it to work right, did you do anything in addition to the posted code?
26 Sep 2008 06:10AM
Rob said:
Rob's Gravatar Ok I got it. The problem in the code for me was the usage of expandPath. This worked for me when I tweaked it:

<cfset filePath = "#GetCurrentTemplatePath()#">
<cfset fileObj = createObject("java","java.io.File").init(filePath)>
<cfset fileDate = createObject("java","java.util.Date").init(fileObj.lastModified())>
26 Sep 2008 01:33PM
Kevan Stannard's Gravatar Rob, thanks for the update.
8 Oct 2008 10:43PM
Mark said:
Mark's Gravatar This is great for reading a file's Last Modified date. Unfortunately, this value doesn't change when you copy a file. I have a situation (on a Windows 2003 server with CFMX 6.1) whereby I copy files to a temporary folder for users to retrieve. I want to read the Created date, or else possibly update the Modified date after a file is copied so that an automated task can look in the folder and delete files older than X minutes. The last modified date carries over from the original that is copied, so you don't have a true picture of how long the file has been in the temp directory. Is there a way to access the Created Date attribute of a file using java.util.Date (preferable)? Or a way to make a change to the last modified attribute after a file has been copied (less preferable)?
9 Oct 2008 02:30AM
Kevan Stannard's Gravatar You might be able to find a utility that lets you change the timestamp on a file. There may also be a couple of other options, depending on your setup.

One option might be to write a record to a database table with the current date/time and filename and use this to control which files should be deleted.

Another option may be to copy each file into a subdirectory based on a UUID and use the timestamp on the directory instead.
Add a comment
(will not be published)
(include http://)