?
Solved

how to get the file extension

Posted on 2006-03-21
13
Medium Priority
?
2,006 Views
Last Modified: 2013-12-20
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?
0
Comment
Question by:stacey14
  • 5
  • 3
  • 2
10 Comments
 
LVL 18

Expert Comment

by:Plucka
ID: 16253756
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
 
LVL 8

Expert Comment

by:pmascari
ID: 16254485
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
 
LVL 18

Expert Comment

by:Plucka
ID: 16254544
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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 

Author Comment

by:stacey14
ID: 16309772
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
 
LVL 8

Expert Comment

by:pmascari
ID: 16311102
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
 
LVL 18

Expert Comment

by:Plucka
ID: 16315127
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
 
LVL 18

Expert Comment

by:Plucka
ID: 16315137
What I mean is that do you already know the filename from a database field or similar?
0
 
LVL 18

Expert Comment

by:Plucka
ID: 16315163
If you do then just do this

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

Where fileNameField is the name of the variable that has the filename.
0
 

Author Comment

by:stacey14
ID: 16321479
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
 
LVL 8

Accepted Solution

by:
pmascari earned 100 total points
ID: 16321707
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
The purpose of this video is to demonstrate how to properly insert a Vimeo Video into a WordPress site or Blog. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp…
The purpose of this video is to demonstrate how to integrate Mailchimp with Facebook. This will be demonstrated using a Windows 8 PC. Mailchimp and Facebook will be used. Log into your Mailchimp account. : Click on your name. Go to Account Setti…
Suggested Courses
Course of the Month15 days, 16 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question