How do I automatically create and populate folders based on document names

Discussion in 'Mac Basics and Help' started by auslegung, Feb 13, 2013.

  1. auslegung macrumors newbie

    Feb 13, 2013
    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!
  2. SpetsnaZ99 macrumors member

    Apr 27, 2012
    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?
  3. auslegung thread starter macrumors newbie

    Feb 13, 2013
    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!

  4. SpetsnaZ99, Feb 15, 2013
    Last edited by a moderator: Feb 17, 2013

    SpetsnaZ99 macrumors member

    Apr 27, 2012
    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.


    Here is the code:-
    tell application "Finder"
    	set the selected_items to the selection
    	--This checks that items have been selected
    	if the selected_items is {} then
    		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
    			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
    				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
  5. auslegung thread starter macrumors newbie

    Feb 13, 2013
    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.

Share This Page