Going back to another item from last weekend, about getting the "page modified date" for a current page, Cheryl, that's a useful solution you offered (the email below, for reference), and I see Brigitte appreciated it. If you don't mind, I'd like to offer a couple of refinements, not to denigrate it but to share some observations that perhaps even others may find helpful. The changes reduce the size of the routine to 2 lines and also can speed it up significantly. 
 
I sincerely hope all this is taken in the spirit of cooperation and education in which it's offered, and not one-upsmanship. Indeed, it's a shame we all don't get to participate in more code reviews. It can be a great way for folks to learn from each other. We often work on our own and miss that great opportunity.
 
First, you could collapse the 3 lines:
 
    <cfset thisPath = ExpandPath("*.*")>
    <cfset thisDirectory = GetDirectoryFromPath(thisPath)>
    <cfdirectory action="LIST" directory="#thisDirectory#" name="GetDir">
 
into 1:
 
    <cfdirectory action="LIST" directory="#ExpandPath(".")#" name="GetDir">
 
putting the function right in the CFDIRECTORY DIRECTORY attribute. I suppose you may have chosen to set the 2 variables because embedded functions within functions might look messy :-), but in fact the GetDirectoryFromPath you used in the second assignment isn't needed. Well, I should say it's not needed if you change the ExpandPath to use just a "." rather than "*.*". That's an acceptable value, and it works in a CFDIRECTORY to get all the files and directories in the current directory. It may not have been obvious from the docs. Indeed, the docs for the ExpandPath function offer the very solution you did, so perhaps where you got it. Oh well, the docs have been inaccurate before. :-)
 
Also, the last two lines can be collapsed into one, as in:
    Last modified date: <cfoutput>#dateformat(mystring, "yyyy-mm-dd")#</cfoutput>
 
There's no reason to break the lastmodifieddate you found into parts to pass it to the DateFormat function. That also may not have seemed obvious, but it knows to ignore the time portion. That would reduce it to just 7 lines:
 
<cfdirectory action="LIST" directory="#ExpandPath(".")#" name="GetDir" >
<cfoutput query="GetDir">
    <cfif GetDir.name eq GetFileFromPath(GetBaseTemplatePath())>
        <cfset myString = GetDir.DateLastModified>
    </cfif>
</cfoutput>
Last modified date: <cfoutput>#dateformat(mystring, "yyyy-mm-dd")#</cfoutput>
 
But better still, you don't need to do that querying of the CFDirectory resultset to find the file you're interested in, because if you added the FILTER attribute on the CFDIRECTORY, it could find JUST the file you're interested in. Very cool! :-) This reduces it to just 2 lines:
 
<cfdirectory action="LIST" directory="#ExpandPath(".")#" name="GetDir" filter="#getfilefrompath(gettemplatepath())#" >
Last modified date: <cfoutput>#dateformat(GetDir.DateLastModified, "yyyy-mm-dd")#</cfoutput>
 
Again, I don't mean to embarrass you by all this. Not at all!  :-) What you offered was a great solution to Brigitte's needs. I'm only offering refinements. It just so happens that this very thing (querying a CFDIRECTORY results) is something I show in my CF 5 one-day seminar, as a demonstration of the kind things people may not think to do with it, so it happened to be in my head. Certainly, your approach of looping through it is needed in a non-CF5 environment.
 
I'll throw in as well that this is another great opp to look at the CFLib.org site. I did a search on "file" and got 22 hits, the 3 of which is FileDateLastModified (http://www.cflib.org/udf.cfm?ID=126). It does the trick, though it uses a Com component and so will work only on Windows servers.
 
Indeed, since the problem remains unresolved without relying on that Com component, it may be tempting for some to ask if we could go ahead and post the solution as a new UDF using either my or Cheryl's approach. Sadly, we can't. UDF's (as of CF 5) don't allow use of CF tags inside them. We could post it to the Macromedia Dev Exchange. In fact, after writing all this, I thought to check there (doing a search for  "last modified"). I found a couple that people are charging 4 or 5 bucks for, but there are 2 others that are free:
 
 
Indeed, that second one is basically doing what mine did. But since it's only 2 lines, it's just as easy for anyone to make it a custom tag themselves with the 2 lines above without having to download it. :-)
 
/charlie
 
 
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On Behalf Of Cheryl Hill
Sent: Sunday, March 24, 2002 9:07 AM
To: [EMAIL PROTECTED]
Subject: RE: [CFTALKTor] Page Modified date

Putting the attached code in your "footer" file will output your date for the template the user is looking at.
 
<!--- CF CODE TO GENERATE LAST MODIFIED DATE --->
    <cfset thisPath = ExpandPath("*.*")>
    <cfset thisDirectory = GetDirectoryFromPath(thisPath)>
    <cfdirectory action="LIST" directory="#thisDirectory#" name="GetDir">
    <cfoutput query="GetDir">
        <cfif GetDir.name eq GetFileFromPath(GetBaseTemplatePath())>
            <cfset myString = GetDir.DateLastModified>
        </cfif>
    </cfoutput>
    <cfset mydate = createdate(mid(mystring,7,4),left(mystring,2),mid(mystring,4,2))>
    Last modified date: <cfoutput>#dateformat(mydate, "yyyy-mm-dd")#</cfoutput>
 <!--- END OF CF CODE TO GENERATE LAST MODIFIED DATE --->
 
Hope this helps.
 
Cheryl
 

Reply via email to