Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if file path is absolute or relative in ColdFusion

Tags:

coldfusion

I have a function that accepts a file path. Users can pass in either an absolute or relative path to a file. If a relative path is provided, the ExpandPath function can convert it to an absolute path like so:

<cfset filepath = ExpandPath("data/test.txt") >

.. and it returns:

C:\www\example\data\test

But if user provides an absolute path like:

<cfset filepath = ExpandPath("C:\www\example\data\test") >

.. it returns:

C:\www\example\C:\www\example\data\test

How may I solve this problem?

like image 922
user160820 Avatar asked Dec 27 '22 01:12

user160820


2 Answers

A possibly more flexible way to do this is to check to see if the directory from the raw input exists and, if not, try expandpath. Something like this:

<cfif directoryExists(myFileLocation)>
  <cfset theDirectory=myFileLocation)>
<cfelseif directoryExists(expandPath(myFileLocation))>
  <cfset theDirectory=expandPath(myFileLocation)>
<cfelse>
  <cfthrow message="Invalid directory!">
</cfif>
like image 144
ale Avatar answered Jan 13 '23 13:01

ale


You could test the string and see if it starts with C:\ for windows or \\ for unix, and use that as an if? This could be your windows check:

<cfif reFindNoCase("[a-zA-Z]:\\",myFileLocation)>
   <!--- Is a absolute path --->
<cfelse>
   <!--- Is not an absolute path --->
</cfif>
like image 45
BennyB Avatar answered Jan 13 '23 15:01

BennyB