How to sort files with similar filenames into discrete folders? (AppleScript)
I have a list of files that looks something like this:
- OBJECTID_45878.dbf
- OBJECTID_45878.prj
- OBJECTID_45879.dbf
- OBJECTID_45879.prj
- OBJECTID_45880.dbf
- OBJECTID_45880.prj
I want to run a script that will sort all of these files into subfolders based on their filenames. So the result would look like this:
OBJECTID_45878 (Folder)
- OBJECTID_45878.dbf
- OBJECTID_45878.prj
OBJECTID_45879 (Folder)
- OBJECTID_45879.dbf
- OBJECTID_45879.prj
OBJECTID_45880 (Folder)
- OBJECTID_45880.dbf
- OBJECTID_45880.prj
Here's the code I have now (see below). It creates a new folder for each file, and doesn't sort them like I would like. How should I modify my AppleScript so this works? Thanks so much!
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
MacBook Pro 15”, macOS 10.14