URL to Variables

Posted on 2004-11-30
Last Modified: 2013-12-24
Ok...I closed my first question too soon it appear.

What I need to do is take a url for example and making each directory a variable.   Below is the code that kind of does what I need:

<cfset url1 = "">
<cfset dir_num = 0>

<cfloop condition = "dir_num less than 3">
     <cfset first_slash = 0>
     <cfset second_slash = 1>
     <cfset ind = 1>
     <cfloop condition="ind less than or equal to len(url1)">
          <cfif #Mid(url1, ind, 1)# eq "/">
               <cfset first_slash = #second_slash#>
               <cfset second_slash = #ind#>
          <cfset ind = #ind# + 1>  
     <cfset length = #second_slash# - #first_slash# - 1>
     <cfset directory = Mid(#url1#, #first_slash#+1, #length#)>

     <cfswitch expression="#dir_num#">
       <cfcase value="0">
         <cfset directory3 = #directory#>
       <cfcase value="1">
         <cfset directory2 = #directory#>
       <cfcase value="2">
         <cfset directory1 = #directory#>

     <cfset url1 = Mid(url1,1,len(url1)-#length#-1)>

     <cfset dir_num = #dir_num# + 1>

d1 = #directory1#
d2 = #directory2#
d3 = #directory3#

I need this code to do a couple more things:
1. if there is a file specified ignore it....I just want the directories returned as variables
2. only define variables for directories defined for example if the site is  there will only be two variables defined ...if there are 3 directories...3 variables will be defined.  
3. completely ignore the host...I do not need this returned as a variable...
Question by:mhixon

    Expert Comment

    You should see the url string as a list, separated with '/'.
    If a file is specied, :

    <cfset urlarray = arrayNew(1)>
    <cfset c=0>
    <cfloop index = "index_name"    list = "#url1#"    delimiters = "/">
    <cfif not index_name contains ".">
       <cfset urlarray[c]=index_name>
       <cfset c=c+1>

    <cfdump var="#urlarray#">

    Author Comment

    I get the following error:

    The element at position 0 of array variable &quot;URLARRAY&quot; cannot be found.
    LVL 17

    Accepted Solution

    <cfset variables.url1 = "" />
    <cfset variables.dir_num = 0 />


          <cfset variables.dir_num = variables.dir_num + 1 />
          <cfset structInsert( variables, "directory" & variables.dir_num, variables.item ) />


    Author Comment

    I get the following error:

    Cannot insert item with key directory1.  
     This key already exists.  

    Author Comment

    Sorry about it to work....Im going to work with this a little bit and make sure it does everything I need it to....Ill be back in touch thanks

    Author Comment

    by:mhixon this to do everything I want....appreciate the help!

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Suggested Solutions

    In our day to day coding, how many times have we come across a necessity to check whether a URL is a broken link or not? For those of you that answered countless and are using ColdFusion like myself, then this article is for you.  It will show yo…
    Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now