User-Defined Function (UDF) Using CFSwitch and CFIF to Dynamically Generate a Document Icon

Have you ever needed (or wanted) to dynamically generate an icon that represents the document type in your document management system? Maybe you just didn't have the time (or want to take the time) to write a user-defined function (UDF) that would accomplish this. Well, have no fear because I've already started the process for you.

In order to use the UDF, you will need a couple of items:

  • A database with a table named "tblDocuments"
  • The table should have at least two columns:
    1. DocID (the primary key)
    2. DocFilename (contains full name of the document including extension)
  • Document icons stored in the "/assets/icons/" directory
  • The UDF listed below
    • Update <cfquery name="getDocType" datasource="#APPLICATION.dsn#"> with your own datasource

 


<!------------------------------------------------------------------------------------------

    Document:        /extensions/includes/getIconFromExt.cfm
    Author:            steve@stephenwithington.com (sjw)
    Creation Date:    May 7, 2008
    Copyright:        (c) 2008 Stephen J. Withington, Jr. | http://www.stephenwithington.com
    
    Purpose:        User-Defined Function (UDF) which queries tblDocuments to obtain
                    file extension to generate icon to use.

    METHOD(s)/VAR:    1) getIconResult    |    returnIcon
    
    Example Usage:    <img src="/assets/icons/#getIconResult(DocID)#" border="0" />

    ASSUMPTIONS:
                        a) icons listed in the CFSwitch below are stored in
                            the directory '/assets/icons/'
                        b) database with a table named tblDocuments
                        c) at least two (2) columns:
                            1) DocID (primary key)
                            2) DocFilename (contains full name of the document including extension)

    NOTES:            This UDF does NOT include all available document types. If another
                    document type is needed, simply add the icon to the "icons" folder and
                    create a new CFCase. If the three (3) right-most characters of the extension
                    are already in use, then simply nest a CFIF statement similar to DOTM and POTM.
    
    Revision Log:    
    MM/DD/YYYY - sjw - comments.

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

