I need to create a folder based on the first part of the file and then move that file and all other files with that same first part of the file name into that folder. In this case the folder would be named "1234-City-Artist-1080x1920". The folder will always be named the same as the "city" part of the file up until the space. There will be multiple files with the same city that need to go in the same file, so I need to build a sequence that tells it to just move the folder in to the folder if it already exists.
I found this solution for windows: Batch create folders based on part of file name and move files into that folder
I need the Mac equivalent to run AppleScript.
I've managed to figure out a script to make a folder for EVERY file in the folder I run ...
tell application "Finder"
set selected to selection
set current_folder to item 1 of selected
set mlist to every file of current_folder
repeat with this_file in mlist
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)
set new_folder to make new folder with properties {name:new_name} at current_folder
move this_file to new_folder
end repeat
end tell
... now I need to figure out how to make using only a section (city) of the file name (1234-city-artist-size), AND put all files with the same (city) name in the same folder. I'm getting very lost when it comes to pulling just a section of the file name, and not getting an error when a folder has already been created.
Any thoughts on how to edit the script to make this work?