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 JPG...in coldfusion, how do I go thru the files in the directory and retrieve what the file extension is?
stacey14Asked:
Who is Participating?

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

x
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.

PluckaCommented:
stacey14,

Use <CFDIRECTORY to list the contents of a directory.

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

Regards
Plucka
pmascariCommented:
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(MyFiles.name, ".") is "jpg">
                  Do something
            <cfelse ListLast(MyFiles.name, ".") is "tiff">
                  Do something else            
            </cfif>
      </cfif>
</cfoutput>
PluckaCommented:
Even better, use switch

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

<cfoutput query="MyFiles">
      <cfif MyFiles.Type is "File">
            <cfswitch expression="#ListLast(MyFiles.name, '.')#">
                  <cfcase value="pdf">
                        PDF
                  </cfcase>
                  <cfcase value="jpg,gif,bmp">
                        Images
                  </cfcase>
                  <cfcase value="xls,doc">
                        Office Documents
                  </cfcase>
                  <cfdefaultcase>
                        Something else
                  </cfdefaultcase>
            </cfswitch>
      </cfif>
</cfoutput>
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 thru...is there a way to go directly to the image and get it's extension?
pmascariCommented:
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">
PluckaCommented:
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.
PluckaCommented:
What I mean is that do you already know the filename from a database field or similar?
PluckaCommented:
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)...so 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 server...how 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...
pmascariCommented:
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 Tiff...do something
<cfelseif FileExists("\\server\imagedoc\#right(PKrecord,4)#\#PKrecord#.jpg")>
  File is Jpg...do something else
<cfelse>
   File is not image...error handling.
</cfif>

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.