Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

auslegung

macrumors newbie
Original poster
Feb 13, 2013
5
0
I have hundreds of Word documents that need to be placed into their own folders. These documents are usually named "FirstName LastName-ParentFirstName ParentLastName-etc", or "FirstName LastName-ParentFirstName ParentLastName etc", or mostly "FirstName LastName-etc" and "FirstName LastName etc", the differences being the hyphen or whitespace. I can manually separate them into different folders so that each folder contains the same naming scheme, to make the automation easier.

But how do I create an automator workflow (or something else entirely) to automatically create folders that are named "LastName FirstName-ParentLastName ParentFirstName", then populate the folder with the appropriate document? Is this even possible? If it isn't, are there shortcuts to doing it manually? Thanks!
 

SpetsnaZ99

macrumors member
Apr 27, 2012
69
1
UK
A simple script could do this.
Can you post an example filename for me and ill see what i can do.
i.e. "John Doe-Jane Doe.doc"

Also are you wanting the hyphen stripped out of the folder name?
 

auslegung

macrumors newbie
Original poster
Feb 13, 2013
5
0
Here are the most common file names:

1. John Smith-Jane Smith-STF.doc
2. John Smith-Jane Smith-STF BHHB.doc
3. Dick Smith-Sue Smith-STF-FGC.doc
4. Beth Smith-STF-FGC.doc
5. Billy Smith-STF.doc
6. Smith STF.doc
7. Bob Smith STF-Florence.doc

I want it to be LastName FirstName-LastName FirstName-STF.doc, for example Smith John-Smith Jane-STF.doc. Anything else I'd like to get rid of, including BHHB, FGC, and Florence, but that's not very important. In fact, changing the name of the document isn't even that important, just the name of the file, which must be LastName FirstName-LastName FirstName. If you need me to identify the most likely naming schemes, they are 1 and 2, above. I can manually move all files that follow 1, for instance, and run the script on those so they'll come out as planned, if that's what you need me to do. I could then edit your script to work with the variants. Thanks!

A simple script could do this.
Can you post an example filename for me and ill see what i can do.
i.e. "John Doe-Jane Doe.doc"

Also are you wanting the hyphen stripped out of the folder name?
 

SpetsnaZ99

macrumors member
Apr 27, 2012
69
1
UK
Ive had a go at writing a script but seem to be banging my head on a brick wall. Based on the file name structure of "Dick Smith-Sue Smith-STF-FGC.doc" it works fine, however on the file names "Smith STF.doc" etc it failed (see attached img). Also there are two filenames that clash,"1. John Smith-Jane Smith-STF.doc" and "2. John Smith-Jane Smith-STF BHHB.doc" the 'BHHB' being the difference, this would mean that the two files would be in the same folder. Perhaps someone could offer a suggestion.

Screenshot2013-02-15at145100_zps8ae249dc.png


Here is the code:-
Code:
tell application "Finder"
	activate
	
	set the selected_items to the selection
	--This checks that items have been selected
	if the selected_items is {} then
		beep
		display dialog "No items selected" buttons {"Cancel"} default button 1
	end if
	--Loop thru all of the selected items
	repeat with this_item in the selected_items
		--Retrieve the filename
		set iName to name of this_item
		
		--Set the string 'theFilename' to the files actual name without the extension
		set theFileName to text 1 thru ((offset of "." in iName) - 1) of iName
		--Seperate the first name from the 'theFilename'
		set fName to text 1 thru ((offset of "-" in theFileName) - 1) of theFileName
		--Seperate the last name from the 'theFilename'
		set LName to text ((offset of "-" in theFileName) + 1) thru -1 of theFileName
		set this_folder to container of this_item
		--An 'IF" statement to check if the 'theFilename' contains the string '-STF'
		--If it does then it will be added to the folder name if not then it will be dismissed
		try
			if iName contains "STF" then
				set theFolder to (make new folder at this_folder with properties {name:word 2 of fName & " " & word 1 of fName & "-" & word 2 of LName & " " & word 1 of LName & "-STF"})
				move this_item to theFolder
			else
				set theFolder to (make new folder at this_folder with properties {name:word 2 of fName & " " & word 1 of fName & "-" & word 2 of LName & " " & word 1 of LName})
				move this_item to theFolder
			end if
		end try
	end repeat
end tell
 
Last edited by a moderator:

auslegung

macrumors newbie
Original poster
Feb 13, 2013
5
0
Thank you! I don't have time right now to implement it, but I should have been more clear in what I said about the file names. There will never be a clash, if there is it's because I provided an unhelpful fake name.

I provided several file names that aren't going to be able to be changed automatically since they don't have the second full name. Those will have to be done manually, but thankfully they are the minority.

What I mostly will need is "John Smith-Jane Smith-STF.doc" to become a folder name "Smith John-Smith Jane" Is that possible to remove the -STF? Perhaps put a line in the script to remove everything after the second hyphen, or just to remove -STF. And if the file is "John Smith-Jane Smith STF.doc" I'll need to remove " STF."

Thanks! What you've already written will work for most of what I need, and I'm sure I can edit it to fit my needs that I outlined above, but if it doesn't take much of anyone's time (assuming the code provided doesn't already fit what I've outlined above), and they feel like it, edit the code as you feel.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.