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:


    Document:        /download/index.cfm
    Author:   (Steve Withington / SJW) |
    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*)


<cfprocessingdirective suppresswhitespace="yes">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""><html xmlns="">

<!--- 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>


    Read files that only match the extensions listed in the filter.

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

    directory="#ExpandPath( './' )#"
    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.


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

    Output the filtered files

<cfoutput query="dir">
<tr bgcolor="###iif(currentrow MOD 2, DE('E0E8F0'),DE('FFFFFF'))#">
    <td><a href=""></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>
<p>Document ##: #dir.recordcount#</p>

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

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

Thank you Matthew Abbott.
# Posted By Stephen Withington | 7/25/08 1:45 PM
Yes, it did help! Thanks!
# Posted By Nic | 9/11/08 1:53 PM
Note that no spaces are allowed between the filter and the delimiters.
# Posted By Jack Archer | 2/17/17 2:17 PM

© 2018, 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