Move files based on partial FOLDER name

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

Move files based on partial FOLDER name Sat Jun 08, 2013 3:07 pm • by judismith
Is there a way to move a file to a folder based on a partial match of the folder name?

For example, I name my file yyyy-mm-dd last, first yy-nnn Some other title

The folders structure is as follows:
Code: Select all
Matters
    Last1, First1
         12-001 Some description
         12-002 Some other description
    Last2, First2
         12-003 yet another description
         12-004 and another description

What I want to do is have Hazel match the yy-nnn token in the file name, let's say it contains 12-001 to the 12-001 portion of the subfolder name.

Let's say I have a file named "2013-06-08 Last1, First1 12-001 My New Note". This should be filed in Matters => Last1, First1 =>12-001 Some description

I have used the custom tokens to parse out the file name. However, what happens is that Hazel navigates to the correct Last1, First1 folder and then creates a new folder "12-001", ignoring the existing folder "12-001 Some description" so we end up with the following structure:

Code: Select all
Matters
    Last1, First1
         12-001
         12-001 Some description
         12-002 Some other description
    Last2, First2
         12-003 yet another description
         12-004 and another description

Any thoughts much appreciated.

Kind regards,
Judi Smith
judismith
 
Posts: 2
Joined: Sat Jun 08, 2013 2:45 pm

Re: Move files based on partial FOLDER name Sat Jun 08, 2013 5:19 pm • by judismith
For anyone who is interested, I solved this with a bit of AppleScript - posted below. I'd still be very interested to know if there is a way to do this natively with Hazel.

Basically this script just uses AppleScript to parse the name to the same tokens as I have set Hazel to create the tokens.

Code: Select all
tell application "Finder"
   set myHazelTokenDelimiters to " "
   set theListOfCustomTokens to name of theFile

   -- use the spaces in the file name to address the "tokens" as a list

   set AppleScript's text item delimiters to {myHazelTokenDelimiters}

   -- since we are using spaces to divide the file name to tokens and client name contains ", " we need to concatenate items 2 and 3 to get the client name token as in Hazel

   set clientName to text item 2 of theListOfCustomTokens & " " & text item 3 of theListOfCustomTokens
   set thePath to "/Users/[username]/Google Drive/Matters/" & clientName
   set theFolder to the name of every item in folder (thePath as POSIX file) whose name begins with text item 4 of theListOfCustomTokens
   -- activate
   -- display dialog theFolder
   return {hazelExportTokens:{folderName:theFolder}}
end tell
judismith
 
Posts: 2
Joined: Sat Jun 08, 2013 2:45 pm


Return to Support