Delete non-corresponding files

Talk, speculate, discuss, pontificate. As long as it pertains to Hazel.

Moderators: Mr_Noodle, Moderators

Delete non-corresponding files Tue Oct 23, 2018 8:22 am • by pluralog
Any Ideas how to solve the following problem?

I have two folders with corresponding files. The one folder has .png the other one .pdf with the same name. Now I want to compare both folders and delete the file if there is no corresponding file.

Example:
Code: Select all
Folder PNG
a.png
b.png
d.png

Code: Select all
Folder PDF
a.pdf
b.pdf
c.pdf
d.pdf

Now I want c.pdf deleted, since there is no corresponding file.

Would appreciate any help.

PS: for those who are interested. I convert all my screenshots with some amazing hazel rules into searchable PDFs in a subfolder. But some screenshots are nonsense. If I delete them, I want the corresponding PDF deleted.
pluralog
 
Posts: 5
Joined: Tue Oct 23, 2018 8:15 am

Re: Delete non-corresponding files Tue Oct 23, 2018 11:08 am • by Mr_Noodle
I think you may need a script for this. There is almost a way to do this if both folders are under a common folder, but currently it would require an extra level of condition nesting that Hazel doesn't allow for.
Mr_Noodle
Site Admin
 
Posts: 7504
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Delete non-corresponding files Tue Oct 23, 2018 11:29 am • by Mr_Noodle
Actually, working it out, I think it can be done, provided the PDF and PNG folders are under the same folder. Assuming you are monitoring the PDF folder, it would be something like:
Code: Select all
   If (all) are met
      Name matches (•pdf file name)
      If (all) are met for (the enclosing folder)
          If (all) are met for (any file/folder in same folder)
              Kind is Folder
              Name is <<<name of PNG folder>>>
              If (all) are met for (all of its sub-files)
                  Name does not (•pdf file name)

where (•pdf file name) is a custom attribute set to match anything.

This will match the name of the pdf file, then check the enclosing folder and try and match any sibling folder with the PNG folder name. Then it checks for the lack of a file in that folder which has the same name as the PDF file.

You'll need to tweak this some and I'd try it on some test files until it gets the expected results.
Mr_Noodle
Site Admin
 
Posts: 7504
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Delete non-corresponding files Thu Nov 22, 2018 12:39 pm • by pluralog
Sounds good, but I'm completely lost. But I learned a lot about hazel in the process. It's unfair to hide the nested conditions behind the option key :)

I tried it on the follwing folder:
Image

With the settings as understood (trying to open the file to be deleted first):
Image

While the file name is:
Image

But in the debug file I'm lost. Is it possible that hazel is searching for x.pdf in the PNG Folder?

Thamnks for your help!


