You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

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

AppleScript or Automator for creating parent folders and renaming folders?

Hello,


I have a folder with about 300 folders in this name format:


Aaa Bbb - Ccc Ddd Eee (yyyy)


Each of these folders have a number of files within.


I want to create a new folder for each of the folders above with the following names:


Aaa Bbb


And, inside it, I want the original folder with their files to reside, but now have this name:


yyyy Ccc Ddd Eee



[yyyy is year, like 2013]



How would I go about doing this with AppleScript or Automator?



Here's an actual folder:


Alex Nussbaum - A Number Of Bits (2014)


I want it to be renamed to: 2014 A Number Of Bits

And, I want it to be placed inside a parent folder named Alex Nussbaum

Alex Nussbaum folder needs to reside in the same original folder (with the 300-odd subfolders)



Thank you!

MacBook Air 13″

Posted on Oct 20, 2023 5:22 PM

Reply
Question marked as Top-ranking reply

Posted on Oct 20, 2023 5:35 PM

Use a script such as:


tell application "Finder"

set source_folder to folder "Temp" of desktop

repeat with this_folder in (get folders of source_folder)

set the_string to name of this_folder

set output_strings to {"", ""}

set the_offset to offset of "-" in the_string

set item 1 of output_strings to items 1 thru (the_offset - 2) of the_string as string

set item 2 of output_strings to items (the_offset + 2) thru -1 of the_string as string

set item 2 of output_strings to word -1 of item 2 of output_strings & " " & items 1 thru -8 of item 2 of output_strings as string

make new folder in source_folder with properties {name:item 1 of output_strings}

move this_folder to the result

set name of the result to item 2 of output_strings

end repeat

end tell


(245986)

Similar questions

5 replies
Question marked as Top-ranking reply

Oct 20, 2023 5:35 PM in response to lx-xl

Use a script such as:


tell application "Finder"

set source_folder to folder "Temp" of desktop

repeat with this_folder in (get folders of source_folder)

set the_string to name of this_folder

set output_strings to {"", ""}

set the_offset to offset of "-" in the_string

set item 1 of output_strings to items 1 thru (the_offset - 2) of the_string as string

set item 2 of output_strings to items (the_offset + 2) thru -1 of the_string as string

set item 2 of output_strings to word -1 of item 2 of output_strings & " " & items 1 thru -8 of item 2 of output_strings as string

make new folder in source_folder with properties {name:item 1 of output_strings}

move this_folder to the result

set name of the result to item 2 of output_strings

end repeat

end tell


(245986)

Oct 20, 2023 6:11 PM in response to Niel

PS. It's running into an issue where the same artist has multiple albums, and it says it can't create the parent folder because the folder already exists. How do I modify the "make new folder" line to first check if the said folder exists, and if so to add to that folder, and if not, to create a new one?

Oct 20, 2023 6:15 PM in response to lx-xl

Use the following:


tell application "Finder"

set source_folder to folder "Temp" of desktop

repeat with this_folder in (get folders of source_folder)

set the_string to name of this_folder

set output_strings to {"", ""}

set the_offset to offset of "-" in the_string

set item 1 of output_strings to items 1 thru (the_offset - 2) of the_string as string

set item 2 of output_strings to items (the_offset + 2) thru -1 of the_string as string

set item 2 of output_strings to word -1 of item 2 of output_strings & " " & items 1 thru -8 of item 2 of output_strings as string

try

make new folder in source_folder with properties {name:item 1 of output_strings}

move this_folder to the result

on error

move this_folder to item (item 1 of output_strings) of source_folder

end try

set name of the result to item 2 of output_strings

end repeat

end tell


(245989)

AppleScript or Automator for creating parent folders and renaming folders?

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