Shaun Mccran

My digital playground

03
J
U
L
2009

Function for getting the last modified date of a template

Whilst looking at creating dynamic Sitemaps for Google bot spidering I found that I needed to populate an XML node with the last modified date of the templates. I figured there must be a programmatic way of doing this, so after some searching around this is what I ended out with:

view plain print about
1<cfset mod_time = createObject("java", "java.util.Date").init(createObject("java", "java.io.File").init(getcurrenttemplatepath()).lastModified())>

This creates a java object, using the date and file utilities we can use 'getcurrenttemplatepath()' to provide the path data, finally referencing the lastModified property.

A handy and relatively quick way of getting the last modified date.

It is a bit of a mouthful though, so might be easier to create it as a handy referencable object:

view plain print about
1<cffunction name="modStamp" access="public" returntype="date" output="no" hint="Gets the last mod date of a file, returns a ts">
2    <cfargument name="file" type="string" required="yes" hint="File to get mod date">
3        <cfset var mod_time = now()>
4        
5        <cfif fileexists(arguments.file)>
6            <cfset mod_time = createObject("java", "java.util.Date").init(createObject("java", "java.io.File").init(arguments.file).lastModified())>
7        </cfif>
8        
9    <cfreturn mod_time>
10</cffunction>

Then you can just do:

view plain print about
1<cfset variables.fileMod = modStamp()>

TweetBacks
Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Back to top