Providing a Complete Hierarchical File Listing (Directory Walking)
Mac OS 9 Mac OS X Windows Linux

> How might one get a list of all files from a folder which contains nested
> folders and files several levels deep?

In my ignorance, I forgot to check my archived mail files which contained this modified code snippet from Geoff Canyon:

global gHierList,gMainFolder,gBaseLevels
local tCount

on mouseUp
  put "" into gHierList
  put "" into fld "Result"
  answer folder "Pick a folder you want to walk:"
  put it into gMainFolder
  if gMainFolder = "" then exit mouseUp
  set the itemDel to "/"
  put 0 into tCount
  put the number of items of gMainFolder into gBaseLevels
  put the directory into tOrigDir
  directoryWalk gMainFolder
  set the directory to tOrigDir
  put gHierList into fld "Result"
end mouseUp

on directoryWalk whatFolder
  set the itemDel to "/"
  set the directory to whatFolder
  put the files into temp
  add the number of lines of temp to tCount
  sort temp
  repeat for each line x in temp
    put whatFolder & "/" & x & cr after gHierList
  end repeat
  put the folders into tDirList
  sort tDirList
  delete line 1 of tDirList
  repeat for each line x in tDirList
    directoryWalk (whatFolder & "/" & x)
  end repeat
end directoryWalk
Posted 8/27/2002 by Scott Rossi to the MetaCard List   (See the complete post/thread)
And here’s an expanded version that adds some additional features (Windows only, however, but it can be modified to work on Macs as well) — Editor.

I wrote a quick little "directorywalker" stack that does this, and reports back whether it is a file (optionally along with its type) or a folder, and (optionally) indents the hierarchy. It was for Windows files only (so there's no Mac type/creator checking), but here's the script:

-- Card controls: [X] Show Hierarchy, [X] Show Type, [popup menu "Delimiters" - had "Spaces (2)", "Spaces (4)" or "Tabs" as choices
-- Field 1 holds the result

global gHierList,gMainFolder,gBaseLevels

on mouseUp
  put "" into gHierList
  answer folder "Pick a folder you want to walk:"
  if it = "" then exit mouseUp
  set the itemDel to "/"
  put it into gMainFolder
  put the number of items of gMainFolder into gBaseLevels
  directoryWalk gMainFolder,(the hilite of btn "ShowHierarchy"),(the selectedText of btn "Delimiters"),(the hilite of btn "ShowType")
  put gHierList into field 1
end mouseUp

on directoryWalk whatFolder,showHier,pDel,showType
  set the itemDel to "/"
  if "(2)" is in pDel then put 2 into numSpcs
  else put 4 into numSpcs
  put "                                        " into spcPad
  set the directory to whatFolder
  if showHier then
    put ((the number of items of whatFolder)) - gBaseLevels into numLevels
    if pDel <> "tabs" then
      do "put format(" & quote & "%" & (numLevels*numSpcs) & "s" & quote & "," & quote & quote & ")  into tPad"
    else
      do "put format(" & quote & "%" & (numLevels) & "s" & quote & "," & quote & quote & ")  into tPad"
      replace " " with numToChar(9) in tPad
    end if
    put last item of whatFolder into tFolderName
    if showType then put numToChar(9) & "folder" after tFolderName
    if numLevels <> 0 then
      put tPad & tFolderName into line (the number of lines of gHierList)+1 of gHierList
    else
      put tFolderName into line (the number of lines of gHierList)+1 of gHierList
    end if
  end if
  put the files into temp
  sort temp
  repeat with x = 1 to the number of lines of temp
    if not(showHier) then
      put whatFolder & "/" & (line x of temp) into line (the number of lines of gHierList)+1 of gHierList
    else
      put whatFolder & "/" & (line x of temp) into tPath
      put ((the number of items of tPath)) - gBaseLevels into numLevels
      if pDel <> "tabs" then
        do "put format(" & quote & "%" & (numLevels*numSpcs) & "s" & quote & "," & quote & quote & ") into tPad"
      else
        do "put format(" & quote & "%" & (numLevels) & "s" & quote & "," & quote & quote & ") into tPad"
        replace " " with numToChar(9) in tPad
      end if
      put line x of temp into tFile
      if showType then put addFileType(tFile) into tFile
      if numLevels <> 0 then
        put tPad & tFile into line (the number of lines of gHierList)+1 of gHierList
      else
        put tFile into line (the number of lines of gHierList)+1 of gHierList
      end if
    end if
  end repeat
  put the directories into tDirList
  sort tDirList
  repeat with x =  2 to the number of lines of tDirList
    directoryWalk (whatFolder & "/" & (line x of tDirList)),showHier,pDel,showType
  end repeat
end directoryWalk

function addFileType what
  put offset(".",what) into tChar
  if tChar <> 0 then
    put char tChar to length(what) of what into tExt
    put queryRegistry("HKEY_CLASSES_ROOT\"&(queryregistry("HKEY_CLASSES_ROOT\"&tExt& "\")) & "\") into tType
  else
    put "" into tType
    put "" into tExt
  end if
  if tType = "" then
    if tExt <> "" then
      delete char 1 of tExt  -- remove "."
      put toUpper(tExt) && "File" into tType
    else
      put "File" into tType
    end if
  end if
  return what & numToChar(9) & tType
end addFileType
Posted 8/27/2002 by Ken Ray to the MetaCard List   (See the complete post/thread)
Updated 4/14/2003 by Ken Ray (with special thanks to Hugh Senior)