<cffunction name="getIconResult" access="public" returntype="string" output="no">

    <!---
        DocID is required
    --->

    <cfargument name="DocID" type="numeric" required="yes" />

    <!---
        Scope variable
    --->

    <cfset returnIcon = "" />

    <!---
        perform the query
    --->

    <cfquery name="getDocType" datasource="#APPLICATION.dsn#">
        SELECT DocID, DocFilename
        FROM dbo.tblDocuments
        WHERE DocID = <cfqueryparam value="#val(ARGUMENTS.DocID)#" cfsqltype="cf_sql_numeric" />
    </cfquery>

    <!---
        Check the file extension and assign an icon and ALT tag
    --->

    <cfswitch expression="#right(getDocType.DocFilename, 3)#">

        <!--- DOC --->
     <cfcase value="doc">
            <cfset returnIcon = "doc.gif" & """ alt=""DOC" />
     </cfcase>

        <!--- DOCM --->
     <cfcase value="ocm">
            <cfset returnIcon = "docm.gif" & """ alt=""DOCM" />
     </cfcase>

        <!--- DOCX --->
     <cfcase value="ocx">
            <cfset returnIcon = "docx.gif" & """ alt=""DOCX" />
     </cfcase>

        <!--- DOTM and POTM --->
<!--- warning: this is the same as POTM (powerpoint template master for office 2007) --->
     <cfcase value="otm">
     <cfif right(getDocType.DocFilename, 4) EQ "dotm">
                <cfset returnIcon = "dotm.gif" & """ alt=""DOTM" />
            <cfelse>
                <cfset returnIcon = "potm.gif" & """ alt=""DOTM" />
     </cfif>
     </cfcase>

        <!--- DOTX and POTX --->
<!--- warning: this is the same as POTX (powerpoint template for office 2007) --->
     <cfcase value="otx">
     <cfif right(getDocType.DocFilename, 4) EQ "dotx">
                <cfset returnIcon = "dotx.gif" & """ alt=""DOTX" />
            <cfelse>
                <cfset returnIcon = "potx.gif" & """ alt=""POTX" />
     </cfif>
     </cfcase>

        <!--- HTM --->
     <cfcase value="htm">
            <cfset returnIcon = "htm.gif" & """ alt=""HTM" />
     </cfcase>

        <!--- HTM --->
     <cfcase value="tml">
            <cfset returnIcon = "htm.gif" & """ alt=""HTML" />
     </cfcase>

        <!--- JPG--->
     <cfcase value="jpg">
            <cfset returnIcon = "jpg.gif" & """ alt=""JPG" />
     </cfcase>

        <!--- JPEG --->
     <cfcase value="peg">
            <cfset returnIcon = "jpg.gif" & """ alt=""JPEG" />
     </cfcase>

        <!--- MDB --->
     <cfcase value="mdb">
            <cfset returnIcon = "mdb.gif" & """ alt=""MDB" />
     </cfcase>

        <!--- MOV --->
     <cfcase value="mov">
            <cfset returnIcon = "mov.gif" & """ alt=""MOV" />
     </cfcase>

        <!--- PDF --->
     <cfcase value="pdf">
            <cfset returnIcon = "pdf.gif" & """ alt=""PDF" />
     </cfcase>

        <!--- PPAM --->
     <cfcase value="pam">
            <cfset returnIcon = "ppam.gif" & """ alt=""PPAM" />
     </cfcase>

        <!--- PPS --->
     <cfcase value="pps">
            <cfset returnIcon = "pps.gif" & """ alt=""PPS" />
     </cfcase>

        <!--- PPSM --->
     <cfcase value="psm">
            <cfset returnIcon = "ppsm.gif" & """ alt=""PPSM" />
     </cfcase>

        <!--- PPSX --->
     <cfcase value="psx">
            <cfset returnIcon = "ppsx.gif" & """ alt=""PPSX" />
     </cfcase>

        <!--- PPT --->
     <cfcase value="ppt">
            <cfset returnIcon = "ppt.gif" & """ alt=""PPT" />
     </cfcase>

        <!--- PPTM --->
     <cfcase value="ptm">
            <cfset returnIcon = "pptm.gif" & """ alt=""PPTM" />
     </cfcase>

        <!--- PPTX --->
     <cfcase value="ptx">
            <cfset returnIcon = "pptx.gif" & """ alt=""PPTX" />
     </cfcase>

        <!--- RTF --->
     <cfcase value="rtf">
            <cfset returnIcon = "rtf.gif" & """ alt=""RTF" />
     </cfcase>

        <!--- TXT --->
     <cfcase value="txt">
            <cfset returnIcon = "rtf.gif" & """ alt=""RTF" />
     </cfcase>

        <!--- WMV --->
     <cfcase value="wmv">
            <cfset returnIcon = "wmv.gif" & """ alt=""WMV" />
     </cfcase>

        <!--- XLAM --->
     <cfcase value="lam">
            <cfset returnIcon = "xlam.gif" & """ alt=""XLAM" />
     </cfcase>

        <!--- XLS --->
     <cfcase value="xls">
            <cfset returnIcon = "xls.gif" & """ alt=""XLS" />
     </cfcase>

        <!--- XLSB --->
     <cfcase value="lsb">
            <cfset returnIcon = "xlsb.gif" & """ alt=""XLSB" />
     </cfcase>

        <!--- XLSM --->
     <cfcase value="lsm">
            <cfset returnIcon = "xlsm.gif" & """ alt=""XLSM" />
     </cfcase>

        <!--- XLTM --->
     <cfcase value="ltm">
            <cfset returnIcon = "xltm.gif" & """ alt=""XLTM" />
     </cfcase>

        <!--- XLSX --->
     <cfcase value="lsx">
            <cfset returnIcon = "xlsx.gif" & """ alt=""XLSX" />
     </cfcase>

        <!--- ZIP--->
     <cfcase value="zip">
            <cfset returnIcon = "zip.gif" & """ alt=""ZIP" />
     </cfcase>

        <!--- DEFAULT --->
     <cfdefaultcase>
            <cfset returnIcon = "rtf.gif" & """ alt=""" />
     </cfdefaultcase>

    </cfswitch>

    <cfreturn returnIcon />
</cffunction>

You can either add missing document icons or even remove unnecessary document icons to/from the UDF if needed.

In order to use the UDF, simply include the file using <cfinclude template="/extensions/includes/getIconFromExt.cfm" /> (update the template path to where you place the UDF in your site) in the page where you want to use this. Then in your query output, place <img src="/assets/icons/#getIconResult(DocID)#" border="0" /> next to the document title, etc.

Did this help you? Please let me know.

Use ColdFusion to Find Out What Directory or Folder You're In

Have you ever wanted to find out what folder or directory a particular page or template resides in, but NOT necessarily the entire path to the page? Well, Raymond Camden recently blogged about it, so I know of at least one other developer (including myself) who has asked that question so I thought it would be worthy of mention here.

Ray mentions a number of extremely useful functions and the person who submitted the question was actually quite close to answering their own question (just as I too find myself on so many occaisions).

Let's say you have a file "index.cfm" which resides at "www.example.com/folder/" which as it might turn out translates to "D:\inetpub\example\folder\". Maybe you just want to return "folder" but don't need anything else. Let's try a couple of different options to see what happens.


<cfset thisPath = ExpandPath("*.*") />
<cfoutput>#getDirectoryFromPath(thisPath)#</cfoutput>

This produces "D:\inetpub\example\folder\"

Well, that's not what we wanted is it? Let's try another way.


<cfset thisDirectory = ExpandPath(".") />
<cfoutput>#getDirectoryFromPath(thisDirectory)#</cfoutput>

This produces "D:\inetpub\example\"

Hmm, while this isn't what we wanted, it is kind of interesting. But we're not quite there, so lets try another method.


<cfset thisDirectory = ExpandPath(".") />
<cfoutput>#getFileFromPath(thisDirectory)#</cfoutput>

Aha! This produces "folder" just like we wanted!

Does this help you? Let me know.

Nine Inch Nails New Album - The Slip - For FREE!

If you're a NIN fan, then you'll be excited to know that Trent Reznor is giving away his new album, The Slip as a way to thank his fans for their "continued and loyal support over the years." Not just a track, which is already free at Facebook, (I first heard about it from Raymond Camden), but the ENTIRE album!

I, for one, am blown away by his generosity. Especially amidst all of the lawsuits and publicity concerning illegal downloading and file-sharing.

I personally would have been willing to pay for my album, but this has given me an idea. Maybe if each person who receives the album would donate say $5 or $10 to a local food bank, or charity, then we could perpetuate his kindness.

Thank you Trent.

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.