Rename file based on content in the file

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

Moderator: Mr_Noodle

Rename file based on content in the file Thu Jun 02, 2016 8:20 pm • by Marcel
Hi,

I'm trying to setup a Hazel rule to rename and move a file based on contents within the file. Here are the details:

1. Search for the word "Invoice" in the contents of the file
2. Rename the file based on the sender of the file. For example, if the invoice is from Google or Microsoft it will rename and use the company name, i.e. Google, Microsoft, etc., in the file name
3. Move the file to a different folder

Thanks very much for your help!
Marcel
 
Posts: 1
Joined: Thu Jun 02, 2016 8:15 pm

Re: Rename file based on content in the file Fri Jun 03, 2016 10:24 am • by Mr_Noodle
Where do you expect to find the company name? From the contents or using the site it was downloaded from or ???
Mr_Noodle
Site Admin
 
Posts: 11250
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Rename file based on content in the file Sat Jun 04, 2016 2:12 pm • by MacOCD
What you want to achieve is all present in Hazel. It's more difficult to explain than it is to do but I'll have a try.

It's more likely you'll need to set up separate rules per company you wish to look for invoices from.

Obviously the company name has to be located from somewhere, it may be in the pdf file name, or in the document text, but it may well be in a 'non-ocr' friendly format or a logo and it's not likely to be in the same place for multiple different companies.

If you are making separate rules per company then you don't even need the name, just something that's unique to that company, part of their address perhaps, or maybe your account number with that company?

You can go further by extracting the Invoice Date, Invoice Number and Amount using "contains match" rules, so long as the document is presented in a way you can read the info. I do this for 30+ companies that invoice me.

IncomingInvoice.pdf becomes:-

2016-06-04 Company Name Invoice #123456 for $123.45.pdf

I use the date in YYYY-MM-DD format as the documents are always sorted in date order in finder.

You can create date variables in the format they appear in the document when matching, then change the date display order for your output file name.

Example:
Obviously the specifics will be different depending on how information is presented in the invoice.

Create a variable called "invdat" looking for the date pattern in the pdf invoice (e.g. 03-26-16) in an extra contains match rule:

If ALL of the below are true:
Document Contents contains "Invoice" <-- Looks for word "Invoice"
Document Contents contains "Insert Name or part of address here" <-- Search for unique to that company text
Document Contents contains Match "Invoice Date: <invdat>" <-- Look for words "Invoice Date:" then the date
Document Contents contains Match "Invoice No: <invnum>" <-- Look for words "Invoice No:" then the invoice number

Then when you come to rename the file
Rename Document to"
<invdat> Company Name Inv#<invnum>.pdf

You are able to change the date order in the action part of the rule.

Does this make any sense?
MacOCD
 
Posts: 44
Joined: Fri Sep 26, 2014 11:02 am

Re: Rename file based on content in the file Fri Oct 28, 2016 1:56 pm • by rdouma
Thank you for this, it really helped me along! It do think the below is useful to explain:

MacOCD wrote:Create a variable called "invdat" looking for the date pattern in the pdf invoice (e.g. 03-26-16) in an extra contains match rule:


This part was not immediately clear to me. I didn't know how to create a variable in Hazel, so I thought I'd make a GIF to show that shows where that feature can be found (below the "Custom text" element). I blurred out some sensitive info but I hope this helps the next one.

Image
rdouma
 
Posts: 7
Joined: Fri Jun 05, 2009 6:58 am


Return to Support