Copy link to clipboard
Copied
Hi
I am trying to view folder recursively and i make it just like that
<html>
<head>
</head>
<body>
<cfinclude template="download.cfm">
Listing <br>
<cfdirectory directory="\\pl1xpd-16286\shared_data\applet" name="dirQuery" action="LIST" type = "all">
<br>Result<br>
<cfset startupDir ="\\pl1xpd-16286\shared_data\applet">
<cfloop query = "dirQuery">
<cfoutput>
<cfif dirQuery.type EQ "Dir">
<cfoutput>
#dirQuery.name# #dirQuery.type#<br>
</cfoutput>
<cfset newDirPath = "#startupDir#">
<cfset newDirPath &= "\">
<cfset newDirpath &= "#dirQuery.name#">
<cfset listDirectory("#newDirPath#")>
<cfelse >
<cfoutput>
#dirQuery.name# #dirQuery.type# <br>
</cfoutput>
</cfif>
</cfoutput>
</cfloop>
</body>
</html>
<cffunction name = "listDirectory" output = "true">
<cfargument name = "directoryPath" required = "true" type = "string">
<cfdirectory directory="#directoryPath#" name = "dir" action ="list" type="all">
<cfloop query = "dir">
#dir.name# #dir.type#
<cfif dir.type eq "dir">
<cfset newPath ="#directoryPath#">
<cfset newPath &="/">
<cfset newPath &= "#dir.name#">
<cfset listDirectory("#newPath#")>
<cfelse>
<cfoutput>
#dir.name# #dir.type#<br>
</cfoutput>
</cfif>
</cfloop>
<cfreturn>
</cffunction>
What iam doing wrong?
Copy link to clipboard
Copied
Here is rest of code
<html>
<head>
</head>
<body>
<cfinclude template="download.cfm">
Dupa i tyle i zacznijmu listowac <br>
<cfdirectory directory="\\pl1xpd-16286\shared_data\applet" name="dirQuery" action="LIST" type = "all">
<br>Result<br>
<cfset startupDir ="\\pl1xpd-16286\shared_data\applet">
<cfloop query = "dirQuery">
<cfoutput>
<cfif dirQuery.type EQ "Dir">
<cfoutput>
#dirQuery.name# #dirQuery.type#<br>
</cfoutput>
<cfset newDirPath = "#startupDir#">
<cfset newDirPath &= "\">
<cfset newDirpath &= "#dirQuery.name#">
<cfset listDirectory("#newDirPath#")>
<cfelse >
<cfoutput>
#dirQuery.name# #dirQuery.type# <br>
</cfoutput>
</cfif>
</cfoutput>
</cfloop>
</body>
</html>
Copy link to clipboard
Copied
If you are going to recursively call a function like that you MUST var scope all those variables.
<cfset var dir="">
<cfset var newpath = "">
This must be done at the top of the function just after the <cfargument...> tags before any other logic.
This makes each varaible local to the current running instance of the funciton. Otherwise all the instances will share the same global variables and that will cause the funciton to produce incorrect results.
But it might be a lot easier if you just use the 'recurse' property of the <cfdirectory...> tag.
Copy link to clipboard
Copied
Ok i need to resolve this problem this way becouse i'm trying to make xml which will reflect structure of listed katalog
i,m making it like this
<cfcomponent>
<cffunction name = "listDirectory" output = "true">
<cfargument name = "directoryPath" required = "true" type = "string">
<cfset var startupDir="">
<cfset var newDirPath = "">
<cfdirectory directory="#directoryPath#" name = "dir" action ="list" type="all">
<cfloop query = "dir">
<cfif dir.type eq "dir">
<cfoutput>
< dir name="#dir.name#"><br>
</cfoutput>
<cfset newPath ="#directoryPath#">
<cfset newPath &="\">
<cfset newPath &= "#dir.name#">
<!--- <cfoutput>
nowa sciezka to #newPath#<br>
</cfoutput>
--->
<cfset listDirectory("#newPath#")>
<cfset newPath = "#directoryPath#">
<cfset dir = #dir#>
<cfoutput>
nowa sciezka to #newPath#<br>
</cfoutput>
<cfelse>
<cfoutput>
< file > #dir.name# < /file ><br>
</cfoutput>
</cfif>
</cfloop>
<cfoutput>
< /dir><br>
</cfoutput>
<cfset newPath = "#directoryPath#">
<cfreturn>
</cffunction>
</cfcomponent>
for structure of folder like this:
root
--------->txt1.txt
--------->txt2.txt
--------->txt3.txt
------------------->dir
-------------------------->txt11.txt
-------------------------->txt12.txt
-------------------------->txt13.txt
------------------------------------------>dir11
------------------------------------------------>txt21.txt
and result is
< dir name="root">
< file > 01.txt < /file >
< file > 02.txt < /file >
< file > 03.txt < /file >
< dir name="dir">
< dir name="dir11">
< file > txt21.txt < /file >
< /dir>
< file > txt21.txt < /file >
< file > txt21.txt < /file >
< file > txt21.txt < /file >
< /dir>
< /dir>
Copy link to clipboard
Copied
You're not VARing your "dir" variable (the one you use in <cfdirectory>. So each recursive call will overwrite the calling function's copy of that variable.
--
Adam
Copy link to clipboard
Copied
Hi,
I hope this tutorial will help you to achieve your requirement.
http://www.bennadel.com/blog/503-Creating-An-XML-Representation-Of-A-Directory-And-Sub-Directories-Using-ColdFusion.htm
HTH