Get Drive Letter With ColdFusion

I ran into an issue recently where my production server's code used a different drive letter than my development environment. A small handful of applications relied on that drive letter and would break if the drive letter wasn't changed before deployment. In these specific scenarios, I couldn't call expandPath() or getTemplatePath() directly because the application wasn't in the root of the website.

My solution to this issue was to put the code below in my application.cfm / application.cfc file which sets an application variable called "driveLetter" to the applications current drive letter; then I call the application variable instead of the static drive letter that could change.

Get Drive letter

If you want to get the drive letter once, you could use do something like below.

	<cfset variables.driveLetter = listGetAt(expandPath('\'),1,'\')&'\' />

Get drive letter, then set application variable.

Below is the exact code I used in my application.cfm file to set the application variable initially, that way I don't have to run the script every time.

	
	<cfif !isDefined('application.driveLetter')>
		<cflock scope='application' timeout='5'>
			<cfset application.driveLetter = listGetAt(expandPath('\'),1,'\')&'\'/>
		</cflock>
	</cfif>

2 responses so far ↓

Dan G. Switzer, II - May 3, 2011 at 10:58 AM

Why not use getCurrentTemplatePath() to get the path to the current executing template. It should return the correct path no matter where the template resides. This would allow the template to reside even in different folder trees.

Paul Alkema - May 10, 2011 at 12:48 PM

You could however, in my case specifically, My files are stored outside the root of our website. So the getCurrentTemplatePath() doesn't work by itself.

Leave a Comment

Leave this field empty: