Learning ColdFusion 8: CFDirectory Improvements

Posted August 1, 2007 at 8:10 AM

Tags: ColdFusion

In addition to all the new tags and functions that ColdFusion 8 has added to the already amazing language that we have come to love, ColdFusion 8 has also made very nice improvements to the existing feature set. Among those are updates to the CFDiretory tag. While the ColdFusion CFDirectory can Create, Rename, Delete, and List directories, the ColdFusion 8 updates were made only to the listing action.

New to the CFDirectory tag are the attributes ListInfo and Type. ListInfo determines what columns are returned in the CFDirectory query object. The valid values for this attribute are "all", the default, and "name". When the ListInfo attribute is set to "all", all the directory and file names are returned in the query just like a CFMX 7 CFDirectory tag. So for instance, a ColdFusion 7 CFDirectory tag:

 Launch code in new window » Download code as text file »

  • <!---
  • List out all directories and images in
  • the data directory.
  • --->
  • <cfdirectory
  • action="list"
  • directory="#ExpandPath( './data/' )#"
  • recurse="true"
  • name="qFile"
  • />
  •  
  • <!--- Dump out file query. --->
  • <cfdump
  • var="#qFile#"
  • label="CFDirectory - CFMX 7 Compliant"
  • />

... will return a query that looks like this:


 
 
 

 
ColdFusion MX7 Style CFDirectory Query CFDump  
 
 
 

The above query is the equivalent of running the CFDirectory with ListInfo="all"; however, now, if we run the same CFDirectory tag, but this time, tell it to only list out the "name":

 Launch code in new window » Download code as text file »

  • <!---
  • List out all directories and images in the data
  • directory. This time, however, only list out the
  • names of the files and directories.
  • --->
  • <cfdirectory
  • action="list"
  • directory="#ExpandPath( './data/' )#"
  • recurse="true"
  • listinfo="name"
  • name="qFile"
  • />
  •  
  • <!--- Dump out file query. --->
  • <cfdump
  • var="#qFile#"
  • label="CFDirectory - Name Only"
  • />

... we will get a query that looks like this:


 
 
 

 
ColdFusion 8 CFDirectory Query CFDump With ListInfo = Name  
 
 
 

Notice here that we are getting only the Name column of the query. However, also notice that the data in the Name column is not exactly the same of the name column in the CFMX 7 compliant query. While the CFMX 7 CFDirectory query put only the file name or directory name in the Name column, the ColdFusion 8 CFDirectory name-only query basically includes the path of the files relative to the root directory we are targeting.

This demo was run during the ColdFusion 8 beta. I have not had a chance to install the official ColdFusion 8 Developer edition yet. It is possible that this may have changed in the official release. My guess, however, is that this was a design decision that was made to make the name-only query more useful. Afteral, a name-only query that had non sense of path structure would not be all that useful with any kind of recursive CFDirectory action. And, think about it - if it wasn't recursive, then you wouldn't have the directory structure at all since all files would be in the root of the target directory. Good call Adobe!

Listing only the name column can have significant performance increases and should be used whenever you don't need the rest of the information returned in CFDirectory.

The Type attribute of the ColdFusion 8 CFDirectory tag determines what type of items are returned in the resultant query. The valid values for this attribute are "file", "dir", and "all. All, the default value, means that both files and directories will be returned. The value "File" means that only files will be returned in the query. The value "Dir" means only directories will be returned in the query.

Running this code with the Type="file" attribute:

 Launch code in new window » Download code as text file »

  • <!--- List out all files in the data directory. --->
  • <cfdirectory
  • action="list"
  • directory="#ExpandPath( './data/' )#"
  • recurse="true"
  • listinfo="name"
  • type="file"
  • name="qFile"
  • />
  •  
  • <!--- Dump out file query. --->
  • <cfdump
  • var="#qFile#"
  • label="CFDirectory - Files Only"
  • />

... we will get a query that looks like this:


 
 
 

 
ColdFusion 8 CFDirectory Query CFDump With ListInfo = Name And Type = File  
 
 
 

Notice that the "images" and "documents" entries we saw earlier are no long present. These were directories and are being excluded from the resultant query. To me, this is an awesome feature because it saves us from having to run a ColdFusion query of queries, checking Type columns during output, or using Filter attribute hacks (*.*).

