JonCav
JonCav

Reputation: 167

cfdirectory on a ColdFusion Mapping

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.

Upvotes: 2

Views: 2018

Answers (5)

Sergey Galashyn
Sergey Galashyn

Reputation: 6956

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.

Upvotes: 0

yfeldblum
yfeldblum

Reputation: 65445

You need to use the form /mymapping, with the / in front. And you need to use ExpandPath to expand the “virtual” directory as defined in the mapping /mymapping. That way, you end up using cfdirectory and passing in a physical directory, one that actually exists on the hard drive and not just in the ColdFusion mappings.

<cfdirectory
    name = "theQuery"
    action = "list"
    directory = "#ExpandPath("/mymapping")#"
/>

Upvotes: 1

ale
ale

Reputation: 6430

Try this (not tested):

<cfset expandedPath=getDirectoryFromPath(expandPath("/mymapping/*.*")) />
<cfdirectory action="list" directory="#expandedPath#" name="dirListing" />
<cfdump var="#dirListing#" />

Upvotes: 0

Goyuix
Goyuix

Reputation: 24370

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#">

Upvotes: 2

jarofclay
jarofclay

Reputation: 680

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#"> 

Upvotes: 0

Related Questions