Defining a pattern from one file to apply to another?

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

Moderator: Mr_Noodle

I have a situation where updated versions of files are frequently added to a folder (by another Hazel rule) so that I accumulate instances of *arbitraryfilename*, *arbitraryfilename-1*, *arbitraryfilename-2* etc. and I was looking to create a rule to delete the older versions.

Although it's easy enough to define a matching rule based on xxx-1 etc as far as I can see you can't then ask it to delete the other file ("xxx"), i.e. apply a rule to a file other than the one that matched the pattern?

I may be missing a simpler way to accomplish this?

Thanks

Rob
RobForsyth
 
Posts: 7
Joined: Sun Dec 08, 2019 7:56 am

Try using something like "<<some date>> is not among the 1 most recent". That will match everything but the most recent file. You may need to play around with it (I suggest doing it on some test files).
Mr_Noodle
Site Admin
 
Posts: 11193
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Mr_Noodle wrote:Try using something like "<<some date>> is not among the 1 most recent". That will match everything but the most recent file. You may need to play around with it (I suggest doing it on some test files).



Thanks -- the slight complication I failed to make clear is that there may be multiple copies of multiple files in the folder, so I have "File A", "File A-1", "File B", "File B-1" (and potentially "File B-2" too, unless the rule is run frequently enough to cull older versions as newer ones are added), which was why I was thinking of an approach matching on the "-1" filename suffixes, deleting the old versions and then deleting the "-1" filename suffix if that could be achieved. If not then ensuring the folder only has versions of File A in it would indeed allow the "date not amongst the 1 most recent" approach, which I hadn't realised was possible, so thanks!
RobForsyth
 
Posts: 7
Joined: Sun Dec 08, 2019 7:56 am

This is a bit funky, but maybe you can try something like:
Code: Select all
    If (all) are met
        <some date> is not among the 1 most recent
        Name matches (• file name)-(number)
        If (all) are met (for any file in the same folder)
            Name matches (• file name)

where (• file name) is a custom text attribute which matches "anything". You'll have to test this but this takes advantage of the fact that any condition under the same "all" parent condition as the "is (not) among" condition will affect which files are considered in the group of files to consider for the "is (not) among".

That probably sound confusing, but what I think should happen here is that the first Name condition should match the file and store its name in the (• file name) custom attribute. In the nested condition, the second Name condition will see if there's another file in the same folder with the same base name. What that should do, then is limit the pool of files to those that match that base name for the "is among the" condition.

You'll need to tweak the patterns but that's the basic gist. I suggest playing around with some test files to see how it works out.
Mr_Noodle
Site Admin
 
Posts: 11193
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Thanks again for your help here but I'm falling at the first hurdle here currently in that I can't get the
Code: Select all
Name matches (• file name)-(number)

rule to behave as expected. Again the files are added to this folder by another Hazel rule so the "-1" suffix is whatever Hazel would cause to be appended (I've tried em- and en-dashes without success). Another thought was whether the (•file name) (=anything) token was being matched "greedily" such that the "-1" suffix was being incorporated into it? If that's possible can it be prevented?

Thanks again!
RobForsyth
 
Posts: 7
Joined: Sun Dec 08, 2019 7:56 am

Using "anything" after won't work here as you have "anything" in the custom attribute. You should try and be as specific as possible. You can try using two conditions, one with the number and one without. You can use a nested condition to do an "any" under the main "all" umbrella.
Mr_Noodle
Site Admin
 
Posts: 11193
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

For reference should anyone else find themselves with this specific query in the future there was a very simple "Doh!" realisation for me. Since, as I had mentioned, the files are added to this folder in the first place by a Hazel rule there is (in the options for the "sort into subfolder" action) an option to "replace the existing file if it exists", which solves the problem at source!
RobForsyth
 
Posts: 7
Joined: Sun Dec 08, 2019 7:56 am


Return to Support