1

I use Plex and when you click the "delete" button, it removes ONLY the video file. If the video file happened to come in a folder and with subtitles, all of that junk will be left behind. So I'm trying to create a scheduled task batch file that will run once a day that will clean up all the left over junk. Here is what I have come up with so far, but it doesn't seem to work as expected :(

rem setting folder variables
set "mymov=E:\Movies\My"
set "hermov=E:\Movies\Her"
set "themmov=E:\Movies\Them"

set folders=%mymov% %hermov% %themmov%

set folder=nul

set delete=no

for %%f in (%folders%) do (
  cd /D %%f
  for /R %%d in (.) do (
    set folder=%%~fd
    for %%m in (*.mkv *.mp4 *.wtv *.avi) do (
      if exist %%~fd\%%m (
        goto :break
      else (
        set delete=yes
      )
    )
    :break
    if %delete%==yes (rd /S /Q %folder%)
    set delete=no
  )
)

**UPDATE1: With the help of Stephan and Rishav, I have put together a solution:

@echo off
setlocal EnableDelayedExpansion

rem setting folder variables
set "mymov=E:\Movies\My"
set "hermov=E:\Movies\Her"
set "themmov=E:\Movies\Them"

set folders="%mymov%" "%hermov%" "%themmov%"

for %%f in (%folders%) do (
  cd /D %%f
  for /D %%d in (*.*) do (
    set keep=no
    for %%m in (mkv mp4 wtv avi) do if exist %%~fd\*.%%m set keep=yes
    if !keep!==no rd /S /Q "%%~fd"
  )
)

**UPDATE2: I would actually like to expand on this. Is there a way I could just specify a root folder, ex. "E:\Movies", and have it intelligently decipher which is a junk folder and which is a folder that has sub-folder that has legit video files? For instance. If I set one of my movies folders as "E:\Movies\My" and it has a bunch of movie folders with video files in each (unless it's been deleted, then my script would remove that folder), but what about trilogies? Let's say I had "The Hobbit" inside my "E:\Movies\My" folder, but it didn't have a valid video file because it's a container for the 3 Hobbit movies - my script would delete the entire "The Hobbit" folder, in turn, deleting all 3 movies :( I was hoping there was someone out there that knows a ton more than I do about cmd scripting and could drop a knowledge bomb on this. Maybe try to figure out how to work backwards. Like, if it finds a video file, that entire tree is safe and nothing will delete.

Jeremy
  • 43
  • 1
  • 11

2 Answers2

1

reverse your logic:

...
set keep=no
for %%m in (avi mp4 wtf mkv) do if exist "*.%%m" set keep=yes
if "!keep!"=="no" rd /S /Q "%%~fd"
...
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • I like the idea of this! I tested and it deleted EVERY folder in my test folder, even ones with test .mp4 files. So here's how I have my folder structure: `-My --Movie1 ---movie.mp4 --Movie2 ---movie.mp4 ---movie.srt` – Jeremy Jun 06 '16 at 16:52
  • I have figured it out! I will edit my original question to include my answer. Thank you for your contribution! – Jeremy Jun 06 '16 at 17:18
0

EDIT:

@echo off
    rem setting folder variables
    set "mymov=D:\Movies\My"
    set "hermov=D:\Movies\Her"
    set "themmov=D:\Movies\Them"
    set folders=%mymov% %hermov% %themmov% 
    set ext=".mp4" ".mov" ".avi" 
    set test=0

    for %%f in (%folders%) do (
        echo "searching in %%f"
        cd /D %%f
        for %%i in (*.*) do (
                for %%b in (%ext%) do (
                if "%%~xi"==%%b (
                    set test=1
                    echo "match_found_not_deleting %%f")
             )
        )
        if %test%==0 (
            echo deleting %%f
            rd /s /q %%f
              )
    )

I guess this ↑↑↑ is what you wanted. Old material (Just in case its useful) ↓↓↓

@echo off
setlocal EnableDelayedExpansion
set exclude=.avi.mp4.wtv.mkv.
for %%f in (*.*) do (
   if /I "%exclude%" == "!exclude:%%~Xf.=!" del "%%f"
)

I tested it. This will work!
It would delete all the files except the four formats. Keep track in the directory you are working.

The following code posted by dbenham also might do the work. (Not tested)

@echo off
for /f %%F in ('dir /b /a-d ^| findstr /vile ".sdb .sbk .log .bat"') do del "%%F"

HOPE I HELPED.

Rishav
  • 3,818
  • 1
  • 31
  • 49
  • Thank you for thinking a little differently and taking the opposite approach! Unfortunately, this will also delete all files that pertain to the video file, for instance, subtitle files, poster files, metadata files, etc. I only want to delete the directory if no video file exists. – Jeremy Jun 06 '16 at 14:12
  • Actually i am not very clear what you want to do. Do you want to delete all the files from a folder and also the folder if no video files exists? – Rishav Jun 06 '16 at 14:45
  • Correct, that is what I want to accomplish. Delete the folder and everything in it, if no video file exists in that folder. – Jeremy Jun 06 '16 at 16:01
  • OK I got it. After working for hours I made the logic. Will edit my answer. – Rishav Jun 06 '16 at 16:08
  • This closely resembles my original code, but after testing it, it deleted EVERY folder in my Test folder, even ones with test .mp4 files. – Jeremy Jun 06 '16 at 16:54
  • I have figured it out! I will edit my original question to include my answer. Thank you for your contribution! – Jeremy Jun 06 '16 at 17:19
  • that's great you found out the solution but wonder in my script when the value of test did become 1 after the _if_ becomes true still _%test%_ came out to be 0 even after encountering set test=1 – Rishav Jun 06 '16 at 17:44
  • 1
    @Rishav: that's because you didn't use [delayed expansion](http://stackoverflow.com/a/30284028/2152082) – Stephan Jun 06 '16 at 20:15