Using Automator to create sequential folders

I'm trying to make an Automator workflow that will grab a file, create a folder with today's date, create a new sequentially numbered subfolder, and copy that file into it. Essentially "To Post/2013_06_11/1", then on the next run it will make "To Post/2013_06_11/2", etc. I can get this to work once, but after the initial "1" folder is created I can't get it to make 2, 3, etc when I run it later. I can get this to work if I make all folders at once, but that doesn't fit our pipeline as we drop files throughout the day. Is there a way to do this in Automator, or does it require an Applescript to recognize the existing numbered folders? Thanks!

Automator-OTHER, OS X Mountain Lion (10.8.4)

Posted on Jun 11, 2013 4:12 PM

Reply
4 replies

Jun 12, 2013 10:54 AM in response to steampower

First off, using / in folder names isn't a good idea, since this is a directory delimiter in UNIX and may cause other problems.


That said, working this kind of logic into Automator is, mostly easy, the issue is parsing the folder names to find the last-used digit (Automator doesn't do this well, IMHO.


You can use a series of Automator Actions to:


Get Specified Finder Items (get your base directory)

Set Value of Variable (store this value in a variable for re-use)

Get Folder Contents (find all the items in it)

Filter Finder Items (filter out the directories)

Sort Finder Items (sort them)


and now you'll have a list of the subfolders. The tricky part (in Automator) is then iterating through those, extracting part of the file name to find the last-used number.


So that's where I'd just use a Run AppleScript Action with something like:


on run {input, parameters}

tell application "Finder"

set n to name of last item of input

set {od, my text item delimiters} to {my text item delimiters, "/"}

set thelastDigit to last text item of n

set my text item delimiters to od

return thelastDigit + 1

end tell

end run


This script takes the last item in the list (which was sorted by name), extracts the last element (note the above caveat of using '/' in file names) and returns the next-highest number


You should then:


Set Value of Variable (store this number in a variable for re-use)

New Folder (create a new folder using the variables you created in earlier steps to define the folder name ( e.g. "PathTo-<Today's Date>-<NewFolderNumber>" and the location where to create it (from step 2).


(note that in the above, the bold text indicates the name of the actions to apply)


Hope that helps.

Jun 12, 2013 7:55 PM in response to Camelot

This is great! I should've explained more clearly, the / in To Post/2013_06_11/1 was to show the folder hierarchy, it's not in the name itself. The only issue I'm having is the Applescript throws an error when it tries to create the first folder of the day, it says "Can't get last item of {}". It will create folder 1, but won't copy the file into it. After that though, it runs perfectly. Any idea what might fix that?


I set up my workflow like this:


New Folder: Today's date

Set Variable: Today's date folder

New Folder: named 1 in Today's date folder


Get variable: Today's date folder

Get folder contents

Filter Finder items for folders

Sort Finder items by name, ascending

Run your Applescript

Set variable: New sequential number


New folder: named new sequential number, today's date folder

Set variable: New sequential number folder


Get selected Finder items

Copy Finder items to New sequential number folder


Thanks again Camelot, very helpful.

Jun 13, 2013 3:11 PM in response to steampower

You might have a look at the following workflow, which I came up with using the ancient Tiger OS's version of Automator. With this approach, it's assumed that you have an existing destination folder into which the new subfolders will be deposited. In this example I created a folder on my desktop and named it "Destination Folder."


Once the workflow is saved as a Finder Plug-in (in later versions of Automator, I believe you would create a Service), the actions are initiated after right-clicking on the desired file(s) and selecting the workflow from a contextual menu. The files are then copied to a new subfolder inside the Destination Folder, the new subfolder now complete with the current date and a unique sequential number. Note that the Rename Finder Items action is used twice, once to get the correct current date (so the workflow can be used tomorrow and beyond), and a second time to add the sequential number. Hope this helps; Good luck.


User uploaded file

Jun 18, 2013 5:49 PM in response to steampower

So after a lot of tweaking, I think I have it working. Two problems I encountered with Camelot's script were getting errors while creating the very first folder, and when the folder numbers get into the double digits it was only looking at the last digit (11 would be followed by 2, for example). This is what it looks like now- it's probably pretty inelegant, but it works:


New Folder: Today's date

Set Variable: Today's date folder


New Folder: named 0 in Today's date folder

Set Variable: Folder 0


Get variable: Today's date folder

Get folder contents

Sort Finder items by name, ascending

Run Camelot's Applescript, modified

on run {input, parameters}

tell application "Finder"

set n to name of last item of input

return n + 1

end tell

end run

Set variable: New sequential number


New folder: named new sequential number, today's date folder

Set variable: New sequential number folder


Get selected Finder items

Copy Finder items to New sequential number folder


Get Variable: New Sequential Number

Run Applescript:

on run {input, parameters}


display dialog "Posted to #" & {input}


return input

end run

(this shows a window showing what number it's been posted to)


Get Variable: Folder 0

Run Applescript:

on run {input, parameters}

tell application "Finder"

delete {input}

end tell

return input

end run

(I had to do a script to delete the folder from a network)

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.

Using Automator to create sequential folders

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