Created
July 19, 2025 14:14
-
-
Save DeveloperOfficeCom/b425404b2b4c767f748e122603ca6f37 to your computer and use it in GitHub Desktop.
FileNameSplit function for ColdFusion/Lucee - Split filename into base and extension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!--- | |
| FILENAMESPLIT Function | |
| Description: Splits a filename into base name and extension. The extension is everything from the | |
| last dot to the end, including the dot. Special cases like hidden files (.bashrc) are handled correctly. | |
| Parameters: | |
| - path: The file path or filename to split (required) | |
| Returns: Array with two elements [basename, extension] | |
| Usage: | |
| <cfset result = fileNameSplit("document.pdf")> | |
| <cfset result = fileNameSplit("/path/to/file.txt")> | |
| <cfset result = fileNameSplit("archive.tar.gz")> | |
| Examples: | |
| fileNameSplit("document.pdf") returns ["document", ".pdf"] | |
| fileNameSplit("archive.tar.gz") returns ["archive.tar", ".gz"] | |
| fileNameSplit("README") returns ["README", ""] | |
| fileNameSplit(".bashrc") returns [".bashrc", ""] | |
| fileNameSplit("file.") returns ["file.", ""] | |
| Author: DeveloperOffice.com | |
| Language: ColdFusion/Lucee | |
| License: MIT (https://opensource.org/licenses/MIT) | |
| Version: 1.0 | |
| ---> | |
| <cffunction name="fileNameSplit" returntype="array" output="false"> | |
| <cfargument name="path" type="string" required="true"> | |
| <cfset var result = []> | |
| <cfset var filename = ""> | |
| <cfset var lastSlashPos = 0> | |
| <cfset var dotPos = 0> | |
| <cfset var basename = ""> | |
| <cfset var extension = ""> | |
| <!--- Extract just the filename from the path -----> | |
| <cfset lastSlashPos = max( | |
| lastIndexOf(arguments.path, "/"), | |
| lastIndexOf(arguments.path, "\") | |
| )> | |
| <cfif lastSlashPos GT 0> | |
| <cfset filename = mid(arguments.path, lastSlashPos + 1, len(arguments.path) - lastSlashPos)> | |
| <cfset var pathPrefix = left(arguments.path, lastSlashPos)> | |
| <cfelse> | |
| <cfset filename = arguments.path> | |
| <cfset var pathPrefix = ""> | |
| </cfif> | |
| <!--- Find the last dot in the filename -----> | |
| <cfset dotPos = lastIndexOf(filename, ".")> | |
| <!--- Handle special cases -----> | |
| <cfif dotPos EQ 0> | |
| <!--- No dot found -----> | |
| <cfset basename = arguments.path> | |
| <cfset extension = ""> | |
| <cfelseif dotPos EQ 1> | |
| <!--- File starts with dot (hidden file like .bashrc) -----> | |
| <cfset basename = arguments.path> | |
| <cfset extension = ""> | |
| <cfelseif dotPos EQ len(filename)> | |
| <!--- File ends with dot -----> | |
| <cfset basename = arguments.path> | |
| <cfset extension = ""> | |
| <cfelse> | |
| <!--- Normal case: split at the last dot -----> | |
| <cfif lastSlashPos GT 0> | |
| <!--- Include the path prefix in basename -----> | |
| <cfset basename = pathPrefix & left(filename, dotPos - 1)> | |
| <cfelse> | |
| <cfset basename = left(filename, dotPos - 1)> | |
| </cfif> | |
| <cfset extension = mid(filename, dotPos, len(filename) - dotPos + 1)> | |
| </cfif> | |
| <!--- Return as array -----> | |
| <cfset arrayAppend(result, basename)> | |
| <cfset arrayAppend(result, extension)> | |
| <cfreturn result> | |
| </cffunction> | |
| <!--- Helper function for finding last occurrence of a string -----> | |
| <cffunction name="lastIndexOf" returntype="numeric" output="false" access="private"> | |
| <cfargument name="string" type="string" required="true"> | |
| <cfargument name="substring" type="string" required="true"> | |
| <cfset var pos = 0> | |
| <cfset var lastPos = 0> | |
| <cfset var searchFrom = 1> | |
| <cfloop condition="true"> | |
| <cfset pos = find(arguments.substring, arguments.string, searchFrom)> | |
| <cfif pos EQ 0> | |
| <cfbreak> | |
| </cfif> | |
| <cfset lastPos = pos> | |
| <cfset searchFrom = pos + 1> | |
| </cfloop> | |
| <cfreturn lastPos> | |
| </cffunction> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment