Page 1 of 1

Comparing Folders by Number of Files

PostPosted: Thu Dec 14, 2017 12:05 am
by laurie_lewis
Hi all,

I have managed to do a major stuff up of my music collection. I can't use rsync or any other folder comparison software as the files have metadata and have been renamed in what I have stuffed up and no metadata in what I have backed up. Therefore effectively different files, but the folder structure is almost identical.

What I want to do is compare the two folder structures, by path and finally by the number of files in each folder. If the number matches the folder gets tagged green. If not the folder get tagged red. That will help me identify where I have to do some work. Now there are many thousands of folders in the following format.

Volume1/Music/Artist/Album/Songs - Backup
Volume 2/Music/Artist/Album/Songs - My Stuff up

So can I do this with Hazel or will I need a script of some kind? Any assistance appreciated.

All the best

Laurie

Re: Comparing Folders by Number of Files

PostPosted: Thu Dec 14, 2017 4:56 am
by laurie_lewis
Progressing a bit.

First part was the following.

Kind is Folder
Folder Depth is 1 (or as required)
Embedded Script True

Code: Select all
DIR_NAME="$1"
PATH1='Volumes/Data/Laurie/Music/My Music'
PATH2='Volumes/Data/Laurie/Music/Test'
DIR_NAME=${DIR_NAME/$PATH1/$PATH2}

if [ -d "$DIR_NAME" ]; then exit 0; else exit 1; fi


This script uses the path of the folder being monitored with Hazel and replaces it with the path of the folder you want to compare it with.

Then it just changes the colour tag as an action.

I created a second rule with the last line of the script changed to

Code: Select all
if [ ! -d "$DIR_NAME" ]; then exit 0; else exit 1; fi


This then returns folder not found and I change it to a different colour.

Next rule I am working on is triggered by the colour change in the first rule.

Rule

Kind Folder
Folder Depth is 1
Tag - Orange
Embedded Script - True

Having some difficulty here with this script


Code: Select all
DIR_NAME="$1"

COUNT1=find $DIR_NAME -type f | wc -l

PATH1='Volumes/Data/Laurie/Music/My Music'
PATH2='Volumes/Data/Laurie/Music/Test'
DIR_NAME=${DIR_NAME/$PATH1/$PATH2}

COUNT2=find $DIR_NAME -type f | wc -l

if [ $COUNT1==$COUNT2 ]; then exit 0; else exit 1; fi


This appears to be working but I have added a file called cover.jpg to some folders so I have to take account of that in this script. Not just sure on how to do that.

Thanks

Laurie

Re: Comparing Folders by Number of Files

PostPosted: Thu Dec 14, 2017 5:23 am
by laurie_lewis
Nope - the count of folders does not appear to be working.

Re: Comparing Folders by Number of Files

PostPosted: Thu Dec 14, 2017 11:27 am
by Mr_Noodle
This type of thing is outside the support I can give though on the Hazel side, I believe you need a rule to tell Hazel to go into subfolders. If you search the help, you'll find a chapter on that.

Re: Comparing Folders by Number of Files

PostPosted: Thu Dec 14, 2017 5:50 pm
by laurie_lewis
No problems Mr Noodle. Was hoping some lurkers could help me. No idea how I could have done this without Hazel.

Think I have got it working. Lot of googling, reading and experimenting but here it is at the moment. Found it best to experiment in the shell to find out how the script was working as I could get the variable values to come up using the echo command.

This compares a count of the two folders. In this case it is only counting m4a files.

DIR_NAME="$1"
COUNT1=0
COUNT2=0

COUNT1=$(find "$DIR_NAME" -name "*.m4a" | wc -l)

PATH1='/dir/path/needed/1'
PATH2='/dir/path/needed 2'
DIR_NAME=${DIR_NAME/$PATH1/$PATH2}

COUNT2=$(find "$DIR_NAME" -name "*.m4a" | wc -l)

if [ $COUNT1 -eq $COUNT2 ]; then exit 0; else exit 1; fi


So just to explain it a bit. Using find for the directory and then piping it through word count to count the lines. Next using defining the parts of the path that I need swapped out to use in the search and replace process. Next the count of the new directory and finally comparing the two counts. Returning 0 if same and 1 if not.

A bit of testing to go.

All the best

Laurie

Re: Comparing Folders by Number of Files

PostPosted: Fri Dec 15, 2017 11:57 am
by Mr_Noodle
I would suggest echo-ing the values of the variables at different points in the script. If you turn on debugging mode in Hazel (https://www.noodlesoft.com/kb/hazel-debug-mode/), it should appear in the logs.