5 Replies Latest reply on Nov 23, 2011 9:19 PM by -==cfSearching==-

    I can't pull a value out of a list from CFDIRECTORY.

    VeryHopeful

      I've avoided insanity all these years, but this issue finally has me on the brink.  My institutionalization is in your hands.

       

      I'm trying to get the name of a specific directory from the list of directories generated by CFDIRECTORY action="list".  But everything I try, fails in one way or another.  Bascially, I can't pull the value out of the structure -- even though the code DOES FIND the value -- because it thinks the value is supposed to be a field or key.  (The server is running CF8.)

       

      These lines of code work fine:

       

      <!--- Make and loop through List of local directories. --->

      <CFSET LocalDirectoryList = "">

      <CFDIRECTORY action="list" name="LocalDirectoryList" TYPE="dir" directory="#PathOfLocalFolderToCopyToRemoteFolders#">

      <CFIF IsDefined("LocalDirectoryList.RecordCount") AND LocalDirectoryList.RecordCount GT "0">

      <CFLOOP query="LocalDirectoryList">

       

      Now comes the killer.  I want to define a variable whose value is the name of the current local directory -- that is, the value of LocalDirectoryList.Name for each loop of the loop.  But if I try to Access LocalDirectoryList.Name as a simple string variable, the way I usually can for a query string variable --

       

      <CFSET ThisLocalDirectory = LocalDirectoryList.Name>

       

      -- I get a message, "You have attempted to dereference a scalar variable of type class java.lang.String as a structure with members."

       

      On the other hand, if I treat "LocalDirectoryList.Name" as a structure, and try to destructure it with any of the following --

       

      1) <CFSET ThisLocalDirectory = Evaluate(LocalDirectoryList.Name)>

      2) <CFSET ThisLocalDirectory = "#Evaluate(LocalDirectoryList.Name)#">

      3) <CFSET ThisLocalDirectory = "#StructFind(LocalDirectoryList, Name)#">

      4) <CFSET ThisLocalDirectory = StructFind(LocalDirectoryList, Name)>

      5) <CFSET ThisLocalDirectory = "#Evaluate(StructFind(LocalDirectoryList, Name))#">

       

      I get either "Variable backgrounds is undefined" (for 1 or 2) or  "The specified key, backgrounds, does not exist in the structure" (for 3, 4 or 5) .

       

      As it happens, the name of that directory is "backgrounds".  So the value I'm looking for DOES EXIST in the structure "LocalDirectoryList".  And the code IS FINDING IT.  But it thinks its the NAME of a key, instead of the VALUE of the key.

       

      Note:  Adding the attribute, listInfo="Name", to the CFDIRECTORY tag restricts the returned fields, or keys, to "Name", but LocalDirectoryList.Name is still a structure, and I still get the same results.

       

      Will I be the next cuckoo in the cuckoo's nest?  Or can you save me, and keep me sane?  My fate is in your hands.

       

      Thank you!

        • 1. Re: I can't pull a value out of a list from CFDIRECTORY.
          Reed Powell Level 3

          Have you tried putting a CFDUMP of LocalDirectoryList immediately following the CFDIRECTORY to see what the data looks like?  That should give you a pointer as to what the .NAME element is actually referring to.

          hth,

          Reed

          • 2. Re: I can't pull a value out of a list from CFDIRECTORY.
            VeryHopeful Level 1

            Thanks, Reed.  Yes, I did that.  The value of the Name element is the directory name, which is exactly what it should be.  In any case, that's evident from what I said:

             

            I get either "Variable backgrounds is undefined" (for 1 or 2) or  "The specified key, backgrounds, does not exist in the structure" (for 3, 4 or 5) .

             

            As it happens, the name of that directory is "backgrounds".  So the value I'm looking for DOES EXIST in the structure "LocalDirectoryList".  And the code IS FINDING IT.  But it thinks its the NAME of a key, instead of the VALUE of the key.

            • 3. Re: I can't pull a value out of a list from CFDIRECTORY.
              insuractive Level 3

              <cfset myDirName = name>

               

              You don't need the evaluate statement (in fact you probably shouldnt need to use that ever in this day and age). And inside of your query loop you may address all of your query fields without prefix.

               

              When in doubt, dump out your query from cfdirectory.

              • 4. Re: I can't pull a value out of a list from CFDIRECTORY.
                VeryHopeful Level 1

                insuractive, thank you!

                 

                So simple!  Actually, I had seen programmers use query variables inside loops without the query name, but I thought it was just for brevity and that the query name added clarity in reviewing the code.  It never occurred to me that there would be situations where having the query name, or not, would make a difference.

                 

                You saved the day!

                • 5. Re: I can't pull a value out of a list from CFDIRECTORY.
                  -==cfSearching==- Level 4

                  But it thinks its the NAME of a key, instead of the VALUE of the key.

                   

                  Because that what the code is instructing CF to do ;-)

                   

                  <CFSET ThisLocalDirectory = StructFind(LocalDirectoryList, Name)>

                   

                   

                  If you want to search for the literal string "Name" it must be enclosed in quotes.  Otherwise you are instructing CF to treat it as a variable and to pass its value (ie "backgrounds") into structFind().  The same thing with evaluate().

                   

                  <CFIF IsDefined("LocalDirectoryList.RecordCount") AND LocalDirectoryList.RecordCount GT "0">

                  However as Michael pointed out, there is rarely a need for evaluate, and under normal circumstances no need for that IsDefined either. The query should always exist and the loop will only execute if the query contains one or more records.  So the code could be reduced to this.

                   

                  <CFDIRECTORY action="list"

                                  name="LocalDirectoryList"

                                  type="dir" 

                                  directory="#PathOfLocalFolderToCopyToRemoteFolders#">

                   

                  <CFLOOP query="LocalDirectoryList">

                           <CFSET ThisLocalDirectory = LocalDirectoryList.Name>

                  </CFLOOP>

                   

                  Note, there is technically no need to assign #LocalDirectoryList.Name# to another variable either.  It can be used directly.