Create hudreds of folders named after a list.txt - Script or Automator or ?

Discussion in 'Mac Programming' started by ign, Jun 16, 2009.

  1. ign
    macrumors member

    Joined:
    Mar 16, 2004
    Location:
    Firenze
    #1
    Hello,

    I need to create some hundreds of folders with names from a list (text file).
    I would like to avoid creating and naming them one at a time manually!

    I was wondering if I should use Automator (haven't figured out how though)
    or applescript.

    Has anyone done something like this before? any hints?

    thank you!
     
  2. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    I'm sure this is possible with either of those, but i would go to shell because i know it better.

    cat list.txt | awk '{system("mkdir "$1)}'

    Or something similar.

    -Lee
     
  3. ign
    thread starter macrumors member

    Joined:
    Mar 16, 2004
    Location:
    Firenze
    #3
    Yup!

    much better a single line than trying to figuring out a way in Automator...

    here's the magical line I used in terminal:

    cat dirlist.txt | xargs mkdir

    it created about 400 folders with the names found in dirlist.txt
    in a second !

    Thanks again, Lee!
     
  4. tbell, Feb 24, 2012
    Last edited: Feb 24, 2012

    macrumors newbie

    Joined:
    Jun 25, 2009
    #4
    The terminal trick worked great! Thanks! A couple notes:

    1) In dirlist.txt, enclose multiple-word folder names in quotes because otherwise the command makes a folder for each word separated by a space. Ex: ["Test 1"] in the text file creates one folder [Test 1] (without brackets). But just [Test 1] in the text file creates two folders, [Test] and [1].

    2) I had to use this trick to make several hundred folders from a list of cell values in an Excel column. The following steps outline how to do this process:

    - If the cell values have multiple words you'll need an easy way to enclose all the words in quotes. To get quotes around the cell values and a space afterward, create a new column and copy this CONCATENATE function down the new column:

    =CONCATENATE("""",Cell_Reference,""""," ")

    - Where Cell_Reference is the cell with the desired folder name. Due to the way Excel handles quotation marks in its formulas, you need four quotation marks in a row to obtain one quotation mark in the formula result. The above function would return a value of ["Cell_Reference" ] in the forumla cell (without brackets of course, just using to show the space afterward).

    - The next necessary step is to paste the cell values that are currently listed down a column as a row. So copy the range of desired folder names in Excel and from the Edit menu select Paste Special. In the box that opens, select "Values" and check the "Transpose" box, and paste to an empty row. This should line the column values up horizontally. Then copy & paste that row into your plain-text dirlist.txt file and you should be good to run the command.

    3) PS for those not good with Terminal I've included a ZIP of a script you can place in the directory where you want the folders to be (make sure the dirlist.txt is there too) -- it'll open your terminal window there.
     

    Attached Files:

  5. macrumors 6502a

    Joined:
    Jan 1, 2005
    #5
    The "OpenTerminalHere.app" does not work for me on Mac with OS X 10.8.4 (Mountain Lion). I place such application in a folder containing the dirlist.txt file which contains, for instance:

    "First name this" "Second name this" "Third name this"

    In such a case, I get the following error when opening OpenTerminalHere.app:

    cd '/Users/me/Desktop/Folder/' && echo $'\ec'
    Illegal variable name.

    How to fix that? Thanks.
     
  6. macrumors G4

    Joined:
    Jan 5, 2006
    Location:
    Redondo Beach, California
    #6
    THat is what I'd do too. Only I like "see" better than "awk".

    "sed" is a kind of text editor. I'd use a text edit command to turn a line from the text file to a shell command to create the folder. The sed editor output is put inside back quotes so it gets executed by the system.


    The other way if this is a one time deal is to use a text editor to manually add "mkdir " to the front of each line then you have transformed the file to a script. Run the script.
     
  7. macrumors 6502a

    Joined:
    Jan 1, 2005
    #7
    How to use it? If I do it in Terminal I get:
    cat: dirlist.txt: No such file or directory

    If I drag and drop the Folder Text containing dirlist.txt into Terminal and paste
    cat dirlist.txt | xargs mkdir
    and carriage return, I get:
    /Users/me/Desktop/Test: Permission denied.

    If I drag and drop the Folder Text containing dirlist.txt into Terminal, type
    sudo
    and space and paste
    cat dirlist.txt | xargs mkdir
    and carriage return, I get:
    /Users/me/Desktop/Test: Permission denied.

    Sorry for my ignorance with Terminal. Thanks.

    ----------

    If I paste
    mkdir aa bb cc
    into the dirlist.txt file and change its name to dirlist.scpt and open and run it with AppleScript Editor.app, I get:

    Syntax Error
    A identifier can’t go after this identifier.

    How to fix it? Thanks.
     
  8. kryten2, Jun 25, 2013
    Last edited: Jun 25, 2013

    macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #8
    I'm guessing your dirlist.txt file is in folder Test and not in Text? Open Terminal and type in cd followed by a space(I mean cd and press the space bar), drag your Test folder from Finder into the Terminal window and press return. Then type or paste the cat dirlist.txt | xargs mkdir line into the Terminal window and press return.
     

    Attached Files:

  9. macrumors 6502a

    Joined:
    Jan 1, 2005
    #9
    Yes. And it works great with your directions. Many thanks!!!

    Text file (dirlist.txt) generated with BBEdit 10.5.1 - Save as Unix (LF) - Unicode (UTF-8) on Mac.

    When using names of persons (that is, name and surname to become each folder), I had to use quotes and spaces to separate the full name of each person (name and surname) from the ones of the other persons, and the quotes had to be plain like "these" (not smart or curved) for it to work properly. No need to use commas.

    You made my day! Many thanks again!
     
  10. macrumors newbie

    Joined:
    Oct 22, 2009
    #10
    Make Folders from a text file

    I had used an application that generates folders with names listed in a text file.

    In the application, simply select a file saved in text format. The application will pick all these text (paragraph) and name that folder with that text.

    Sorry. The bad news is that was greater than 3 years ago and it should be a PPC application. It have a name (if I recall it well) MakeFolder.

    I have been trying to hunt this for some time now but failed.

    Wish someone here can make such an application. Thank you in advance.
     
  11. macrumors newbie

    Joined:
    Oct 22, 2009
    #11
    Make Folders from a text file

    After my earlier posting, I found an AppleScript that does it.

    In this webpage (http://macgrunt.com/2011/10/25/create-folders-from-list-ii/), it is listed two scripts.

    I copy the first, use AppleScript Editor and saved as an application.

    Then, I make a list of folder names I want to create, type it in paragraphs, save it as a Text file (yes, a text format file. TextEdit do not support Save As a text format file).

    I launch the AppleScript made earlier, select the Text File, voila! Folders are created in the same folder as where the Text File was picked.

    Cheers

    and THANKS to the contributor at macgrunt.com.
     
  12. macrumors 68040

    Joined:
    Feb 2, 2008
    #12
    What is the typical use case for needing to create hundreds of empty folders? :confused: Doesn't it take about as long to create the text file with the names typed in, as to create the folders manually?

    Actually it does.
     
  13. macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #13
    I suppose it depends on what your workflow is doing. I have an Automator action that can create folder structures from text, but usually it is easier to just copy the structure.
     
  14. macrumors newbie

    MacGrunt

    Joined:
    Jul 25, 2011
    #14
    As Red Menace says, it depends on your workflow. Where I work I need to create a dozen or two dozen job folders at a time. Copy/pasting the (mostly consecutive) job names from an existing database into a single text file is much, much quicker than copy/pasting the individual names to the individual folders. But, I agree, if you were typing the names in by hand, it wouldn't make too much difference.

    The main one I use is something more like this version — altered to also create a bunch of common subfolders : http://macgrunt.com/2011/10/05/create-folders-from-list/

    The one that marbacha mentioned was created for someone with different workflow requirements.

    m.
     
  15. macrumors member

    Joined:
    Jun 16, 2008
    Location:
    London, UK
    #15
    Thank you, you absolute legends!

    Saved a good few hours of work that I can now spend procrastinating :)

     
  16. macrumors member

    Joined:
    Jun 16, 2008
    Location:
    London, UK
    #16
    Good tip on BBEdit. You can easily add the prefix/suffix quotation marks you need with this using menu - text - prefix/suffix lines... also saves to a proper text file.

    For those wondering why this might be useful, I needed to make 630 folders based on a web list, so I didn't need to type anything, just copy, paste, strip the hypertext junk, add the quotation marks, and run the terminal code. Saved 630 manual copy and paste operations, each taking 6 or 7 clicks and key combinations. That is a hell of a lot of labour avoided.

    The internet is full of people who help solve problems, they are great, and people who tell you your problems are invalid, they are not so great...

    If anyone in the future is stuck with this feel free to PM for a walkthrough.


     

Share This Page