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

View folder recursively how to?

New Here ,
Dec 21, 2009 Dec 21, 2009

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?

437
Translate
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
New Here ,
Dec 21, 2009 Dec 21, 2009

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>

Translate
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 ,
Dec 21, 2009 Dec 21, 2009

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.

Translate
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
New Here ,
Dec 23, 2009 Dec 23, 2009

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>
                              &lt dir name="#dir.name#"&gt<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>
                              &lt file &gt #dir.name# &lt /file &gt<br>
                         </cfoutput>                                   
                    </cfif>
               
     </cfloop>
     <cfoutput>
          &lt /dir&gt<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>

Translate
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
LEGEND ,
Dec 23, 2009 Dec 23, 2009

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

Translate
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 ,
Dec 23, 2009 Dec 23, 2009
LATEST

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

Translate
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