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.