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.

Comments
Updated the code example to filter for both 3- and 4-letter Microsoft extensions (i.e., *.doc*|*.ppt*).

Thank you Matthew Abbott. http://www.bennadel.com/index.cfm?dax=blog:1219.vi...
# Posted By Stephen Withington | 7/25/08 1:45 PM
Yes, it did help! Thanks!
# Posted By Nic | 9/11/08 1:53 PM

© 2014, Stephen J. Withington, Jr.  |  BlogCFC was created by Raymond Camden – Version 5.9.004

Creative Commons License  |  This work is licensed under a Creative Commons Attribution 3.0 Unported License.  |  Hosted by Hostek.com