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?
 
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>
0
 
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
0
 
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>
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
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>
0
 
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?
0
 
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">
0
 
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.
0
 
PluckaCommented:
What I mean is that do you already know the filename from a database field or similar?
0
 
PluckaCommented:
If you do then just do this

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

Where fileNameField is the name of the variable that has the filename.
0
 
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...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.