Remove extra file formats

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

Moderator: Mr_Noodle

Remove extra file formats Thu Sep 17, 2015 4:18 pm • by awfki
I use Dropbox to make ebooks available for my ereader. When I save the book from Calibre it will save multiple formats if they exist, so I might end up with book1.pdf and book1.mobi and book1.epub.

I'd like Hazel to clean up all the unneeded formats such that: If there's an epub, just keep that. If there's a mobi and pdf just keep the mobi. Only keep the pdf if it's the only format.

Can Hazel do this?

Can you point me at an example?
awfki
 
Posts: 5
Joined: Thu Sep 17, 2015 4:00 pm

Re: Remove extra file formats Fri Sep 18, 2015 12:18 pm • by Mr_Noodle
It's a little tricky but you can try something like:
Code: Select all
If (all) are met
    Extension is pdf
    Name matches (• original name)
    If (all) are met for (the enclosing folder)
        If (all) are met for (any of its subfiles)
            Name matches (• original name)
            If (any) are met for (the current file)
                Extension is mobi
                Extension is epub
Do
    Move to trash

To get the nested conditions, hold down option while clicking the + button. (• original name) is a custom attribute which is defined as matching "anything". That basically says, if it's a pdf and there's either a mobi or epub in the same folder with the same name, then throw it away. You'd have to construct another similar rule to match mobi files to check if there's an epub.
Mr_Noodle
Site Admin
 
Posts: 11868
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Remove extra file formats Fri Sep 18, 2015 1:53 pm • by awfki
Awesome! Thanks very much.

I played with this for a few hours last night and ended up solving it with a bash script. I'll try yours tonight since it doesn't spawn processes and I'll post the bash script here in case it's of interest to anyone else.
awfki
 
Posts: 5
Joined: Thu Sep 17, 2015 4:00 pm

Re: Remove extra file formats Sat Sep 19, 2015 10:43 pm • by awfki
Here's the code from my embedded bash script. I run this on anything where Extension = epub to delete mobi and pdf files with the same name. I have a second rule that runs on mobi and deletes pdf files with the same name.

Code: Select all
file=$1

# save path
filepath=${file%/*}
cd "$filepath"

# strip path
file=${file##*/}

# change extension to mobi and remove mobi if it exists
file=${file%.epub}.mobi
if [ -e "$file" ]; then rm "$filepath/$file"; fi

# change extension to pdf and remove pdf if it exists
file=${file%.mobi}.pdf
if [ -e "$file" ]; then rm "$filepath/$file"; fi
awfki
 
Posts: 5
Joined: Thu Sep 17, 2015 4:00 pm


Return to Support