how to get the file extension

I have image files in a directory on one of our servers that are displayed as links on a web page...some of the image files are TIFF and some are coldfusion, how do I go thru the files in the directory and retrieve what the file extension is?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


Use <CFDIRECTORY to list the contents of a directory.

Then once you have the filenames, use ListLast(name, ".") to get the extension.

Plucka's solution is what you want.  Here's how to go about it:

<cfdirectory action="LIST" directory="c:\yourImageFolder\" name="MyFiles">

<cfoutput query="MyFiles">
      <cfif MyFiles.Type is "File">
            <cfif ListLast(, ".") is "jpg">
                  Do something
            <cfelse ListLast(, ".") is "tiff">
                  Do something else            
Even better, use switch

<cfdirectory action="LIST" directory="c:\yourImageFolder\" name="MyFiles">

<cfoutput query="MyFiles">
      <cfif MyFiles.Type is "File">
            <cfswitch expression="#ListLast(, '.')#">
                  <cfcase value="pdf">
                  <cfcase value="jpg,gif,bmp">
                  <cfcase value="xls,doc">
                        Office Documents
                        Something else
Active Protection takes the fight to cryptojacking

While there were several headline-grabbing ransomware attacks during in 2017, another big threat started appearing at the same time that didn’t get the same coverage – illicit cryptomining.

stacey14Author Commented:
is there any way of doing this without having to scan the entire directory? There are a ton of images in each directory that the app scans there a way to go directly to the image and get it's extension?
You could use the FILTER attribute of CFDIRECTORY.  For example:

<cfdirectory action="LIST" directory="c:\yourImageFolder\" name="MyJPGs" filter="*.jpg">

<cfdirectory action="LIST" directory="c:\yourImageFolder\" name="MyTIFFs" filter="*.tiff">
If the app is already scanning through the images, or has them in a database. Then you can just use this field to get your extension.
What I mean is that do you already know the filename from a database field or similar?
If you do then just do this

<cfset extension = listLast(fileNameField, ".") />

Where fileNameField is the name of the variable that has the filename.
stacey14Author Commented:
the name of the image is based off the PK field in a table, like 1056453...the path is built from this key as well...(confusing I know, thankfully I can't take credit for this method) if you have a document that's scanned into the system, it saves info about the document in a table, the Pk for that record is, let's say, 1056453, when it saves the image out to our server (the image path is \\server\imagedoc\) using part of the PK number for the next folder in the image path, so now our path is
\\server\imagedoc\6453\    and the PK for the record is the image name so the complete path is \\server\imagedoc\6453\1056453...most of these images are TIFF format but there are a few that are JPG...these are the only two types of images that are saved in this part of the the end user views these images is through a browser...on our doc page these doc title are links to the image files...I need to know what the extension is so I can correctly display the link.
You should be thoroughly confused by now...
How about something like this:

<cfset PKrecord = 1056453> <!--- Set just for example.  Your data would come from your DB --->

<cfif FileExists("\\server\imagedoc\#right(PKrecord,4)#\#PKrecord#.tiff")>
   File is something
<cfelseif FileExists("\\server\imagedoc\#right(PKrecord,4)#\#PKrecord#.jpg")>
  File is something else
   File is not image...error handling.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Servers

From novice to tech pro — start learning today.