如何在ColdFusion中获取字符串的内容减去扩展名?

时间:2022-06-24 07:37:27

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?

例如,我只想要字段中文件的“文件名”。说我有myimage.jpg我只想显示“myimage”我怎么得到那个?

5 个解决方案

#1


8  

Use the List functions to your advantage.

使用List功能对您有利。

<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:

使事情进一步复杂化:可能存在以点开头的文件。可能存在包含许多相邻点的文件名。列表函数为它们返回错误的结果,因为它们忽略空列表元素。也可能有文件有点,但没有扩展名。只有在您提供扩展名白名单时才能处理这些内容:ListFindNoCase(FileExt,“doc,xls,ppt,jpg”)。如果您想要考虑所有这些,您可能需要辞职到一个reguar表达式:

<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()

要从路径中拆分文件名,ColdFusion提供了可处理平台差异的不同功能:GetFileFromPath()和GetDirectoryFromPath()

#2


6  

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?

Tomalak的答案很好,但这可能会变得棘手。给定一个名为“mydoc.ver1.doc”的文件(一个有效的Windows文件名),它是文件名,是扩展名?如果有文件路径怎么办?

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 is not a listAllButLast() function (although such a thing might exist on CFLIB.org) but there are two ways I can think of to get what you're after.

如果你想确保你得到的东西除了最后一段时间之后的东西,那么需要一点点诡计,但并不多。没有listAllButLast()函数(尽管CFLIB.org上可能存在这样的事情),但我可以通过两种方式来获得你想要的东西。

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.

然而,与Tomalak的建议一样,这将打破缺少扩展名的文件名。将此包装在 中将解释这一点。

#3


3  

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):

这可能仍然不完美 - 你可能有一个文件有一个。但它不被视为扩展 - 您可以通过使用已剥离的已知扩展列表或通过限制您将接受的扩展(例如,最多5)来解决此问题:

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

#4


1  

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:

所以你首先需要找到最后一个fullstop的位置(完整文件名中可能有多个fullstop)。我不认为Coldfusion有一个向后工作的find函数,所以首先反转字符串:

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

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

如果返回零,则文件名中没有fullstop,因此请适当处理。否则......

<cfset Filename = Left(FullFileName, Len(FullFileName) - Position>

#5


1  

As of ColdFusion 9+ (perhaps earlier, but I can't verify), the Apache Commons library was included. Within that is org.apache.commons.io.FilenameUtils. You can utilize methods within the cut down on the amount of operations needed in CF to get the same (or similar) results.

从ColdFusion 9+开始(可能更早,但我无法验证),包含了Apache Commons库。其中包括org.apache.commons.io.FilenameUtils。您可以利用减少CF中所需操作量的方法来获得相同(或类似)的结果。

filepath = "some/dir/archive.tar.gz";
oUtils = createObject("java", "org.apache.commons.io.FilenameUtils");

writeDump(oUtils.getFullPath(filepath)); // "some/dir/"
writeDump(oUtils.getName(filepath)); // "archive.tar.gz"
writeDump(oUtils.getBaseName(filepath)); // "archive.tar"
writeDump(oUtils.getExtension(filepath)); // "gz"
writeDump(oUtils.getPath(filepath)); // "some/dir/"
writeDump(oUtils.getPathNoEndSeparator(filepath)); // "some/dir"

#1


8  

Use the List functions to your advantage.

使用List功能对您有利。

<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:

使事情进一步复杂化:可能存在以点开头的文件。可能存在包含许多相邻点的文件名。列表函数为它们返回错误的结果,因为它们忽略空列表元素。也可能有文件有点,但没有扩展名。只有在您提供扩展名白名单时才能处理这些内容:ListFindNoCase(FileExt,“doc,xls,ppt,jpg”)。如果您想要考虑所有这些,您可能需要辞职到一个reguar表达式:

<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()

要从路径中拆分文件名,ColdFusion提供了可处理平台差异的不同功能:GetFileFromPath()和GetDirectoryFromPath()

#2


6  

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?

Tomalak的答案很好,但这可能会变得棘手。给定一个名为“mydoc.ver1.doc”的文件(一个有效的Windows文件名),它是文件名,是扩展名?如果有文件路径怎么办?

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 is not a listAllButLast() function (although such a thing might exist on CFLIB.org) but there are two ways I can think of to get what you're after.

如果你想确保你得到的东西除了最后一段时间之后的东西,那么需要一点点诡计,但并不多。没有listAllButLast()函数(尽管CFLIB.org上可能存在这样的事情),但我可以通过两种方式来获得你想要的东西。

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.

然而,与Tomalak的建议一样,这将打破缺少扩展名的文件名。将此包装在 中将解释这一点。

#3


3  

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):

这可能仍然不完美 - 你可能有一个文件有一个。但它不被视为扩展 - 您可以通过使用已剥离的已知扩展列表或通过限制您将接受的扩展(例如,最多5)来解决此问题:

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

#4


1  

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:

所以你首先需要找到最后一个fullstop的位置(完整文件名中可能有多个fullstop)。我不认为Coldfusion有一个向后工作的find函数,所以首先反转字符串:

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

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

如果返回零,则文件名中没有fullstop,因此请适当处理。否则......

<cfset Filename = Left(FullFileName, Len(FullFileName) - Position>

#5


1  

As of ColdFusion 9+ (perhaps earlier, but I can't verify), the Apache Commons library was included. Within that is org.apache.commons.io.FilenameUtils. You can utilize methods within the cut down on the amount of operations needed in CF to get the same (or similar) results.

从ColdFusion 9+开始(可能更早,但我无法验证),包含了Apache Commons库。其中包括org.apache.commons.io.FilenameUtils。您可以利用减少CF中所需操作量的方法来获得相同(或类似)的结果。

filepath = "some/dir/archive.tar.gz";
oUtils = createObject("java", "org.apache.commons.io.FilenameUtils");

writeDump(oUtils.getFullPath(filepath)); // "some/dir/"
writeDump(oUtils.getName(filepath)); // "archive.tar.gz"
writeDump(oUtils.getBaseName(filepath)); // "archive.tar"
writeDump(oUtils.getExtension(filepath)); // "gz"
writeDump(oUtils.getPath(filepath)); // "some/dir/"
writeDump(oUtils.getPathNoEndSeparator(filepath)); // "some/dir"