[Feat. Req.] Option to automatically delete orphaned folders

All your suggestions, requests and ideas for future development
Post Reply
STiFU
Posts: 4
Joined: 17 Dec 2015, 17:45

[Feat. Req.] Option to automatically delete orphaned folders

Post by STiFU »

Some TV shows extract into a subfolder rather than to the "root"-directory. After cleaning up my files using filebot, I always end up with loads of empty or almost-empty folders and every now and then a non-empty folder in between, which prevents me from simply deleting all folders. An option to automatically delete these orphaned folders if say their size is below 100 kB would be appreciated.

I tried to write a batch script for the windows commandline yesterday to do the job. It worked in a test-directory. However, when I applied it to my actual folder, I accidentally deleted quite a few files I did not want to delete. Dang! :D It's probably got something to do with the data type used in batch-variables and overflows thereof. In case any batch-script pros lurk around, here's my script:

Code: Select all

echo Deleting empty folders...
setlocal EnableExtensions EnableDelayedExpansion
for /D %%F in (*) do (set /a "size=0" & (for /r %%g in (%%F\*) do (set /a "size=!size!+%%~zg")) & IF !size! LSS 100000 rmdir /S/Q %%F)
STiFU
Posts: 4
Joined: 17 Dec 2015, 17:45

Re: [Feat. Req.] Option to automatically delete orphaned fol

Post by STiFU »

Thanks mate. Must've entered the wrong search terms...
STiFU
Posts: 4
Joined: 17 Dec 2015, 17:45

Re: [Feat. Req.] Option to automatically delete orphaned fol

Post by STiFU »

So here's the deal. My media folder is more of a general folder, with all kinds of things that wait to be sorted into appropriate other folders. Hence, this script does not work for me. It would have to be modified a little. Apparently the script API reference is rather limited, so I won't be able to do it. The logic is pretty simple:
  • Traverse through all folders directly under the root directory
  • Delete the whole current directory only if all contained files (and subfolders) are considered clutter.
I guess the clutter detection function should work for me. Any hints would be appreciated.
User avatar
rednoah
The Source
Posts: 23004
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: [Feat. Req.] Option to automatically delete orphaned fol

Post by rednoah »

Isn't that what it's doing? Have you tried just running it with --action test and see what happens?

Check hasMediaFiles and isClutter. hasMediaFiles is true for folders that have one or more non-clutter files, which means files in those folders won't be touched.

Once files get deleted, it'll leave empty folders, which will also be deleted in a last step.

@see https://github.com/filebot/scripts/blob ... ner.groovy

PS: FileBot just adds a few extra to the Groovy context, so in terms of general coding your primary reference would be Groovy
:idea: Please read the FAQ and How to Request Help.
STiFU
Posts: 4
Joined: 17 Dec 2015, 17:45

Re: [Feat. Req.] Option to automatically delete orphaned fol

Post by STiFU »

Yeah, I tried it with --action test and it queued for example all jpg files inside my subfolders for deletion, regardless of other content that might be contained in the folder.

Anyway, thanks for the tip, I think I managed it now.... somehow! :D I commented out line 49 and 52 and just did it all by altering the last line. I am no Java-Coder and I find this kind of code absolutely not human-readable, so please excuse my bland stupidity. :D

The code now checks on each folder, if it is a direct subfolder under the root directory

Code: Select all

it.getParent().equals("HARDCODED STRING TO MY PATH")
and then checks if all files in that folder are clutter, by checking if any file is non-clutter and negating the result

Code: Select all

!it.getFiles().find{ !isClutter(it) }
I couldn't figure out how to check getParent() against the passed /path/to/media, so I hardcoded it. :D Here's the complete line of code.

Code: Select all

args.getFolders().each { if (it.isDirectory() && it.getParent().equals("HARDCODED STRING TO MY PATH") && !it.getFiles().find{ !isClutter(it) }) { clean(it) } }
Post Reply