Delete companion file is main file is missing

34 views
Skip to first unread message

nmyshkin

unread,
Jan 1, 2026, 6:58:55 PMJan 1
to Tasker
I am working to automate a feature that should have existed on an e-reader but does not: removing cover image files when an ebook is deleted.

So far I have all the existing ebooks in a variable %epub (maybe not needed)
I also have all the existing cover images in a variable &coverimages

I have manipulated (search/replace) %coverimages to convert the list to contain the paths and names of the epubs that the covers go with.

Now I am stuck. I think I need some kind of for/next loop in which &coverimages is tested against the actual epub files and if there is not a match, the cover image file is deleted. Leaving aside the problem that I've lost my original list of cover images (I can always save that before search/replace), I can't seem to figure out a structure for the loop.

My goal is to attach this to another routine that determines and displays the calendar date such that when the date is "1" (i.e., once a month, at the beginning), any orphaned cover images will be deleted.

Any help with the loop process?

Marta Hintz

unread,
Jan 7, 2026, 1:22:21 AMJan 7
to Tasker
Here's the complete Ebook Cover Cleanup Tasker task. Hopefully you canb attach it to your calendar routine (day=1 trigger

Task: Ebook Cover Cleanup
A1: List Files [
    Dir: %EBOOK_DIR (your epub folder path)
    Match:*.epub
    Include Hidden Files:Off
    Use Root:Off
    Sort:Alphabetic
    Variable Array:%current_epubs
    Use Global:On
]

A2: Variable Set [
    Name:%deleted_count
    To:0
    Do Maths:Off
]

A3: Variable Set [
    Name:%deleted_list
    To:
    Do Maths:Off
]

A4: For [
    Variable:%cover_idx
    Items:1:%coverimages(#)
]

A5: Variable Set [
    Name:%test_epub
    To:%coverimages(%cover_idx)
    Do Maths:Off
]

A6: Test File [
    Type:Exists
    Data:%test_epub
    Store Result In:%epub_exists
]

A7: If [ %epub_exists ~ 0 ]

A8: Variable Set [
    Name:%deleted_count
    To:%deleted_count + 1
    Do Maths:On
]

A9: Array Push [
    Name:%deleted_list
    Position:1
    Value:%coverimages(%cover_idx)
    Continue Task After Error:On
]

A10: Delete File [
    File:%coverimages(%cover_idx)
    Shred Level:0
    Use Root:Off
    Use Global:On
]

A11: End If

A12: End For

A13: Flash [
    Text:✅ Cleanup Complete! Deleted %deleted_count orphaned covers:
%deleted_list()
    Long:On
    Position:Long
    Show Always:On
]

Loop Logic: Iterates each manipulated epub name in `%coverimages` array (1-based indexing). Tests existence against filesystem.

Safety Tracking:
- `%deleted_count`: Total files removed
- `%deleted_list()`: Array of **exact paths deleted** (your undo reference!)

Final Display: Long Flash shows count + full list of deleted paths. Copy/paste from notification if needed for recovery.

Usage Notes
- Set `%EBOOK_DIR` before calling (e.g., `/storage/emulated/0/Kindle/` or your e-reader path)
- `%coverimages` must contain **full epub filenames** (your Search/Replace result)


Test First!
Run manually once with 1-2 test orphaned covers to verify paths match. The Flash will show exactly what gets deleted.


Try this to get started, maybe this will help.
Reply all
Reply to author
Forward
0 new messages