Search for a specific file in a set of folders. If that file is the only one in the folder, delete the folder

1

I have a very large eBook library. The structure is as follows:

/Library
   /Author Name
      /Book Name
         /{.pdf .mobi .epub eBook files}, {.opf metadata files}, {.jpg book cover files}

About 15% of the 11,000 "Book Name" folders I have contain only a .opf file and sometimes a .jpeg/.jpg/.png/.gif cover file, with no eBook files.

I would like to write something that can find folders that do not contain an eBook file (.pdf/.epub/.mobi) and delete the contents of the "Book Name" folder, the "Book Name" folder itself, and if it is the only "Book Name" folder in the "Author Name" folder, delete the "Author Name" folder, as well.

I am completely knew to this, but will continue to research and post my results here if someone else can't help me out.

Bob

Posted 2015-06-25T23:25:23.240

Reputation: 11

Answers

1

Assuming you're on a recent Windows... I'd break this in two steps.

CD to "Library" and...

  1. Remove files in folders with no ebooks:

    for /f "delims=" %F in ('dir /s/b/a-d *.opf') do dir /b "%~pF*.pdf" "%~pF*.epub" "%~pF*.mobi" || del "%~pF*.*"
    

    (If you have many files to erase, add the /q option to del not to have prompts)

  2. Recursively delete empty directories in Windows:

    for /f "delims=" %D in ('dir /s/b/ad ^| sort /r') do rd "%D"
    

SΛLVΘ

Posted 2015-06-25T23:25:23.240

Reputation: 1 157

Thanks Salvo, what a great solution. I'm going to try it out soon. – Bob – 2015-06-26T01:53:03.530