Conditional Renaming and Sorting

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

Moderator: Mr_Noodle

Conditional Renaming and Sorting Fri Nov 13, 2015 6:26 am • by lnikj
I have files for software receipts exported as PDF from emails with the format: string-product

where strings / types are:

rsg - receipt software general (product purchase receipt)
lsg - licence software general (license details)
psg - payment software general (payment details, usually paypal)

I have equivalent triads for audio software, photographic software etc, all with three letter prefixes.

I can obviously create single rules in Hazel for every use case but I am wondering if I can handle all these with a single rule (that I can easily recreate on other computers).

What I want to do is strip the prefix, append the date from the file, move the file to an appropriate directory, and append a verbose description of the type of file.

For example,

rsg-hazel.pdf becomes ~/receipts/software/general/2015-11-12-hazel-receipt.pdf
lsg-hazel.pdf becomes ~/receipts/software/general/2015-11-12-hazel-licence.pdf
psg-hazel.pdf becomes ~/receipts/software/general/2015-11-12-hazel-payment.pdf

but

rsa-protools.pdf becomes ~/receipts/software/audio/2015-11-12-protools-receipt.pdf

As stated I have no trouble setting up a single rule for each case, but what I cannot figure out how to do is a general conditional rule that needs to:

(a) check the 3 letter prefix, and move the file to the appropriate folder
(b) strip the prefix off the front of the filename, and automatically change it to the corresponding suffix

Is there a way to do this in Hazel?

Or is it a case for a shell script?

Cheers

Nik
lnikj
 
Posts: 2
Joined: Fri Nov 13, 2015 6:01 am

Re: Conditional Renaming and Sorting Fri Nov 13, 2015 12:11 pm • by Mr_Noodle
You can use a combination of pattern matching and text replacement. Create a custom token to match the "code" part and then in the rename pattern, use that custom token, and apply a text replacement of the code with the longer text. Search the help for pattern matching if you aren't familiar with the feature.
Mr_Noodle
Site Admin
 
Posts: 11868
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Conditional Renaming and Sorting Sat Nov 14, 2015 1:22 am • by lnikj
Mr_Noodle wrote:You can use a combination of pattern matching and text replacement. Create a custom token to match the "code" part and then in the rename pattern, use that custom token, and apply a text replacement of the code with the longer text. Search the help for pattern matching if you aren't familiar with the feature.


Oh dear .... I spent hours yesterday writing a shell script (I'm a bit rusty in that area!) to achieve this (which does work!) when I failed to notice that the text replacement popup contains the ability to specify multiple key-value pairs. :oops:

For my entire life (I'm 50) I have always managed to find complex solutions to problems only to realise later that a far simpler method would have sufficed :roll: :(

Many thanks.
lnikj
 
Posts: 2
Joined: Fri Nov 13, 2015 6:01 am


Return to Support