Use CFDirectory to Filter Document Types by File Extension

Ben Nadel was sharing some good information he brought back from cf.Objective() where he showed how to use CFDirectory to filter using a single character wild card.

A reader commented that it would be nice to filter based on a limited set of file extensions. I responded simply that it could be done and gave a brief example.

I thought it would be worthy of mention here to show how I've used it in some of my applications.

As would be expected, we would attempt to separate filters by using a comma (,). However, this method does not work with CFDirectory. Awhile ago when I ran into this, I began trying other "OR" switches, and low and behold the pipe (|) did the trick.

I sometimes create a download directory in my web applications which often ends up being filled up with all kinds of documents that have been uploaded either on the front- or back-end. I wanted a way to just read what was in that directory without having to connect via FTP, etc.

I just created a simple index.cfm file that resides in the download directory (or folder). You can name it what you want, or even place it where you want, but would only need to update the directory read path, etc.

Here you go:


<cfsilent>
<!---------------------------------------------------------------------------------------------------------------

    Document:        /download/index.cfm
    Author:            steve@stephenwithington.com (Steve Withington / SJW) | www.stephenwithington.com
    Creation Date:    May 7, 2008
    
    Purpose:        Dynamically read a download directory.
    
    Input:            None.
    
    Revision Log:    
    07/25/2008 - sjw -    Updated the filter to catch 4-letter extensions from Microsoft,
                        I.e., .docx, .pptx, etc. Added a wildcard to grab all (*.doc*)


---------------------------------------------------------------------------------------------------------------->

</cfsilent>
<cfprocessingdirective suppresswhitespace="yes">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head>

<!--- clear browser cache --->
<cfheader name="expires" value="#dateformat(now(), 'ddd, dd mmm yyyy')# #timeformat(now(), 'HH:mm:ss tt')#" />
<cfheader name="pragma" value="no-cache" />
<cfheader name="cache-control" value="no-cache, no-store" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>download directory</title>
</head>

<body>

<!---
    Read files that only match the extensions listed in the filter.
--->


<cfset filters = "*.pdf|*.zip|*.doc*|*.ppt*|*.pps*|*.pot*|*.dot*|*.xls*|*.swf|*.rtf">

<cfdirectory
    directory="#ExpandPath( './' )#"
    name="dir"
    sort="name ASC"
    filter="#filters#" />


<!---
    If there are no files to display, then show a friendly message
--->

<cfif dir.recordcount eq 0>
Sorry, no documents are currently available. Please try again later.

<cfelse>

<table border="0" cellpadding="5" cellspacing="0">
<tr>
    <th>Name</th>
<th>Size (bytes)</th>
    <th>Size (kb/Mb)</th>
    <th>Modified</th>
</tr>

<!---
    Output the filtered files
--->

<cfoutput query="dir">
<tr bgcolor="###iif(currentrow MOD 2, DE('E0E8F0'),DE('FFFFFF'))#">
    <td><a href="#dir.name#">#dir.name#</a></td>
    <td align="right">#dir.size#</td>
    <!---
        Convert the filesize to either KB or MB for readability
    --->

    <td align="right"><cfif dir.size GT "1000000">#numberformat(dir.size*.000001, 9.99)#Mb
    <cfelse>#numberformat(dir.size*.001, 9)#kb</cfif></td>
    <td>#dateformat(dir.dateLastModified, "mm/dd/yyyy")# #timeformat(dir.dateLastModified, "long")#</td>
</tr>
</cfoutput>
</table>
<cfoutput>
<p>Document ##: #dir.recordcount#</p>
</cfoutput>
</cfif>
</body>
</html>
</cfprocessingdirective>

And that's it! Hope it helps you in your current or next ColdFusion project.

ColdFusion Developer Tools and Resources

Just when you thought you were out of luck because your Google search turned up useless links to your query, along comes Charlie Arehart's "Tools and Resources to Consider for CF Developers" where there's over 700 tools and resources in over 100 categories. Yeah, I know, it's nothing new (he started the list in 2002), but it's fairly new to me.

Ok, his list is so long that it could probably warrant it's very own search functionality! And Charlie, if you're reading this, I mean it in a good way.

If you develop in ColdFusion, you definitely need to bookmark the list. And if Charlie ever does begin to broadcast his list via RSS, I'll be one of the first to sign up!

Parker Lost a Tooth ...

My son lost his second tooth on Saturday (May 3rd) ... at the park ... somewhere. We can't find it. And for those who don't know, when Parker lost his first tooth, it was during the middle of the night, so we think he may have swallowed it. But don't tell him that, because he told me the tooth fairy came and took it.

What I thought was cool though, was at his school, when a child loses a tooth, they get to sit up front on a stool while everyone sings this little song:

Parker lost a tooth, let's all give a shout
It wiggled and it jiggled, and it skated all about.
When that tooth was ready, it jumped right out.
Parker has lost two teeth.

More Entries

© 2026, Stephen J. Withington, Jr.  |  Hosted by Hostek.com

Creative Commons License   |   This work is licensed under a Creative Commons Attribution 3.0 Unported License.