create folders from list II

the original create folders from list post prompted a question from Jocely. basically the question is… it’s all very good being able to create folders through applescript, but what about subfolders and whatnot? it’s such a great question — it needs its own post as a reply.

to run either of these scripts — copy it into script editor, save it out as an application, then double-click to run it. you’ll be prompted to select your csv file — then the folders will be created in the same location as the csv file.

ok, scripts build up over time — first you try something and, once you get it working, you add, refine, stabilise, improve, etc. that’s how this development progressed — the first stab at this challenge looks like this :

set mgCSVfile to choose file
open for access mgCSVfile
set mgList to (read mgCSVfile)
close access mgCSVfile

tell application "Finder"
  set mgFolder to container of mgCSVfile as string
  repeat with x from 1 to count paragraphs of mgList
    set text item delimiters of AppleScript to ","
    set mgThisList to text items of paragraph x of mgList as list
    set text item delimiters of AppleScript to ""
    
    set mgTopFolder to item 1 of mgThisList
    if (exists folder mgTopFolder of folder mgFolder) is false then
      make new folder at mgFolder with properties {name:mgTopFolder}
    end if
    set mgNewFolder to (folder mgTopFolder of folder mgFolder) as alias
    
    repeat with i from 2 to count mgThisList
      if item i of mgThisList is not "" then
        set mgSubFolder to item i of mgThisList
        if (exists folder mgSubFolder of folder mgNewFolder) is false then
          make new folder at mgNewFolder with properties {name:mgSubFolder}
        end if
      end if
    end repeat
    
  end repeat
end tell

… and it will render a csv file like this :
screen grab of basic csv file
… into a folder structure like this :
screen grab of first folder structure

well, that’s not a bad start. but what about a folder hierarchy a little more complex than that? we need to go deeper with the nesting. so here’s the second stab. and you’ll notice it’s actually a simpler script :

set mgCSVfile to choose file

open for access mgCSVfile
set mgList to (read mgCSVfile)
close access mgCSVfile

tell application "Finder"
  repeat with x from 1 to count paragraphs of mgList
    set mgFolder to container of mgCSVfile as string
    set text item delimiters of AppleScript to ","
    set mgThisList to text items of paragraph x of mgList as list
    set text item delimiters of AppleScript to ""
    
    repeat with i from 1 to count mgThisList
      if item i of mgThisList is not "" then
        set mgSubFolder to item i of mgThisList
        if (exists folder mgSubFolder of folder mgFolder) is false then
          make new folder at mgFolder with properties {name:mgSubFolder}
        end if
        set mgFolder to (folder mgSubFolder of folder mgFolder) as alias
      end if
    end repeat
    
  end repeat
end tell

… and the results using the same csv file :
screen grab of second folder structure

now we see that each line (paragraph) in the csv file renders a single nested folder structure. this is more like what we’re looking for. what do you reckon you’ll get if you run this csv file through that second script? :

screen grab of more complex csv file

you can get a copy of the CreateFoldersII app here

update : if you are in the german or dutch regions you may find that your CSV files export as semi-colon separated (rather than comma separated). in which case you would replace this line :

    set text item delimiters of AppleScript to ","

with this :

    set text item delimiters of AppleScript to ";"

macgrunt icon

Advertisements

15 thoughts on “create folders from list II

  1. Hello,
    Great work, as far as I understand. I am very interested in your script Create folders I and II to complete not a very complex hierarchy of folders from a list, but almost 300 lines (this is for a folder of a list of education training).
    Thank you !
    O.

  2. I have over four hundred folders need to create and your script provide the foundation for me to create folders from a csv file. I managed to use mulitple myCSVfile to get what I exactly want. Thank you so much and you save me hours!

    Bryan

  3. Could you use this to set the permissions (for the username to be the same as the top folder name) using the ‘owner’ and ‘owner permissions’ settings? I’m strungling to make this work within apple script editor (your code works brilliantly for what we want) and thinking that i might need to make another script to set this. Many thanks for the confirmation/pointers on this.

    • Oh yeah, that’s a great question George.
      I can’t remember doing anything with permissions before, but I’d be interested to look into it.
      Unfortunately I’m flat out for the next couple of days. I’ll get back to you as soon as I get a chance to give it a go.
      m.

  4. Hey! This is very useful. Thanks a lot. I have one more question. How do i input in the script to choose the destination folder for the folders to be created in?

    Thanks agin

    • G’day Kai
      By default the script creates the folders in the same location as the csv file.
      If you want to have the script stop and ask you for a location then change this line :
      set mgFolder to container of mgCSVfile as string
      … to this :
      set mgFolder to choose folder

      m.

  5. hey- this is awesome! any way to limit folder creation to a single column on the csv? i’m working from csv files with multiple columns of info and need to make folders from numbers is column A.

    • G’day Martin
      If you just want Column A, then you don’t need all items of each paragraph, only item 1.
      So, change this line :
      set mgThisList to text items of paragraph x of mgList as list
      to :
      set mgThisList to text item 1 of paragraph x of mgList as list

      I haven’t tested this in the context of the rest of that particular script.
      I’d actually write the script differently for this functionality — but it may work with the simple line change.
      See how you go and let us know.

      You should also change this line :
      set mgFolder to container of mgCSVfile as text
      to :
      set mgFolder to container of mgCSVfile as string
      I’ll have to update that in the post — ‘as text’ has been deprecated I believe

      m.

  6. I just found this page because I’m trying to do the opposite. Do you have an applescript that can look at a directory structure and output a CSV? Sorry I can’t figure it out on my own, I’m not an Applescript guru.

    • G’day
      I haven’t done one yet but I’m happy to have a crack at it.
      Send through a small example of a folder structure and how you’re hoping the resulting csv will look.
      I’ll be able to have a look at in when I get back in a couple of days.

      m.

thoughtful and respectful comments welcome

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s