0

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?

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • AppleScript is a relatively poor choice for this sort of task. Learn a scripting language with great string manipulation support, like Ruby. You'll be glad you did. – matt Jul 27 '22 at 11:29
  • Try `set cs to "1234-city-artist-size"`; `set AppleScript's text item delimiters to "-"`; `set cn to second text item of cs`;. Then filtre on 'cn' using `whose name contains cn` (look up `whose` in applescript language guide for guidance). As well, there are 'related' questions here that address your question. – Mockman Aug 01 '22 at 20:01
  • I agree. I definitely need to learn a scripting language. I am a nooby with dreams of figuring out this mess. – Stephanie Stockton Aug 11 '22 at 16:42

0 Answers0