How to set a variable based on a user selected folder / directory location of .cfm file

Posted on 2007-03-27
Last Modified: 2013-12-16
Hello CF Experts,

I have a CF application that I would like to dynamically set an ID variable based on the folder location of the .cfm file.

So, in my application, I have folders named Category1, Category2, Category3 and so on.

In those folders, I have index.cfm.

I would like to set a (known) category ID based on the folder (category) that the user chooses.

The logic would go something like this.

If folder name Category1 is detected in URL, set this:
<cfif (detect folder name for Category1 in url)>
<cfset sectionID = "123456">
<cfif (detect folder name for Category2 in url)>
<cfset sectionID = "123457">

Again, we already know what the ID is going to be, which will corelate to the chosen category, we just need to have a way to detect the folder name on the site that they choose.  Of course, we could always hard code the categoryID in the URL on the category selection page, but we don't really want to do that here.  Our main page is not dynamic in nature, and we are fine with that.

Thanks in advance for any help!
Question by:dstjohnjr
  • 2
  • 2
LVL 39

Accepted Solution

gdemaria earned 500 total points
ID: 18802337

this function will give you your folder location..

This will tell you the folder

 <cfset dir =  ListLast(getDirectoryFromPath(getCurrentTemplatePath()),"/")>

Then use a switch

 <cfswitch expression="#dir#">
  <cfcase value="category1">
      <cfset sectionID = 12343>



Author Comment

ID: 18802701
Ok, in this example, #dir# returns the full path (i.e. c:\cfusionmx7\wwwroot\category1\) to the location being browsed.

How do I test for a portion of the path in the cfcase code?  It works if I enter:

<cfcase value="c:\cfusionmx7\wwwroot\category1\">

but not just

<cfcase value="category1">

Also, you mention:

"this function will give you your folder location..

how do I implement this function?

Thanks for your help!

Author Comment

ID: 18803349
Ok, I actually figured this out on my own.  Here is the solution:

<cfset dir =  ListLast(getDirectoryFromPath(getCurrentTemplatePath()),"\")>
<!--- Start parsing and testing for the various cases based on our folder  --->
<!--- structure and then set the categoryID and variable based on our folder location --->
<!--- NOTE:  This code MUST be executed from within each folder you want to test for and NOT in an includes folder --->
 <cfswitch expression="#dir#">
<cfcase value="Category1" delimiters="\"  >
<cfset categoryID = 1001>
<cfcase value="Category2 delimiters="\" >
<cfset categoryID = 1002>
...and so on...

The problem was that initially, I did not have the delimiters property setup in the cfcase statement.  Once I added that, all is good!  Thanks for your help!  Although I ended up figuring it out on my own, you provided a great start and led me down the right path.  Thanks!
LVL 39

Expert Comment

ID: 18803499

 glad you got it.

 The problem was the direction of the slash...
<cfset dir =  ListLast(getDirectoryFromPath(getCurrentTemplatePath()),"/")>

 should have been
 <cfset dir =  ListLast(getDirectoryFromPath(getCurrentTemplatePath()),"\")>

 Glad you caught that small problem.

 However, in your cfcase statement, the delimiters parameter should be removed

  <cfcase value="Category1" delimiters="\">

 That parameter indicates the delimiter in the value clause, if there is a list such as this..

 <cfcase value="A|B|C|D" delimiters="|">

 Since you only have one value "Category1" the delimiter does nothing.


Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
Objective of This Article In 1990’s, when I was a budding software professional, I had a lot of confusion about which stream or technology, I had to choose to build my career. In those days, I had lot of confusion like whether to choose System so…
The purpose of this video is to demonstrate how to Import and export files in WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: : Click on Too…
The purpose of this video is to demonstrate how to integrate Mailchimp with Facebook. This will be demonstrated using a Windows 8 PC. Mailchimp and Facebook will be used. Log into your Mailchimp account. : Click on your name. Go to Account Setti…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question