Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Automator: Create folder from filename

I am trying to use Automator to query a file in a foldr, then create a folder based off of the filename, then move the file into the newly created folder. I am not as savvy as I thought that I was when it comes to Automator. Even thinking that Automator might not be the correct tool for the job. Each file has a different name or this would be a lot easier. Any help would be greatly appreciated.

Posted on Dec 23, 2012 8:02 PM

Reply
Question marked as Best reply

Posted on Dec 23, 2012 8:55 PM

So I found this Script:


tell application "Finder"

set current_folder to folder of front window

set x to (get the selection) as list

repeat with i from 1 to the count of x

set this_file to item i of x

if i is not 1 then

set previous_file to item (i - 1) of x

set prev_ext to cur_ext

set prev_name to new_name

else

set prev_name to ""

end if

set cur_ext to name extension of this_file

set new_name to text 1 thru -((length of cur_ext) + 2) of (name of this_file as text)

if new_name is not equal to prev_name then

set new_folder to makenewfolderwith properties {name:new_name} atcurrent_folder


movethis_filetonew_folder

else


movethis_filetonew_folder

end if

end repeat

end tell


But it only works if the file is selected in the finder window. How can it be modified to select the file/files in a folder to process them ?

24 replies

Feb 14, 2013 10:43 AM in response to alexgsrubio

The script does not try to match files with a particular folder, they are just moved into a folder that has the same name as the file, with the folder getting created if needed. Since everything does have the same name except for the one with the "LOW" suffix, the modification just removes the suffix before getting the name of the folder.


The statement


setdestinationto (makeNewFolderfor (text1thru-5oftheName) attheContainer)


strips the " LOW" suffix (the last 4 characters) from the file name before moving it to the folder, so if you are getting another folder (such as one with a trailing space), then there is more than one space separating the suffix. If there are multiple spaces separating the "LOW" suffix, give the following statement a try:


setdestinationto (makeNewFolderfor (text1thruword-2oftheName) attheContainer)


This statement will strip off the last word, including however many spaces are separating it (there are other characters used to determine word bountaries, but in this case I am assuming that the words are separated by spaces).

Feb 14, 2013 12:10 PM in response to red_menace

Sorry!! My mistake! I think I missed something while editing the script. It works great!. It puts all my 4 files into one single folder!.


This is the final script:


on run {input, parameters} -- create folders from file names and move


set output to {} -- this will be a list of the moved files


repeat with anItem in the input -- step through each item in the input

set {theContainer, theName, theExtension} to (getTheNames from anItem)

try



# check for a suffix and strip it off for the folder name

if theName ends with " LOW" then

set destination to (makeNewFolder for (text 1 thru -5 of theName) at theContainer)

else

set destination to (makeNewFolder for theName at theContainer)

end if



tell application "Finder"

move anItem to destination

set the end of the output to the result as alias -- success

end tell

on error errorMessage -- duplicate name, permissions, etc

log errorMessage

# handle errors if desired - just skip for now

end try

end repeat


return the output -- pass on the results to following actions

end run



to getTheNames from someItem -- get a container, name, and extension from a file item

tell application "System Events" to tell disk item (someItem as text)

set theContainer to the path of the container

set {theName, theExtension} to {name, name extension}

end tell

if theExtension is not "" then

set theName to text 1 thru -((count theExtension) + 2) of theName -- just the name part

set theExtension to "." & theExtension

end if

return {theContainer, theName, theExtension}

end getTheNames



to makeNewFolder for theChild at theParent -- make a new child folder at the parent location if it doesn't already exist

set theParent to theParent as text

if theParent begins with "/" then set theParent to theParent as POSIX file as text

try

return (theParent & theChild) as alias

on error errorMessage -- no folder

log errorMessage

tell application "Finder" to make new folder at theParent with properties {name:theChild}

return the result as alias

end try

end makeNewFolder


One more question: Where do I put that your response was the one that solved my question?

Jul 15, 2013 5:21 PM in response to red_menace

Hi red_menace,


The first post you made with the AppleScript works wounderfully! Thank you!

Could you or perhaps someone else be so kind and help me modify it slightly? I would like the script to do one extra thing.

Once a directory is made(as well as file moved) inside the newly created directory, i need it to create a second directory eg."other-stuff" or possibly even a 3rd dir.
How could this be done?


Thanks very much, in advance!


PS- since its just a small modification, i didint think a new thread was necessary. So i thaught ill keep it in the same place.

Jul 15, 2013 7:21 PM in response to tiqtaq

You can add statements in the Finder tell statement where the file is being moved into the destination folder. Note that the original script adds the results of the move (the path of the file) to the output of the action so that it is available for further processing. If you want the other folder paths to be output you can use a similar statement to add to the output, otherwise you can just do something like the following:


tellapplication"Finder"

moveanItemtodestination

settheendoftheoutputtotheresultasalias-- success

makenewfolderatdestinationwith properties {name:"other-stuff"}

endtell

Feb 2, 2014 6:39 AM in response to bullware

Hi guys, can you please help me with the following pattern? So I've got folder with miltiple files named as this:



AP1077-10 blu_green-14L_1.tif

AP1077-10 blu_green-14L_2.tif

AP1077-10 blu_green-14L_3.tif

AP1077-10 blu_green-14L_4.tif

AP1077-10 blu_green-14L_5.tif

AP1077-10 blu_green-14L.tif

AP1090-01 black-14L_1.tif

AP1090-01 black-14L_2.tif

AP1090-01 black-14L_3.tif

AP1090-01 black-14L_4.tif

AP1090-01 black-14L_5.tif

AP1090-01 red-14L_1.tif

AP1090-01 red-14L.tif

AP1090-02 black-14L.tif

- and so on


I need this files to be splitted to corresponding folders, e.g. AP1077-10 blu_green-14L_1.tif - AP1077-10 blu_green-14L_5.tif and AP1077-10 blu_green-14L.tif go to folder AP1077-10 blu_green-14L and so on.


Every sequence containes files and has the same structure, several files with suffixes from _1 up to _5 and one file with no suffix - and this one with no suffix should give a name for a folder (with no extention of course). Sometimes there a sequences containing only one file.


Please note that the filename itself may contain symbol _. Thanks in advance!

Aug 29, 2014 9:26 AM in response to red_menace

I've started using the original script you posted at the top of the thread and it's awesome! Thank you for helping out @red_menace


Hopefully you can help me out with this...


I have a bunch of files that have similar names to them, but I don't want every to be put into its own folder, but all files with the same string of characters should go into the same folder.


For example all of these files names that end with the same integer (003) should be dumped into the same folder:

  • Boston_ProRes422_1920x1080_24p_Audio_003
  • Boston_ProRes422_3840x2160_24p_Audio_003
  • Boston_ProRes422_3840x2160_24p_RAW_Audio_003
  • Boston_ProRes422_1920x1080_24p_Audio_002
  • Boston_ProRes422_1920x1080_24p_Audio_001
  • Boston_ProRes422_1920x1080_24p_Audio_010


Any help at all would be incredible!!


Thanks!

Automator: Create folder from filename

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