• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Guest
Nov 21, 2011 Nov 21, 2011

Copy link to clipboard

Copied

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!

TOPICS
Advanced techniques

Views

1.0K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Advocate , Nov 21, 2011 Nov 21, 2011

<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.

Votes

Translate

Translate
Enthusiast ,
Nov 21, 2011 Nov 21, 2011

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Nov 21, 2011 Nov 21, 2011

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Nov 21, 2011 Nov 21, 2011

Copy link to clipboard

Copied

<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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Nov 23, 2011 Nov 23, 2011

Copy link to clipboard

Copied

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Nov 23, 2011 Nov 23, 2011

Copy link to clipboard

Copied

LATEST

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation