Skip to main content
Participant
December 21, 2009
Question

View folder recursively how to?

  • December 21, 2009
  • 2 replies
  • 475 views

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?

    This topic has been closed for replies.

    2 replies

    ilssac
    Inspiring
    December 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.

    Participant
    December 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>

    Inspiring
    December 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

    Participant
    December 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>