I have done a lot of research and cannot find a simple solution for renaming a file by its parent folder.

Basically I am zipping a bunch of files from an online file manager and I receive them in a ridiculous structure. (see below) the first column is the extracted parent folder which names itself after the last folder in the set when I zip it. The second column is the actual order numbers for each order. The third column is what seems to be randomly named folders which each contain ONE customer uploaded file. So, each file a customer uploads for an order gets put in a randomly-named folder, then into the order number folder.

current structure

I would like to make an app or service with Apple Automator to rename the customer files like this (and get rid of all the subfolders)leaving them all in the parent folder named sequentially .


any help/insight?

  • Are Order# and SomeAutomatedFilename directories? Can you post the output of a sample ZIP via unzip -l downloaded.zip ... cheers – tink Apr 6 '16 at 21:44

You can do this with an AppleScript. Use the Script Editor in the Utilities folder. Here is a script I wrote for you just now. It works on one folder at a time-- not the top level folder, but the one in your example that is 3587915.

When you run this script it asks you to choose a folder. In this example you would open 3587915 and then click "Choose." Very important: don't choose your 3589919. We are only working on one folder at a time here. If this works for you we can expand to looping through the folders in the top-level folder.

tell application "Finder"
    set the_folder to choose folder
    set the_folder_name to name of the_folder
    set the_subfolders to every folder in the_folder
    repeat with I from 1 to count of the_subfolders
        set name of every file of item I of the_subfolders to the_folder_name & "_" & I & ".pdf"
        move every file of item I of the_subfolders to the_folder
    end repeat
    -- optionally delete the subfolders
    delete every folder in the_folder -- put two dashes in front of this line to comment it out
end tell

Here are screenshots showing you how it works. You should practice on a set of folders that don't matter at first. You can see my starting set-up in the first picture and the result in the second. The captions on the pictures are not showing up (for me) so here is what they should be:

Picture 1: Automator Service, with slightly modified AppleScript.

Picture 2: folder_1, folder_2, folder_3, folder_4, and folder_5 each have a single pdf in them. Only folder_1's contents are shown here.

Picture 3: This is the result. The subfolders are gone. The files are named after the parent folder.

You can save the script as an app. Or, you can run the script within a step in Automator since Automator can run an AppleScript. That's in the Utilities section of Automator, "Run AppleScript." I would include a picture but I am new here and am only allowed two pictures until I am upgraded.

If you want to do it with a service, you use almost the same script, but you have to pass the control-clicked folder. Rather than using the AppleScript step "Choose Folder" you tell your Automator Service to "receive selected folder in the Finder" and you set "the_folder" to item 1 of the input. See the first picture. Works like a champ here!

Automator Service, passing the control-clicked folder

folder_1, folder_2, folder_3, folder_4, and folder_5 each have a single pdf in them. Only folder_1's contents are shown here

This is the result. The subfolders are gone. The files are named after the parent folder.

  • Thanks so much Christian, this definitely works and saves a bunch of headaches. Is it possible to make it a droplet or a service in a way that I don't have to choose the folder, but it applies to whatever folder(s) I ctrl-click on? – Becky Piscitelli Apr 7 '16 at 20:10
  • Becky, you're welcome. I edited my original post to show the Automator Service method. Thanks to my friend Ben Waldie for reminding me how to pass a folder to the AppleScript. Make that service in Automator and it will be available to you anytime you control-click a folder. – Christian Boyce Apr 8 '16 at 2:15
  • Neat! I would have done this with a shell script. (: – SilverWolf Jan 3 '18 at 14:30

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.