Creating multiple folders with the first 3 words of a file name

Hello there, I’m a newbie here and normally search tirelessly on the web to find things. I noticed here that many will take the time to write a basic script to help others out and that was awesome to see! Well as you can guess, I’m asking for help…
I have ultimately thousands of files (in the end) that I have to take the first three words of the pdf file name and make a folder and put that file or files in the folder. Some times there are 2-6 files that will have the same first 3 words and will go into the same folder…
example: (made up names)
2026 John Doe intake.pdf - Folder would be 2026 John Doe
2026 John Doe application.pdf - File would go into above folder
2026 Mary Smith intake.pdf - Folder would be 2026 Mary Smith
2026 Jack Smith application.pdf. - Folder would be 2026 Jack Smith
The pdf files will all be in the same folder to begin with, but again, there may be several that will end up with the same first 3 words and should go into the same folder. I have found script on a site that names the folder after the file name, I just need that tweaked to only use the first 3 word essentially. or to gather the data before the third space…? Oh, just another bit of info, some names may have a hyphen in them ie 2026 Brenda Smith-Doe intake.pdf and if it make a difference, the file names may be a lot longer than my example like 2026 John Doe application for bluecross mapd direct abhhnthdksj.pdf if that makes any difference.

I need to automate this process so badly, I will make sure you guys are recognized also. My head can’t take more searching for the fix and I wish I knew how to write scripting…! ;/
Here is the script I mentioned that names the folder after the full file name.

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

If anyone can help me out… Would help me and this small company immensely!!!

And thank you for at least taking the time to read my first post!!
Brian

Try this…

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

local tid, current_folder, file_name, file_names, this_file, new_name, i, c
set current_folder to choose folder
set current_folder to current_folder as text
set tid to text item delimiters
set text item delimiters to " "
tell application "System Events"
	set file_names to name of files of folder current_folder
	repeat with i from 1 to count file_names
		set file_name to item i of file_names
		set this_file to file (current_folder & item i of file_names)
		get properties of this_file
		set cur_ext to name extension of this_file
		if cur_ext ≠ "" then
			set new_name to text 1 thru -((length of cur_ext) + 2) of file_name
			set new_name to text items of new_name
			if (item 4 of new_name) is in {"intake", "application"} then
				set new_name to (items 1 thru 3 of new_name) as text
				if not (exists folder (current_folder & new_name)) then
					make new folder at folder current_folder with properties {name:new_name}
				end if
				move this_file to (current_folder & new_name)
			end if
		end if
	end repeat
end tell
set text item delimiters to tid

Are there any sub-filenames that contain a word other than “intake” or “application”?

WOW! Thank you so much Robert!!! I’ll have to test it tomorrow, I just wanted to see if anyone responded yet… I’m so excited to try it out and I’ll let you know right away…
yeah there can be pretty much anything after the 2026 Firstname Lastname unfortunatly… Alot of the agents aren’t very versed in naming schemes at all… It’s tough enough to get the clients first and last on the file name! Here is a small example of a renamed corrected file name.
2026 Jane Doe E-App Blue Cross MAPD - John Agent 2025-12-06.pdf
But a lot can change after the clients first and last name depending on plans and agent names etc.
Oh MY! I can’t wait to try this!!!
Thank you so so much for your time and help!!! You have a wonderful night.

I would need to know what the 4 word could be beforehand.
In addition to intake & application.
Or do you want it to be any word?

Another solution:
Unix command line using macOS perl in directory where files exists - variant when only 3 first words in filename will be used to create directories:

ls -1 | perl -ne 'chomp; @v = split /\s/, $_, 4; $d = join " ",@v[0..2]; mkdir "$d"; system("mv \"$_\" \"$d\"\n")'

I ran it, chose the folder and clicked “choose” and this is the output->
{“”}
Nothing was processed.
To answer your last question: I need the folder to be named by the first 3 'words" (year Firstname Lastname) of the pdf file name and ignore the rest of the file name…
so if a file was named “2026 Jim Bob application Blue cross MAPD - John Doe 2025-12-06.pdf”
The folder should be named “2026 Jim Bob”
also, there may be several files for Jim Bob with the same first 3 ‘words’ ie
“2026 Jim Bob Intake Medicare John Doe.pdf”
I also need that file to go into that “2026 Jim Bob” folder and so on…
I hope that makes more sense, I’m sorry if I’m not describing it thoroughly enough…

Who are you replying to?

sorry, to you Robertfern

Can you post some actual file names that are in the folder?
Because mine worked with the sample names you gave.

to Robertfern
No, confidential, but they look exactly like the ones that I posted, Just different names.

To Robertfern
Give me a minute, I’m going to take out some that may be questionable and try again.

I’ve just checked my solution on your example file - it works - file was moved to subdirectory
2026 Jim Bob

I understand that you don’t want t play with terminal operations but in that case it can be packed in shortcut where you select subdirectory and script will be called by “Run script shortcut”
If you want such solution I can spent a few minutes.

Here is a slightly different version that doesn’t care what the fourth word is…

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

local tid, current_folder, file_name, file_names, this_file, new_name, i, c
set current_folder to choose folder
set current_folder to current_folder as text
set tid to text item delimiters
set text item delimiters to " "
tell application "System Events"
	set file_names to name of files of folder current_folder
	repeat with i from 1 to count file_names
		set file_name to item i of file_names
		set this_file to file (current_folder & item i of file_names)
		set cur_ext to name extension of this_file
		if cur_ext ≠ "" then
			set new_name to text 1 thru -((length of cur_ext) + 2) of file_name
			set new_name to text items of new_name
			if (count new_name) > 3 then
				set new_name to (items 1 thru 3 of new_name) as text
				if not (exists folder (current_folder & new_name)) then
					make new folder at folder current_folder with properties {name:new_name}
				end if
				move this_file to (current_folder & new_name)
			end if
		end if
	end repeat
end tell
set text item delimiters to tid

** EDIT** - I cleaned out a line that was used for debugging

I’ll give anything a try… I saw that some of the file names had periods, dashes, and () in the name and thought they may be causing the issue… But I removed those files and it still has the same output and nothing happens… IDK I don’t see anything different from the sample files I send over…

THAT DID IT!!! Thank you so Much Robertfern… you are awesome!!

What OS version are you on?

** EDIT ** NEVERMIND

robertfern, I can’t wait to get this working… You are saving a ton of needless time for me a few others… Thank you so much for your time!!
You have a VERY Merry Christmas!!

How many files are there in the directory? and how long did it take?

The sample folder only had 11 files and it maybe took a second or two… I will report back if I’m able to do a larger batch.

To robertfern
I tried 50 files for the fun of it and that will probably be the amount we do, just several times… but it took 7 seconds to finish and it did a perfect job inserting several files into a folder when needed.
Thanks again!