views:

285

answers:

5

Guys/Gals, I am trying to use CFDirectory to get a file listing of a mapping created in ColdFusion Admin. So far I cannot get the list to populate, but if I reference the physical path the full file list is displayed.

Here's the code I'm using:

<cfoutput> <cfdirectory action="list" directory="mymapping" name="test"><cfdump var="#test#"> </cfoutput>

Thanks,

Jon C.

A: 

If you are setting the directory in a variable called "mymapping". It would be as follows:

<cfdirectory action="list" directory="#mymapping#" name="test">
<cfdump var="#test#"> 
jarofclay
+2  A: 

Depending on how the mapping is set up - you may need to give it the full "virtual" path:

<cfdirectory action="list" directory="/mapping/folder" name="test">
<cfdump var="#test#">
Goyuix
Just tested this on Railo and it works.
Adrian Lynch
A: 

You didn't say which version of CF are you using, so Goyix's solution is partially correct: it works with the Railo, but not ACF.

In ACF8+ you can use the ServiceFactory to extract the real path. Code can look like this:

<cfset mapping = "/fusebox5" />

<cfset serviceFactory = createObject("java","coldfusion.server.ServiceFactory") />
<cfset mappings = serviceFactory.runtimeService.getMappings() />

<cfif StructKeyExists(mappings, mapping)>
    <cfdirectory action="list" directory="#mappings[mapping]#" name="test">
    <cfdump var="#test#">
<cfelse>
    <p>Mapping not found</p>
</cfif>

Note: used my existing FB5 mapping for the testing.

EDIT

Proposed later method with ExpandPath is much clearer. Leaving this one only as possibly useful alternative solution.

Sergii
A: 

Try this (not tested):

<cfset expandedPath=getDirectoryFromPath(expandPath("/mymapping/*.*")) />
<cfdirectory action="list" directory="#expandedPath#" name="dirListing" />
<cfdump var="#dirListing#" />
Al Everett
+1  A: 
Justice
Nice gotcha! Actually works with ACF and Railo.
Sergii

related questions