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 = "datamyfile.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.

This entry was posted in ColdFusion and tagged , . Bookmark the permalink. Both comments and trackbacks are currently closed.

13 Comments

  1. Posted February 19, 2008 at 2:34 am | Permalink

    Many thanks – This saves doing a CFDirectory with a filter applied then faffing about with LSDateformat

  2. Suman Kar
    Posted March 12, 2008 at 8:31 pm | Permalink

    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!

  3. DCF
    Posted September 19, 2008 at 9:49 am | Permalink

    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?

  4. Posted September 21, 2008 at 3:34 am | Permalink

    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.

  5. Chris
    Posted September 24, 2008 at 6:27 am | Permalink

    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.

  6. Rob
    Posted September 26, 2008 at 6:03 am | Permalink

    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?

  7. Rob
    Posted September 26, 2008 at 6:10 am | Permalink

    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())>

  8. Posted September 26, 2008 at 1:33 pm | Permalink

    Rob, thanks for the update.

  9. Posted October 8, 2008 at 10:43 pm | Permalink

    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)?

  10. Posted October 9, 2008 at 2:30 am | Permalink

    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.

  11. Angela Thompson
    Posted March 11, 2010 at 4:22 pm | Permalink

    Thanks for this post – very helpful because it formats the date in a readable format, without the long numbers (unlike the other Java scripts that I found).

    Is there a way of pulling the file’s *actual* modified date? Not the modified date on the server, but the actual date it was modified? {For a MS-Word file, this is the info that appears when you click on File > Properties > Statistics.}

    I built a web-based file system and want to print-out all the files in order of their actual modified date, so that I can let the managers of certain files know that their files are very out of date.

    Is this possible? Thanks in advance!

  12. Posted March 11, 2010 at 5:20 pm | Permalink

    Hi Angela, the modified date on the server should be the actual modified date. Are you seeing a different date in the file’s properties window?

  13. Angela Thompson
    Posted March 11, 2010 at 9:44 pm | Permalink

    Hi Kevan,

    It would seem that by using a web-form to upload the documents to the server, I screwed-up all the real "modified" dates.

    So I thought that using the "Created" date (instead of modified) would be a second-best solution… However, I came across this thread (http://www.mail-archive.com/cfaussie@lists.daemon.com.au/msg21268.html). you read into a couple of the answers, it seems though this is not possible to grab the "creation date" without using a Windows COM object (which I can’t use since we’re running UNIX).

    So I’m not sure I’ll be able to print-out actual creation dates, even though it seems like it would be a relatively simple task.

    I’ll update you if I do find a workaround.

    Cheers,

    Angela