tags:

views:

783

answers:

4

for example, I want just the "filename" of a file in a field. Say I have myimage.jpg I only want to display "myimage" How do I get just that?

+5  A: 

Use the List functions to your advantage.

<cfset FileName = ListDeleteAt(FileFullName, ListLen(FileFullName, "."), ".")>

Be aware that this only works for file names that actually have a file extension (that is defined as the thing after the last dot). To make it safer, the following is better:

<cfset ExtensionIndex = ListLen(FileFullName, ".")>
<cfif ExtensionIndex gt 1>
  <cfset FileExt  = ListGetAt(ExtensionIndex , ".")>
  <cfset FileName = ListDeleteAt(FileFullName, ExtensionIndex, ".")>
<cfelse>
  <cfset FileExt  = "">
  <cfset FileName = FileFullName>
</cfif>

To complicate things a bit further: There may be files that start with a dot. There may be file names that contain many adjacent dots. List functions return wrong results for them, as they ignore empty list elements. There may also be files that have dots, but no extension. These can only be handled if you provide an extension white list: ListFindNoCase(FileExt, "doc,xls,ppt,jpg"). If you want to account for all of this, you probably need to resign to a reguar expression:

<cfset FileExtRe = "(?:\.(?:doc|xls|ppt|jpg))?$">
<cfset FileName  = REReplaceNoCase(FileExtRe, FileFullName, "")>

To split file name from path, ColdFusion provides distinct functions that also handle platform differences: GetFileFromPath() and GetDirectoryFromPath()

Tomalak
+1  A: 

So you first need to find the position of the last fullstop (there could be more than one fullstop in the full filename). I don't think Coldfusion has a find function that works backwards, so reverse the string first:

<cfset Position = Find(".", Reverse(FullFileName))>

If that returns zero then you don't have a fullstop in the file name, so handle appropriately. Else ...

<cfset Filename = Left(FullFileName, Len(FullFileName) - Position>
Guy C
BTW: As of ColdFusion MX, you have access to the underlying Java classes. Calling 'FileFullName.lastIndexOf(".")' is a lot more efficient than doing 'Find(".", Reverse(FileFullName))'.
Tomalak
+2  A: 

The current accepted solution will not work for a file that does not contain an extension.

You can solve this by using a regular expression to strip the extension only if it exists:

<cfset FileName = rereplace( FullFileName , '\.[^.]+$' , '' ) />


This might still not be perfect - you might have a file that has a . but it isn't considered an extension - you can solve this either by using a list of known extensions to strip, or by limiting how long an extension you will accept (e.g. upto 5):

<cfset FileName = rereplace( FullFileName , '\.(jpg|png|gif|bmp)$' , '' ) />
<cfset FileName = rereplace( FullFileName , '\.[^.]{1,5}$' , '' ) />
Peter Boughton
+3  A: 

Tomalak's answer is good, but this can get tricky. Given a file named "mydoc.ver1.doc" (a valid Windows file name) which is the filename and which is the extension? What if there is a filepath?

You can still leverage the list functions to your advantage, however, even in these scenarios.

You can easily parse out the file from the path with

fullFileName=listLast(fieldname,"\/")

If you assume the filename is everything before the dot, then

theFileName=listFirst(fullFileName,".")

will work.

If you want to ensure that you get everything but what's after the last period, then a little trickery is needed, but not much. There not a listAllButLast() function (although such a thing might exists on CFLIB.org) but there are two ways I can think of to get what you're after.

fileName=reverse(listRest(reverse(fullFileName),"."))

or

fileName=listDeleteAt(fullFileName,listLen(fullFileName,"."),".")

As with Tomalak's suggestion, however, this will break down on a filename that lacks an extension. Wrapping this in a <cfif listLen(fullFileName,".") GT 1> will account for that.

Al Everett