Code: Select all
2018-11-22 17:54:02.894 hazelworker[41447] DEBUG: Processing directories: (
    "/Users/jona/Desktop/Test/PDF"
)
2018-11-22 17:54:02.896 HazelHelper[83895] DEBUG: Checking events for path /Users/jona/Desktop/Test/PDF, folder PDF
2018-11-22 17:54:02.897 hazelworker[41447] DEBUG: Received file event: {
    date = "2018-11-22 16:54:00 +0000";
    path = "<ComNoodlesoft_NoodlePathSet: 0x7f89150081e0>\n  /Users/jona/Desktop/Test/PDF : 18446744073709551615\n";
}
2018-11-22 17:54:02.897 hazelworker[41447] DEBUG: About to process directory /Users/jona/Desktop/Test/PDF
2018-11-22 17:54:02.903 hazelworker[41447][PREDICTION] DEBUG: Calculating fire time - predicate: displayBasename hazelMatchesPattern: ({hazelcustomtoken:pdf file name:({anything:sc0:id0}):sc0:id0:cap}),{
} negation: 0 result: 1
2018-11-22 17:54:02.903 hazelworker[41447][PREDICTION] DEBUG: Next fire time: 4001-01-01 01:00:00.000
2018-11-22 17:54:02.935 hazelworker[41447][PREDICTION] DEBUG: Calculating fire time - predicate: typeObject isType: "public.folder" negation: 0 result: 1
2018-11-22 17:54:02.935 hazelworker[41447][PREDICTION] DEBUG: Next fire time: 4001-01-01 01:00:00.000
2018-11-22 17:54:02.935 hazelworker[41447][PREDICTION] DEBUG: Calculating fire time - predicate: displayBasename ==[cd] "PNG" negation: 0 result: 1
2018-11-22 17:54:02.935 hazelworker[41447][PREDICTION] DEBUG: Next fire time: 4001-01-01 01:00:00.000
2018-11-22 17:54:02.935 hazelworker[41447][PREDICTION] DEBUG: Calculating fire time - predicate: displayBasename hazelMatchesPattern: ({hazelcustomtoken:pdf file name:({anything:sc0:id0}):sc0:id0:cap}),{
} negation: 0 result: 0
2018-11-22 17:54:02.936 hazelworker[41447][PREDICTION] DEBUG: Next fire time: 4001-01-01 01:00:00.000
2018-11-22 17:54:02.936 hazelworker[41447][PREDICTION] DEBUG: Bail out: AND predicate
2018-11-22 17:54:02.936 hazelworker[41447][PREDICTION] DEBUG: Bail out: ALL SUBFILES predicate target
2018-11-22 17:54:02.936 hazelworker[41447][PREDICTION] DEBUG: Bail out: AND predicate
2018-11-22 17:54:02.936 hazelworker[41447][PREDICTION] DEBUG: Bail out: AND predicate
2018-11-22 17:54:02.936 hazelworker[41447][PREDICTION] DEBUG: Bail out: AND predicate
2018-11-22 17:54:02.936 hazelworker[41447][PREDICTION] DEBUG: Predicted fire time for file: /Users/jona/Desktop/Test/PDF/1.pdf and rule Delete: 4001-01-01 01:00:00.000 Should poll: 0
2018-11-22 17:54:02.936 hazelworker[41447] DEBUG: 1.pdf: Did not match any rules.
2018-11-22 17:54:02.936 hazelworker[41447][PREDICTION] DEBUG: Calculating fire time - predicate: displayBasename hazelMatchesPattern: ({hazelcustomtoken:pdf file name:({anything:sc0:id0}):sc0:id0:cap}),{
} negation: 0 result: 1
2018-11-22 17:54:02.936 hazelworker[41447][PREDICTION] DEBUG: Next fire time: 4001-01-01 01:00:00.000
2018-11-22 17:54:02.938 hazelworker[41447][PREDICTION] DEBUG: Calculating fire time - predicate: typeObject isType: "public.folder" negation: 0 result: 1
2018-11-22 17:54:02.938 hazelworker[41447][PREDICTION] DEBUG: Next fire time: 4001-01-01 01:00:00.000
2018-11-22 17:54:02.938 hazelworker[41447][PREDICTION] DEBUG: Calculating fire time - predicate: displayBasename ==[cd] "PNG" negation: 0 result: 1
2018-11-22 17:54:02.938 hazelworker[41447][PREDICTION] DEBUG: Next fire time: 4001-01-01 01:00:00.000
2018-11-22 17:54:02.938 hazelworker[41447][PREDICTION] DEBUG: Calculating fire time - predicate: displayBasename hazelMatchesPattern: ({hazelcustomtoken:pdf file name:({anything:sc0:id0}):sc0:id0:cap}),{
} negation: 0 result: 1
2018-11-22 17:54:02.938 hazelworker[41447][PREDICTION] DEBUG: Next fire time: 4001-01-01 01:00:00.000
2018-11-22 17:54:02.938 hazelworker[41447][PREDICTION] DEBUG: Calculating fire time - predicate: displayBasename hazelMatchesPattern: ({hazelcustomtoken:pdf file name:({anything:sc0:id0}):sc0:id0:cap}),{
} negation: 0 result: 0
2018-11-22 17:54:02.938 hazelworker[41447][PREDICTION] DEBUG: Next fire time: 4001-01-01 01:00:00.000
2018-11-22 17:54:02.939 hazelworker[41447][PREDICTION] DEBUG: Bail out: AND predicate
2018-11-22 17:54:02.939 hazelworker[41447][PREDICTION] DEBUG: Bail out: ALL SUBFILES predicate target
2018-11-22 17:54:02.939 hazelworker[41447][PREDICTION] DEBUG: Bail out: AND predicate
2018-11-22 17:54:02.939 hazelworker[41447][PREDICTION] DEBUG: Bail out: AND predicate
2018-11-22 17:54:02.939 hazelworker[41447][PREDICTION] DEBUG: Bail out: AND predicate
2018-11-22 17:54:02.939 hazelworker[41447][PREDICTION] DEBUG: Predicted fire time for file: /Users/jona/Desktop/Test/PDF/3.pdf and rule Delete: 4001-01-01 01:00:00.000 Should poll: 0
2018-11-22 17:54:02.939 hazelworker[41447] DEBUG: 3.pdf: Did not match any rules.
2018-11-22 17:54:02.939 hazelworker[41447][PREDICTION] DEBUG: Calculating fire time - predicate: displayBasename hazelMatchesPattern: ({hazelcustomtoken:pdf file name:({anything:sc0:id0}):sc0:id0:cap}),{
} negation: 0 result: 1
2018-11-22 17:54:02.939 hazelworker[41447][PREDICTION] DEBUG: Next fire time: 4001-01-01 01:00:00.000
2018-11-22 17:54:02.941 hazelworker[41447][PREDICTION] DEBUG: Calculating fire time - predicate: typeObject isType: "public.folder" negation: 0 result: 1
2018-11-22 17:54:02.941 hazelworker[41447][PREDICTION] DEBUG: Next fire time: 4001-01-01 01:00:00.000
2018-11-22 17:54:02.941 hazelworker[41447][PREDICTION] DEBUG: Calculating fire time - predicate: displayBasename ==[cd] "PNG" negation: 0 result: 1
2018-11-22 17:54:02.941 hazelworker[41447][PREDICTION] DEBUG: Next fire time: 4001-01-01 01:00:00.000
2018-11-22 17:54:02.941 hazelworker[41447][PREDICTION] DEBUG: Calculating fire time - predicate: displayBasename hazelMatchesPattern: ({hazelcustomtoken:pdf file name:({anything:sc0:id0}):sc0:id0:cap}),{
} negation: 0 result: 0
2018-11-22 17:54:02.941 hazelworker[41447][PREDICTION] DEBUG: Next fire time: 4001-01-01 01:00:00.000
2018-11-22 17:54:02.941 hazelworker[41447][PREDICTION] DEBUG: Bail out: AND predicate
2018-11-22 17:54:02.941 hazelworker[41447][PREDICTION] DEBUG: Bail out: ALL SUBFILES predicate target
2018-11-22 17:54:02.941 hazelworker[41447][PREDICTION] DEBUG: Bail out: AND predicate
2018-11-22 17:54:02.941 hazelworker[41447][PREDICTION] DEBUG: Bail out: AND predicate
2018-11-22 17:54:02.941 hazelworker[41447][PREDICTION] DEBUG: Bail out: AND predicate
2018-11-22 17:54:02.941 hazelworker[41447][PREDICTION] DEBUG: Predicted fire time for file: /Users/jona/Desktop/Test/PDF/2.pdf and rule Delete: 4001-01-01 01:00:00.000 Should poll: 0
2018-11-22 17:54:02.941 hazelworker[41447] DEBUG: 2.pdf: Did not match any rules.
2018-11-22 17:54:02.942 hazelworker[41447] DEBUG: Directory /Users/jona/Desktop/Test/PDF processed in 0.044507 seconds
2018-11-22 17:54:02.942 HazelHelper[83895] DEBUG: Checking events for path /Users/jona/Desktop/Test/PDF, folder PDF
2018-11-22 17:54:02.942 hazelworker[41447] DEBUG: Sending metrics to scheduler. Next scheduled run: 4001-01-01 01:00:00.000
2018-11-22 17:54:02.943 HazelHelper[83895] DEBUG: Received metrics for folder /Users/jona/Desktop/Test/PDF: {
    directoryDepth = 18446744073709551615;
    requestedSchedulingTime = "4001-01-01 00:00:00 +0000";
    triggerPaths = "<ComNoodlesoft_NoodlePathSet: 0x60000275d5a0>\n";
    unavailablePaths = "{(\n)}";
}
2018-11-22 17:54:02.943 HazelHelper[83895] DEBUG: Timer scheduled for folder /Users/jona/Desktop/Test/PDF at 4001-01-01 00:00:00 +0000
2018-11-22 17:54:02.944 hazelworker[41447] Done processing folder PDF

