Laguna de los Curas, Pichilemu

On why everyone should learn how to Bash and how to create 2,300 folders named from a spreadsheet file with one click

Fernanda is a really good photographer, but her knowledge about computers its limited to the apps she usually needs for her work, like Capture One and Lightroom. However, most of her advertising work is to deal with huge catalogs of clothing and accessories, each one needing multiple shots. While working with a single project is simple in terms of organization, working with thousands of different products with multiple angles can be a mess.

That’s why they use a long, and sometimes overwhelming, spreadsheet with all the SKUs and descriptions. Keeping an organized folder with all the photos of these products means creating as many folders as products you have. Yeah… exactly… thousands of folders, with subfolders, named something long and weird like: SKU123123AB20Z.

But wait, we have a really cool car traveling trough the galaxy and plans to send humans to Mars, so why do we make our photographers make folders by hand!?!

Enter, Apple Automator

Automator, the cute but probably paranoid pipe bender robot

In every single Mac, since OSX Tiger (2005), Apple has installed a cute little app called “Automator”, that, in their on words “Automates the tasks you do on your Mac — even if you don’t know any programming — by using built-in actions to create workflows.”

So, with a little bit of imagination, curiosity and web searching abilities you can make small apps that can help you do repetitive tasks, such as “Batch rename files” or “Transform PDFs to JPGs”.

But for now, lets get back to Fernanda’s predicament.

So, there is this new project where she needs to take photos for the products of a nice, cool client who luckily has all the product SKUs in a nice, organized spreadsheet file. Now, Fernanda needs to create a working folder structure with ALL the product SKUs as folders with 4 subfolders inside, one for each of the angles of the product.

Basically, it looks something like this: Line / SKU1234 / SKU1234_Top

Doing the thousand of folders and subfolders by hand is absurd, so with Automator and a little bit of CSV and Bash magic we can create a simple App to convert the list of SKUs from our spreadsheet into folders.

The CSV

A CSV is basically a text file with Comma, Separated, Values. It can be opened by any text and spreadsheet editor and it looks like this:

A CSV File opened in TextEdit
A CSV File opened in Numbers

OK, so it’s a table with numbers. How we can use this to create folders!?

Well, let’s open Automator!

Apps / Automator

Now, we have our CSV file with all the SKUs that we need to transform to folders, so let’s create an app for that.

Automator Document Selection Screen

We will need to tell Automator to ask us for a file, in this case, our CSV file:

Files & Folders / Ask for Finder Items

Right, now for the tricky part. We are going to use some Bash power to handle the file, the content and the folder creation.

Bash is basically a shell and a command language first released in 1989. Yeah, an oldie but a goodie ❤, so good every single Mac uses it today.

Bash allows us to handle simple things, like creating folders and reading files, using basic instructions and handling the information for us to perform actions with them.

So, let’s write some Bash!

We made Automator ask us for a file, now we need to handle it.

First we need to add a Run Shell Script Module to our app:

Utilities / Run Shell Script

And the first line is basically to know where our app is:

cd “${1%/*}”

Here, “CD” is the command for “change directory” and “${1%/*}” is the simple command to learn where the path for file Finder asks us to open is.

Then we need to get the name of the file we just opened, and store it in a variable we can use to create the base folder where all the subfolders will exist.

BaseDir="${1%%.*}"

Then, we create the first folder, which will be named after the CSV file we opened.

mkdir "$BaseDir"

Now our app hasopened a CSV file and created a Folder named as the file. That was the simple part. Now we need to read the file, get each SKU and create a folder named after each one.

So, we will use a feature that allows us to go through each line of the file and get the content of it, to store it as a variable to use it to create a folder, like we just did for the file.

while read line
do
FolderName=${line%,*}
done < "$1"

This goes through the file, line by line, stores the line content in a variable called $FolderName and when it’s done, it stops.

So, now we have the SKU! Let’s create our folder structure!

while read line
do
FolderName=${line%,*}
 mkdir "$BaseDir"/"$FolderName"

mkdir "$BaseDir"/"$FolderName"/"$FolderName"_top
mkdir "$BaseDir"/"$FolderName"/"$FolderName"_front
mkdir "$BaseDir"/"$FolderName"/"$FolderName"_left
mkdir "$BaseDir"/"$FolderName"/"$FolderName"_right
done < "$1"

For this we use the “mkdir” command that basically allows to create an empty folder wherever we want, so we use the $BaseDir, which is the name of our file, then we create a subfolder on it, with the name of our SKU and inside that we create 4 different folders for each of angles of the product.

The end code would look like this:

cd "${1%/*}"
BaseDir="${1%%.*}"
mkdir "$BaseDir"
while read line
do
FolderName=${line%,*}
mkdir "$BaseDir"/"$FolderName"
mkdir "$BaseDir"/"$FolderName"/"$FolderName"_top
mkdir "$BaseDir"/"$FolderName"/"$FolderName"_front
mkdir "$BaseDir"/"$FolderName"/"$FolderName"_left
mkdir "$BaseDir"/"$FolderName"/"$FolderName"_right
done < "$1"
The end result!!

So let’s use it!!

We have our app and our CSV File

When we open our app, it will ask us for our CSV file:

Select File Dialog

And then, the magic happens! It processes our file and creates our folders and the result will look like this:

Folders inside folders!!!

And that’s it! We just saved tens of hours spent manually creating folders that probably would have errors because we are human. But with the power of Bash and some Automator magic, we have an app to create folder structures with ease.

TLDR;

We can use BASH to make our life better and create simple apps for complex workflows that would otherwise drive us nut.

Thanks for reading, and don’t forget to check Fernanda’s awesome work.