Similary, running this code with the Type="dir" attribute:

 Launch code in new window » Download code as text file »

  • <!--- List out all directories in the data directory. --->
  • <cfdirectory
  • action="list"
  • directory="#ExpandPath( './data/' )#"
  • recurse="true"
  • listinfo="name"
  • type="dir"
  • name="qFile"
  • />
  •  
  • <!--- Dump out file query. --->
  • <cfdump
  • var="#qFile#"
  • label="CFDirectory - Directories Only"
  • />

... we will get a query that looks like this:


 
 
 

 
ColdFusion 8 CFDirectory Query CFDump With ListInfo = Name And Type = Dir  
 
 
 

Notice that now, the only entries we have are the two directories, "documents" and "images".

ColdFusion 8 is so awesome, not only for all the new tags and functions, but also for all these little updates to existing features that are going to make our lives as developers so much easier.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page


You Might Also Be Interested In:



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Aug 1, 2007 at 9:28 AM // reply »
92 Comments

Just a heads up Ben that listinfo is available in CF7 but was an undocumented feature. I'm not sure though if its available in any earlier release than CF7. It's a great attribute if you just need filenames since thats all I've ever used cfdirectory for so the performance boost is very nice.


Aug 1, 2007 at 9:46 AM // reply »
56 Comments

So they still don't have the created date information. I think this was request ages ago.


Aug 1, 2007 at 10:10 AM // reply »
6,515 Comments

@Javier,

Yeah, I have seen it used unofficially in ColdFusion MX7, but I never used it personally. But, now that it is documented and publicly known, I am sure I will start to make use of it.

@Tony,

Sorry :(


Aug 2, 2007 at 8:55 AM // reply »
16 Comments

One more thing that I was expecting to be there in CF8 is the folder/directory size

but it seems that still I need to use java to do that.
:(


Sep 27, 2007 at 11:41 AM // reply »
5 Comments

Awesome post Ben!

I tested this in CF8 and my cfdirectory query is giving me just the filename, rather than the relative path to the file from the calling page like in your example... it's behaving just like you showed CF7 used to handle the "name" at the top of your post. And this is with listinfo="name" set in the cfdirectory tag.

Is this just happening for me, or have you had a chance to test in the official version of CF8?

Cheers, Fitz


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 20, 2009 at 5:38 PM
Learning ColdFusion 8: CFImage Part I - Reading And Writing Images
Hi Ben, Great article. I've been looking around to see if ColdFusion image engine can programatically create the following "wrap around" effect: http://www.creativepro.com/article/photoshop-s-she ... read »
Nov 20, 2009 at 5:35 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Dave: I talked to Gert he suggested: <cfhttp method="get" url="http://{some cf website}" result="stuff" addtoken="yes" /> Note the addition of cfhttp attribute addtoken. That should persist y ... read »
Nov 20, 2009 at 5:23 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Todd, Ahh, gotcha, yeah that makes sense. ... read »
Nov 20, 2009 at 5:17 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
Ben, sorry if I didn't make this clear. You can make it work like that if you want, just put <cfset session.foo = 1> (and <cfset application.foo = 1>) in your OnRequestStart() and it reve ... read »
Nov 20, 2009 at 5:07 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Todd, I have seen tidbits about the way Railo handles session. I can understand that it lazy-loads sessions, but I also think that I might make some things more complicated. For example, often tim ... read »
Nov 20, 2009 at 4:53 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
Ben, you can ramp up the security by turning on J2EE session which gives you a third set of numbers other than CFID/CFTOKEN. There's a reason why ACF put this in place (other than just session replic ... read »
Nov 20, 2009 at 4:52 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
Case in point, Ben, you may not be aware of this, but in Railo - OnApplicationStart() & OnSessionStart() act differently than in ACF. ACF does: OnApplicationStart (1st hit) OnSessionStart (1st and e ... read »
Nov 20, 2009 at 4:46 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Todd, That's understandable. I am not sure if this really leaves any more security holes than the fact that using old cookie-based CFID / CFTOKEN values will create a new session using the old CFI ... read »