pluralog
 
Posts: 5
Joined: Tue Oct 23, 2018 8:15 am

Re: Delete non-corresponding files Fri Nov 23, 2018 1:16 pm • by Mr_Noodle
It is a bit hard to discover though it is the standard behavior (it also works with Smart Folders in Finder and Smart Playlists in iTunes as well). Also, the "target" part of nested conditions tends to get misused so I find it best to keep it out of site to prevent confusion.
Mr_Noodle
Site Admin
 
Posts: 7504
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Delete non-corresponding files Fri Nov 30, 2018 1:01 pm • by pluralog
Mr_Noodle wrote:It is a bit hard to discover though it is the standard behavior (it also works with Smart Folders in Finder and Smart Playlists in iTunes as well). Also, the "target" part of nested conditions tends to get misused so I find it best to keep it out of site to prevent confusion.


I did not want to critizise that. Only wanted to explain why I did not understand your first reply in the beginning.

Can you read any problem from the Log?
pluralog
 
Posts: 5
Joined: Tue Oct 23, 2018 8:15 am

Re: Delete non-corresponding files Mon Dec 03, 2018 12:08 pm • by Mr_Noodle
Have you tried previewing your rule? Can you send a screenshot of that?
Mr_Noodle
Site Admin
 
Posts: 7504
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Delete non-corresponding files Mon Dec 03, 2018 5:56 pm • by pluralog
Tried it. Didn't work. Tried it again. Didn't work. Tried again to understand the logic of the enclosed folder conditions and I found the mistake.

The last condition couldn't be
Code: Select all
If (all) are met for (all of its sub-files)
                  Name does not (•pdf file name)

but
Code: Select all
If (all) are met for (any of its sub-files or folders)
                  Name does not (•pdf file name)


Thank you so much for your help. Any forum, store or plattform I can help with a positive review?
pluralog
 
Posts: 5
Joined: Tue Oct 23, 2018 8:15 am

Re: Delete non-corresponding files Tue Dec 04, 2018 11:36 am • by Mr_Noodle
Hmm, I believe that "all" should be correct there. Using "any", I feel that it would match more cases than you'd want.
Mr_Noodle
Site Admin
 
Posts: 7504
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Delete non-corresponding files Tue Dec 04, 2018 1:52 pm • by pluralog
In all my tests it finds the cases where there is a match in both folders.
pluralog
 
Posts: 5
Joined: Tue Oct 23, 2018 8:15 am


Return to Open Discussion

cron