Created
July 19, 2025 11:26
-
-
Save DeveloperOfficeCom/ec6cc76622422ca8149ffc5acc8031b0 to your computer and use it in GitHub Desktop.
PathExists function for ColdFusion/Lucee - Check if path exists as file or directory
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
| <!--- | |
| PATHEXISTS Function | |
| Description: Checks if a path exists (either as a file or directory), similar to Python's os.path.exists(). This is a convenience function that combines fileExists() and directoryExists() into a single check. | |
| Parameters: | |
| - path: The path to check (required) | |
| Returns: Boolean - true if the path exists as either a file or directory, false otherwise | |
| Usage: | |
| <cfset exists = pathexists("/path/to/file.txt")> | |
| <cfset exists = pathexists("/path/to/directory")> | |
| <cfset exists = pathexists("relative/path")> | |
| Examples: | |
| pathexists("/tmp") returns true (directory exists) | |
| pathexists("/etc/hosts") returns true (file exists) | |
| pathexists("/nonexistent/path") returns false | |
| pathexists("") returns false (empty path) | |
| Author: DeveloperOffice.com | |
| Language: ColdFusion/Lucee | |
| License: MIT (https://opensource.org/licenses/MIT) | |
| Version: 1.0 | |
| ---> | |
| <cffunction name="pathexists" returntype="boolean" output="false"> | |
| <cfargument name="path" type="string" required="true"> | |
| <!--- Handle empty or null path -----> | |
| <cfif NOT len(trim(arguments.path))> | |
| <cfreturn false> | |
| </cfif> | |
| <!--- Check if path exists as file or directory -----> | |
| <cftry> | |
| <!--- First check if it's a file -----> | |
| <cfif fileExists(arguments.path)> | |
| <cfreturn true> | |
| </cfif> | |
| <!--- Then check if it's a directory -----> | |
| <cfif directoryExists(arguments.path)> | |
| <cfreturn true> | |
| </cfif> | |
| <!--- Path doesn't exist as either file or directory -----> | |
| <cfreturn false> | |
| <cfcatch type="any"> | |
| <!--- If any error occurs during checking, assume path doesn't exist -----> | |
| <cfreturn false> | |
| </cfcatch> | |
| </cftry> | |
| </cffunction> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment