Automator: How do I create folders from a text file with a list of names for the folders?

Hello!


I'm a long-time Mac user (graphic designer), but new to using Automator.


Here's what I am trying to accomplish (without paying for Zapier).


I receive text files from my clients contain a list of names separated by carriage returns, and I need to make project folders using the names in those lists. Sometimes the list is rather long so I am looking for a solution to automate this.


I have spent two days trying to replicate this process with Automator on my Mac, to no avail.


I've learned how to automatically create folders by manually typing the folder names into the Automator app, which isn't useful because the folder names are different for every client. (This automation was basically how to create an empty structure of folders for every new client or project so all project files are consistent).


Any help would be greatly appreciated!


Thank you in advance kind people who are much smarter than me.


g

MacBook Pro with Touch Bar

Posted on Aug 27, 2020 2:24 PM

Reply
Question marked as Top-ranking reply

Posted on Aug 28, 2020 10:27 PM

Yes, in order to make it work, you need either to go into the directory containing the folders.txt file and then execute the command or add the complete path where the file resides in the command itself.


Example for the former scenario. Assuming the folders.txt is saved in a folder named foobar on your Desktop, you type in Terminal:


cd

add a space

drag from the finder the foobar folder which contains the folders.txt


The result should be something like this (maybe ThatAdGirl will be different – it's your username on Mac):


cd /Users/ThatAdGirl/Desktop/foobar/


Press Enter.


Now you're inside the foobar folder where you can execute the command and where the folders will be created.


The example for the latter scenario is similar. In Terminal type:


cat

add a space

drag from the finder the folders.txt file

add a space and add | xargs mkdir


The result should be something like this (again, maybe ThatAdGirl will be different for the same reason as above):


cat /Users/ThatAdGirl/Desktop/foobar/folders.txt | xargs mkdir


Press Enter.


Consider that the folders are created in the directory where the command is executed. So, in this case, assuming that you started your Terminal session in your home directory, the folders will be created in /Users/ThatAdGirl/ and not inside your foobar folder.


Follow the useful tip provided by VikingOSX to prevent errors due to different line endings.


BTW, I'm a graphic designer as well. Maybe you can achieve the same result with Automator. I guess that you'd still have to rely somehow on Terminal or Apple Script, though.



Similar questions

15 replies
Question marked as Top-ranking reply

Aug 28, 2020 10:27 PM in response to ThatAdGirl

Yes, in order to make it work, you need either to go into the directory containing the folders.txt file and then execute the command or add the complete path where the file resides in the command itself.


Example for the former scenario. Assuming the folders.txt is saved in a folder named foobar on your Desktop, you type in Terminal:


cd

add a space

drag from the finder the foobar folder which contains the folders.txt


The result should be something like this (maybe ThatAdGirl will be different – it's your username on Mac):


cd /Users/ThatAdGirl/Desktop/foobar/


Press Enter.


Now you're inside the foobar folder where you can execute the command and where the folders will be created.


The example for the latter scenario is similar. In Terminal type:


cat

add a space

drag from the finder the folders.txt file

add a space and add | xargs mkdir


The result should be something like this (again, maybe ThatAdGirl will be different for the same reason as above):


cat /Users/ThatAdGirl/Desktop/foobar/folders.txt | xargs mkdir


Press Enter.


Consider that the folders are created in the directory where the command is executed. So, in this case, assuming that you started your Terminal session in your home directory, the folders will be created in /Users/ThatAdGirl/ and not inside your foobar folder.


Follow the useful tip provided by VikingOSX to prevent errors due to different line endings.


BTW, I'm a graphic designer as well. Maybe you can achieve the same result with Automator. I guess that you'd still have to rely somehow on Terminal or Apple Script, though.



Sep 2, 2020 11:28 AM in response to ThatAdGirl

I was prepared to help you with an Automator solution. If the text file is always services.txt in the folder where you want to create the service name folders, then it is a simple prompt for the parent folder, and then a very short script to generate the folders.


The following Automator application works as expected to create sub-folders in a specified parent folder based on the line items in the services.txt file, which is already located in the parent directory.



Steps:

  1. Launch /Applications/Automator
    1. New Document on your Desktop as Application, then click Choose.
  2. Click the Files & Folders library on the left, and on the top of the actions list is Ask for Finder Items.
    1. Drag and Drop Ask for Finder Items into the larger workflow window as you see above. Imitate my settings.
  3. Click the Utilities Library, locate the Run Shell Script action, and drag and drop it below the previous action. Imitate my settings for Shell and Pass Input. Remove all content that is automatically added to the Run Shell Script action, and replace it with the following content.
  4. Before you save this Automator application to your Desktop, click the Run button in the upper right corner to confirm that it creates new folders in the selected Parent folder containing the services.txt file.
  5. Save, and give it a meaningful name. You double-click the Automator application to run it.


Code to copy/paste into the now empty Run Shell Script action following step 3. Ensure you select it all because it is scrolling off the page to the right:


PARENT="${@}"

< "${PARENT}"/services.txt /usr/bin/tr -s "\r\n" "\0" | /usr/bin/xargs -0 -I {} /bin/mkdir -p "${PARENT}/{}"




I have only tested this solution on Mojave 10.14.6 (18G6020) at this time.

Sep 2, 2020 9:35 AM in response to ThatAdGirl

Thank you to both Marco and VikingOSX for solving my problem. I appreciate both of your efforts. To make it simple for the next person who comes along with the same question, here is the resolution in one tidy place:


How-to create folders from lines in a text file using Terminal on Mac



Where services.txt is a file containing lines of text that need to become the names of folders, open terminal on Mac


In Terminal type the following:


cd

add a space

drag from the finder the parent folder which contains the services.txt


Press enter


You are now inside the folder where you want the folders created. Now paste into Terminal:


< services.txt tr -s '\r\n' '\0' | xargs -0 -I {} mkdir "{}"


Press enter


Your folders should now be created inside your parent folder.


It worked! Now I may try to create an automator app based on this result.


Aug 29, 2020 8:16 AM in response to ThatAdGirl

You will need a parent folder name, within where you plan to create your new project folders, and the location of the servicenames.txt file. Automator actions can prompt you for those items, and then a third action would proceed to make the folders from the text file entries.


Do you expect any additional folder hierarchy beneath the folders created from the text file?



Aug 28, 2020 11:50 AM in response to Marco Klobas

hmmm. okay. I'm a graphic designer, and have never needed to open terminal before, let alone pass it a line of code (although, I'm dating myself here, running software from a disk on PCs way back in the ages of dinosaurs looks very similar).


So, I changed folders.txt to the name of my project servicenames.txt and left the rest of the command line the same.


I pasted that into terminal and hit return.


I got an error message about the directory not existing.


I suppose terminal needs a longer path to the folder where the text file resides.


So, if my folder is in the documents folder of my mac in a subfolder with my client's name, what should my terminal command be?


mymac/documents/clientname/folder.txt


Am I getting warm?


Dang. Should have learned to talk to my computer better. Was really hoping Automator could do this (although, honestly that's complicated enough).

Aug 28, 2020 11:57 AM in response to Marco Klobas

The challenge you run into is where the folders.txt file may have originated from a Windows platform, and you have to handle the different line endings, in addition to white-space and other characters that may be in the individual line items.


To wit, folders.txt:

ABC Business Products
Dante's Marshmallow Roasters
Yours & Mine Wine Consumers


The following will handle both types of line endings, and replace each line ending with a single '\0' to set it up for xargs:


< folders.txt tr -s '\r\n' '\0' | xargs -0 -I {} mkdir "{}"


which in the above example, creates new folders in the current directory location based on each line in the folders.txt file.


Sep 2, 2020 4:42 AM in response to ThatAdGirl

How about an Automator workflow that prompts you for the parent folder where the .txt file resides that contains the list of folder names, and if that .txt file name changes, then 1) a second prompt for that .txt file, or if it is the only .txt file in that parent folder, then that is sufficient to avoid the second prompt, and commence folder creation?

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Automator: How do I create folders from a text file with a list of names for the folders?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.