ColdFusion ExpandPath() And GetCurrentTemplatePath()

Posted September 21, 2006 at 3:33 PM

Tags: ColdFusion

One of the things that always get's me is the fact that in ColdFusion, the ExpandPath() method expands the path of the base path. I would say 95% of the time, this doesn't matter, but the other 5% of the time, I end putting stuff in the wrong directories. In my head, it just makes sense that ExpandPath() should expand the current template path since that is the one calling the code (just how the CFInclude template attribute works).

When I do need to expand the current path, not the base one, I just use the GetCurrentTemplatePath(). And, since expand path is meant to add to a directory, I get the directory from the returned path:

 Launch code in new window » Download code as text file »

  • <!--- Get expanded path of the BASE path. --->
  • <cfset strPath = ExpandPath( "./" ) />
  •  
  • <!--- Get the expanded path of the CURRENT template path. --->
  • <cfset strPath = GetDirectoryFromPath(
  • GetCurrentTemplatePath()
  • ) />

Assuming that we have the current directory structure:

sites
..test
....index.cfm
....sub
......test.cfm

and that our CGI.script_name points us to the page "index.cfm" but the code is being executed by the CFIncluded page, test.cfm, the first path would be:

D:\sites\test\

and the second path would be:

D:\sites\test\sub\

So anyway, just keep that in mind. We use CFIncludes so much in ColdFusion that I feel it easy to make this mistake.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Other Searches  |  Print Page




Reader Comments

Oct 20, 2008 at 6:27 AM // reply »
5 Comments

Thanks Ben - your Blog has to be the most useful resource out there when it comes to CF Quirks - I remember reading this entry a couple of years back - first port of call when I needed to do this...


Oct 22, 2008 at 7:30 AM // reply »
7,572 Comments

@Dan,

I am always happy to hear that I have provided some value - thanks.


Dec 18, 2009 at 11:10 AM // reply »
8 Comments

Hopefully this tip will help someone down the road too, or maybe someone can enlighten me as to the "right" way to code for the physical file path of an application.

I was setting the following code in the OnApplicationStart() method to set the application's physical file path (http://mydomain.com/myApplication):

<cfset application.rootpath = expandPath('./')>

The application uses index.cfm as a "controller" (i.e.index.cfm?disp=thispage) so I THOUGHT it would set the path relative to the root of the application (C:\inetpub\wwwroot\mydomain\myApplication)

Well, it does...MOST of the time....I still haven't gotten to the bottom of it, but sometimes the rootpath would get set to nested directories (C:\inetpub\wwwroot\mydomain\myApplication\middle\). I can only assume that the application would sometimes reinit when it was in the middle of processing something in the "middle" directory.

Long story short, I'm about to deploy this code fix to (hopefully) resolve the problem: <cfset application.rootpath = "#expandpath('/')##application.applicationname#\"> . The application.applicationname is the same as the "myApplication" directory, so I'm hopeful that this will work....at least my three test runs worked..lol

With any luck, all this code will actually show up so its readable on Ben's blog :)


Jan 5, 2010 at 9:00 AM // reply »
7,572 Comments

@Dan,

The strategy that I have found to be very helpful is using:

getDirectoryFromPath( getCurrentTemplatePath() )

... inside of Application.cfc. Since you always know where the Application.cfc file lives, you know you can get the root directory by stripping off "Application.cfc" from the current template path (which is Application.cfc).


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 21, 2010 at 7:40 PM
Is Simulating User-Input Events With jQuery Ever A Good Idea?
A couple of things. One you embed the initial state of of more-info in the CSS. IMHO, that behavior should be in jQuery: moreInfo.hide(); It shows that the behavior your toggling and closing is mor ... read »
Mar 21, 2010 at 3:59 PM
Exploring ColdFusion Component Runtime Class Properties And Serialization
@Elliott, according to Ben's experiment, serializeJSON() doesn't access the private data by default - it doesn't even access the getHair() method - so trying to clone a Girl.cfc via serializeJSON/des ... read »
Mar 21, 2010 at 3:49 PM
Ask Ben: Javascript String Replace Method
I'm confused a bit by what you are asking, but if had this sentence: The color, red, is in the style statement; style: red;. and wanted to remove all or change all of the commas, colons, and semi-c ... read »
Mar 21, 2010 at 3:13 PM
Ask Ben: Javascript String Replace Method
I am trying to make a java program to count the number of times that these punctuation marks occur in a body of text: , : ; . ! - ' " ? / \ I am using this piece to ferret out the commas: numcommas ... read »
Mar 21, 2010 at 11:13 AM
A New Wrist Pain
@chiropractor suwanee, Spoken like someone trying to sell something. Other than for minor, temporary relief from some back pain, chiropractic treatment is nothing but placebo effect and quackery. ... read »
Mar 21, 2010 at 6:32 AM
ColdFusion CFPOP - My First Look
Apologies... The field name in the db for C. is "BounceCode" It stores the code / message which is returned in the email. Sorry for the confusion. ... read »
Mar 21, 2010 at 6:29 AM
ColdFusion CFPOP - My First Look
@Jose Galdamez, Hi Ben and Jose 1st of all.. big thanks to Jose for his Skype chat a few weeks back. Your time was much appreciated. I have come up with a rather unelegant solution to my problem a ... read »
Mar 21, 2010 at 3:42 AM
A New Wrist Pain
Chiropractic treatment is one of the best methods for treating numerous health problems naturally. After years of experience being a chiropractor, I have found that it is a powerful way to solve many ... read »