directory tree -> array

Alex Tweedly alex at tweedly.net
Tue Feb 4 20:53:37 EST 2020


On 04/02/2020 17:20, Bob Sneidar via use-livecode wrote:

> Good points. I'll make some changes and resubmit. It never dawned on me that someone would intentionally send an empty path, but it *might* happen accidentally. Also, a path like /Users/BobSneidar being repeated would be very edge case, and as an IT guy I would probably take the computer away from any user that managed to make that happen, but it *could* happen and it's always a good idea to guard against such things.

Absolutely :-)

There should be a "basic competency test" - if you pass, you get to have 
a computer; if not, you're limited to an iPad :-)

Although "/Users/BobSneidar" is an extremely unlikely case, maybe "/tmp" 
or "/Users" are example where there is a greater danger the string 
occurring within the sub-folder names.

If you're going to resubmit to the Master Library, then I have one more 
suggestion (though I realize that it may be ruled out for backwards 
compatibility). Your function is called 'directorylisting', but it 
actually returns a list of files; maybe it could have its name changed 
to 'filelisting', and a 'directorylisting' function added ?

-- Alex.

P.S. Here's my version, called folderListing.

NB

1. it is non-recursive - in general iteration is faster than recursion.

2. like Ben's recursive version, it allows for easy suppression of 
"dot-folders", but I made that a parameter of the function

3. it puts newly found directories at the start of the list, rather than 
the end, so that it will get exactly the same result (i.e. in the same 
order) as the straightforward recursive method (i.e. depth-first). Just 
adding them at the end would have been marginally more efficient, and 
noticeably simpler in code - but it's a price worth paying for 
equivalency testing.


function folderListing pFolder, pIgnoreDotFolders
    local tFolders, tAllFolders
    local tOriginalFolder
    if pIgnoreDotFolders is empty then -- ignore them by default (Unix 
convention)
       put TRUE into pIgnoreDotFolders
    end if

    put the defaultfolder into tOriginalFolder

    local tAbsFolder, tFoldersLeft, tSub, tFoldersHere
    set the defaultfolder to pFolder
    put the defaultfolder into tAbsFolder   -- changes relative into 
absolute

    put CR into tFoldersLeft
    repeat until tFoldersLeft is empty
       put line 1 of tFoldersLeft into tSub
       set the defaultFolder to (tAbsFolder & tSub)
       if the result is not empty then
          -- skip or report as needed
          next repeat
       end if
       put (tAbsFolder & tSub)  &CR after tAllFolders
       try
          put folders() into tFolders
       end try
       delete line 1 of tFolders   -- delete ".."
       if pIgnoreDotFolders then
          filter tFolders without ".*"
       end if
       if tFolders is empty then
          delete line 1 of tFoldersLeft
       else
          put empty into tFoldersHere
          repeat for each line L in tFolders
             put tSub & "/" & L & CR after tFoldersHere
          end repeat
          put char 1 to -2 of tFoldersHere into line 1 of tFoldersLeft
       end if
    end repeat
    set the defaultfolder to tOriginalFolder
    return tAllFolders
end folderListing







More information about the use-livecode mailing list