Recursively create folders leading to a prespecified path?

Hi all. A newbie here.
I’ve got a trouble and I was hoping you guys can help. So here’s what I want to do.

I have something like this:

But I am actually not sure if all of those folders leading to the path exist. I guess I could check each and every single folder in the path and if it does not exist, then create one. But this seems tedious.

What would be an optimal method to solving this problem, especially given the path could change on you at any point? Thanks guys!

The ramifications of paths beginning with colons are a bit complex in OS X. They’re best avoided if you’re a newbie. :slight_smile:

The line you’ve quoted is in Finder format, but in fact your problem is most easily and quickly handled by using a shell script instead of the Finder:

set my_path to (path to startup disk as Unicode text) & "Some:Folder:Deep:Down:There:"
-- "mkdir -p" creates the given folder hierarchy if it doesn't exist.
do shell script ("mkdir -p " & (POSIX path of my_path))

tell application "Finder"
  set my_folder to folder my_path
end tell

Thanks Nigel! This worked beautifully.

Just for the sake of doing the same thing in just plain vanilla Applescript, here’s what I used to use to create the folder hierarchy for new books. It allows for any number of folders in any level of subfolders…


-- Opening dialogs to get the author and book code
set AuthorName to text returned of (display dialog "What is the first author's last name? If there is a descriptor, please enter it here seperated by an underscore (_). Examples: AAOS_EMTB9e_47386, Dale_Java_04903" buttons {"OK", "Cancel"} default button "OK" default answer "Letters and numbers only; no puncuation.")
set BookCode to text returned of (display dialog "What are the last five digits of the ISBN?" buttons {"OK", "Cancel"} default button "OK" default answer "No dash")

-- Dialog to set the path where the book folder will be created
set FolderLoc to (choose folder with prompt "Where shall the new folder go?") as string

-- Concantonation of the author name and book code
set NewFolderName to (AuthorName & "_" & BookCode) as Unicode text

-- Creates the main book folder
tell application "Finder"
	make new folder at (alias (FolderLoc)) with properties {name:NewFolderName}
end tell

-- Makes the FolderLoc into Unicode text to be used later
set SubFolderLoc to FolderLoc as Unicode text

-- Creates the path INTO the main book folder
set SubFolderPath to (SubFolderLoc & ":" & NewFolderName) as Unicode text

-- Creates the production folders in the main book folder
tell application "Finder"
	make new folder at (alias (SubFolderPath)) with properties {name:"Admin"}
	make new folder at (alias (SubFolderPath)) with properties {name:"Ancillaries"}
	make new folder at (alias (SubFolderPath)) with properties {name:"Art"}
	make new folder at (alias (SubFolderPath)) with properties {name:"Cover"}
	make new folder at (alias (SubFolderPath)) with properties {name:"Design_Templates"}
	make new folder at (alias (SubFolderPath)) with properties {name:"Documentation"}
	make new folder at (alias (SubFolderPath)) with properties {name:"Editorial"}
	make new folder at (alias (SubFolderPath)) with properties {name:"Fonts"}
	make new folder at (alias (SubFolderPath)) with properties {name:"Pages"}
end tell

-- Gives another name to the new layer of folders, which probably isn't necessary.
set EditFolderLoc to SubFolderPath as Unicode text

-- Creates the path into the Editorial folder
set EditFolderPath to (EditFolderLoc & ":Editorial") as Unicode text

-- Creates the Editorial folders in the Editorial (ooh, redundancy!)
tell application "Finder"
	make new folder at (alias (EditFolderPath)) with properties {name:"Ancillaries"}
	make new folder at (alias (EditFolderPath)) with properties {name:"Art"}
	make new folder at (alias (EditFolderPath)) with properties {name:"Documentation"}
	make new folder at (alias (EditFolderPath)) with properties {name:"Drafts"}
	make new folder at (alias (EditFolderPath)) with properties {name:"Launch_Materials"}
	make new folder at (alias (EditFolderPath)) with properties {name:"MS"}
	make new folder at (alias (EditFolderPath)) with properties {name:"PandL_Contracts"}
	make new folder at (alias (EditFolderPath)) with properties {name:"Reviews_Surveys"}
	make new folder at (alias (EditFolderPath)) with properties {name:"Sales_Marketing"}
end tell

-- The process can be repeated any number of times for different folders, and new layers.

This is my vanilla AS code for creating a folder hierarchy:

Jon


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

And here’s a recursive one for the collection:

realiseFolderPath((path to desktop as Unicode text) & "path:to:folder:")

on realiseFolderPath(folderPath)
  script o
    on rfp(folderPath) -- recursive subhandler
      tell application "Finder"
        tell folder folderPath to if (it exists) then return it
        set folderName to text item -1 of folderPath
        set containerPath to text 1 thru text item -2 of folderPath
        return (make new folder at my rfp(containerPath) with properties {name:folderName})
      end tell
    end rfp
  end script
  
  if (folderPath ends with ":") then set folderPath to text 1 thru -2 of folderPath
  set astid to AppleScript's text item delimiters
  set AppleScript's text item delimiters to ":"
  set diskName to text item 1 of folderPath
  if (diskName is not in (list disks)) then
    set AppleScript's text item delimiters to astid
    error "realiseFolderPath(): Disk " & diskName & " wasn't found."
  end if
  
  set folderAlias to o's rfp(folderPath) as alias
  set AppleScript's text item delimiters to astid
  
  return folderAlias
end realiseFolderPath