Save list of folder contents

[UPDATE] Revisited this one and made it a little nicer. It is faster and gives a total count of directories and files, skips invisibles (but you can undo that), and doesn’t error on item names longer than 31 characters. Confirmed to run on 10.4, YMMV.

This AppleScript will create a file containing a tree style list of all files inside the chosen directory and save that list in a file created in that directory. The list file will include the path of the listed directory as well as the date and time it was saved and a tally of me bananas… er, folders and files under the starting directory.

The default directory in the choose dialog is the folder of the Finder’s frontmost window unless there are no Finder windows. In which case the default is the Desktop folder.

It does not follow aliases (not sure about symlinks) and will indicate an alias by placing ” [alias]” behind the file name.

This is actually a modified version of a script that’s been around the web for years.


set fileList to ""
set d_count to 0
set f_count to 0
global fileList, d_count, f_count

on listFolder(f, s)
  list folder f as alias without invisibles
  repeat with i in the result
    set finfo to (info for alias (f & i))
    if folder of finfo then
      set d_count to d_count + 1
      set fileList to fileList & s & i & "/" & return
      listFolder(f & i & ":", s & "    ")
      if alias of finfo then
        set f_count to f_count + 1
        set fileList to fileList & s & i & " [alias]" & return
        set f_count to f_count + 1
        set fileList to fileList & s & i & return
      end if
    end if
  end repeat
  return {fileList, d_count, f_count}
end listFolder

  tell application "Finder" to set cwd to (POSIX path of (folder of window 1 as string)) as POSIX file
on error
  set cwd to path to desktop folder
end try
set theFolder to (choose folder "Select a folder to list:" default location cwd) as string

--This will get the name of the folder for use in the name of the generated file
set folderName to name of (info for (theFolder as alias))

--This will get the Unix style path of the folder for info purposes inside the file itself
set folderPath to POSIX path of theFolder

set infoList to listFolder(theFolder, "")
set fileList to "# File list of: " & folderPath & " on " & (current date) & return & item 2 of infoList & " folders and " & item 3 of infoList & " files total." & return & return & item 1 of infoList

set listFile to ((theFolder) as text) & "!File list of this folder (" & folderName & ")" & (do shell script "date "+ %Y-%m-%d %H%M"") & ".txt"

tell application "Finder"
  set newFile to (open for access file listFile with write permission)
  set eof newFile to 0
  write fileList to newFile
  close access newFile
  open listFile as alias
end tell

Leave a Reply

Your email address will not be published. Required fields are marked *