The Directory Walker revisited

wouter wouter.abraham at pi.be
Mon Sep 1 05:52:00 EDT 2003


Hi,

Upon examining the nice (recursive) directory walking script:

file007
at
http://www.sonsothunder.com/index2.htm?http://www.sonsothunder.com/ 
devres/revolution/revolution.htm

But I watched it bump into the recursion-limit wall.
Following is a non recursive version of this script.
Although it is non recursive be careful not to unleash it (without  
filters) on for example the complete Mac OS X volume or you will be  
tied up for quite a while seen the amount of files and directories  
there.
First is a bare bones script. Next some additions.


on mouseUp
   put "" into fld "Result"
   answer folder "Pick a folder you want to walk:"
   if it = "" then exit mouseUp
   put it & "/" into tMainFolder
   put the directory into tOrigDir
   directoryWalk tMainFolder
   set the directory to tOrigDir
end mouseUp

on directoryWalk tDir
   set the itemDel to "/"
   put "" into tDirlist
   repeat
     if tDir = empty then exit repeat
     set the directory to tDir
     put tDir & cr after tHierList
     if the hilite of btn "theFiles" then
       put the files into temp
       repeat for each line x in temp
         put tDir  & x & cr after tHierList
       end repeat
     end if
     put line 2 to -1 of the folders into tTempDirList
     repeat for each line x in tTempDirList
       put tDir &  x & "/" & cr after tDirList
     end repeat
     put line 1 of tDirList into tDir
     delete line 1 of tDirlist
     put "" into tTempDirList
   end repeat
   sort tHierlist
   put tHierlist into fld "result"
end directoryWalk

Now for some condiment. Adapt to your own taste and circumstances:

on mouseUp
   put "" into fld "Result"
   answer folder "Pick a folder you want to walk:"
   if it = "" then exit mouseUp
   put it & "/" into tMainFolder
   put the directory into tOrigDir
   directoryWalk tMainFolder
   set the directory to tOrigDir
end mouseUp

on directoryWalk tDir
   set the itemDel to "/"
   put "" into tDirlist
   #### to constrain the examination depth of directories if needed
   get the label of btn "depth"  ### option styled btn with  
none,1,2.....xx as content
   if  it is not a number or it = empty then put empty into tMaxDepth
   else put it  +  the number of items in tDir into tMaxDepth

   repeat
     if tDir = empty then exit repeat
     set the directory to tDir
     put tDir & cr after tHierList

     #### directories with or without the files
     if the hilite of btn "theFiles" then
       put the files into temp
       repeat for each line x in temp
         put tDir  & x & cr after tHierList
       end repeat
     end if

     put line 2 to -1 of the folders into tTempDirList
     repeat for each line x in tTempDirList
       put tDir &  x & "/" & cr after tDirList
     end repeat
     put line 1 of tDirList into tDir

     ####  check part  for constraining the depth of directories to be  
examined
     if tMaxDepth <> empty and the number of items in tDir = tMaxDepth  
then exit repeat

     delete line 1 of tDirlist
     put "" into tTempDirList
   end repeat
   sort tHierlist
   if the hilite of btn "ShowHierarchy" then ShowHierarchy tHierlist
   else put tHierlist into fld "result"
end directoryWalk

on ShowHierarchy @x
   if the hilite of btn "theTabs" then  ##### tabs or spaces
     put tab into tIndentContainer
     put tab into tExtraIndent
     put 1 into tFactor
   else
     put space into tIndentContainer
     put  "     " into tExtraIndent
     put 3 into tFactor
   end if
  #### make a string of 128 chars
   repeat 7
     put tIndentContainer after tIndentContainer
   end repeat
#### choose the char you like as indicator
   if the hilite of btn "dirMarker" then put "•" into tdirMarker
   else put "" into tdirMarker
   set the itemdelimiter to "/"
   put the number of items in line 1 of x into tDepth
   put tDepth into tPrevDepth
   repeat for each line i in x
     get the number of items in i
     if last char of i = "/" or it <= tPrevDepth then
       put it into tPrevDepth
       put "" into tIndent
       put char 1 to ((  it - tDepth) * tFactor) of tIndentContainer  
into tIndent
       if last char of i = "/" then put tIndent & item -1 of i &&  
tdirMarker & cr after tList
       else put tIndent & item -1 of i  & cr after tList
     else put tIndent& tExtraIndent & item -1 of i & cr after tList
   end repeat
   put tList into fld "result"
#### if tab delimited it can be used in menu buttons
   if tExtraIndent = tab then put tList into btn "theMenu"
end ShowHierarchy

Have a nice day,
WA



More information about the use-livecode mailing list