ODBiC Discussion Board


RE: Recusing directories as an unordered list, David Richardson, 09-18-2007

This may do what you want

by Roger Harris, September 23, 2007 11:37

I think you are getting <ul></ul> whenever the function is called but it doesn't output any lines, which would happen if the subdirectory only has files below it. I think you are trying to output a "view files" link if there are files on the directory, or "empty" if there are no files or subdirectories, so the function should always output at least one line. But I think your test for that is in the wrong place; it should be after the EACHFILE loop is finished. Try this version, which counts subdirectories and files separately and then tests at the end of the loop if either of the special lines should be output:

<!--FUNCTION recurse (currentdirectory, is_subdirectory) 
   filerow, dirrow -->
 <!--SET filerow=0, dirrow=0 -->
 <!--IF $is_subdirectory$--><ul><!--ENDIF-->
 <!--EACHFILE $currentdirectory$ --> 
   <!--IF $file_is_dir$ -->
     <!--SET dirrow=$dirrow$+1 -->
     <li><img src="images/folder.gif" />$file_name$
     <!--SET subdirectory=$rootdirectory$"\"$file_name$ -->
     $recurse($subdirectory$"\"$filetype$, 1)
     </li>
   <!--ELSE -->
     <!--SET filerow=$filerow$+1 -->
   <!--ENDIF -->
 <!--ENDFILE --> 
 <!--IF $filerow$ + $dirrow$ = 0 -->
       <li>Empty</li>
 <!--ELSE IF $filerow$ > 0 -->
       <li><a href="#">View files</a></li>
 <!--ENDIF -->
<!--IF $is_subdirectory$--></ul><!--ENDIF-->
<!--RETURN -->

(Note that I've listed the variables filerow and dirrow immediately after the FUNCTION name(arguments) definition but before the end of the function definition command line. That defines "local" variables within the function, unique for that call, which is useful in cases like this so you won't overlay the counts in your current EACHROW iteration when you call the function recursively. Variables that not declared this way in the function definition command are assumed to be "global" -- there is only on instance of the variable used by all script code. The names given to function arguments (i.e. between parentheses in the function definition) are also "local" to the function and are undefined outside the function.)



Replies:


Post Your Reply:

Name   
E-mail  optional
Topic  
Message:

HTTP Link: 
Link text: 

          


Copyright ©1997-2003, Roger Harris. All rights reserved.