Skip to content

Instantly share code, notes, and snippets.

@psitem
Last active November 1, 2023 17:41
Show Gist options
  • Save psitem/77ac095ded4d84ba3479 to your computer and use it in GitHub Desktop.
Save psitem/77ac095ded4d84ba3479 to your computer and use it in GitHub Desktop.
My naive Batch script for moving Television torrents
@ECHO OFF
GOTO :TheLoop
:TheFile
FOR /F "tokens=1-7 delims=. " %%a in (%1) do (
IF EXIST "\\server\tv\%%a %%b %%c %%d %%e %%f %%g" CALL :MoveFile %1 "\\server\tv\%%a %%b %%c %%d %%e %%f %%g\" & GOTO :EOF
IF EXIST "\\server\tv\%%a %%b %%c %%d %%e %%f" CALL :MoveFile %1 "\\server\tv\%%a %%b %%c %%d %%e %%f\" & GOTO :EOF
IF EXIST "\\server\tv\%%a %%b %%c %%d %%e" CALL :MoveFile %1 "\\server\tv\%%a %%b %%c %%d %%e\" & GOTO :EOF
IF EXIST "\\server\tv\%%a %%b %%c %%d" CALL :MoveFile %1 "\\server\tv\%%a %%b %%c %%d\" & GOTO :EOF
IF EXIST "\\server\tv\%%a %%b %%c" CALL :MoveFile %1 "\\server\tv\%%a %%b %%c\" & GOTO :EOF
IF EXIST "\\server\tv\%%a %%b" CALL :MoveFile %1 "\\server\tv\%%a %%b\" & GOTO :EOF
IF EXIST "\\server\tv\%%a" CALL :MoveFile %1 "\\server\tv\%%a\" & GOTO :EOF
REM Move items in subdirectories to the parent so that the subdirectory can be removed.
MOVE %1 %~dp0 > NUL 2>&1
)
GOTO :EOF
:MoveFile
ECHO Move: %1 %2
MOVE /Y %1 %2 > NUL 2>&1
ECHO Moved.
GOTO :EOF
:InnerLoop
FOR /F "delims=" %%i in ('dir *.avi *.mkv *.mp4 *.srt /b 2^>NUL') DO (
Call :TheFile "%%i"
)
GOTO :EOF
:TheLoop
REM Process files in current directory.
CALL :InnerLoop
REM Process files in subdirectories and remove the subdirectories.
for /d %%d in (*) do (
PUSHD %%d
CALL :InnerLoop
POPD
ECHO Removing Directory: %%d
rd "%%d" /s /q
)
TIMEOUT /T 10 > NUL
GOTO :TheLoop
GOTO :EOF
@psitem
Copy link
Author

psitem commented Feb 13, 2015

This is the script I use to process torrents into directories on my media server. It's pretty naive in that it simply tokenizes the filenames and looks for directories that match the first 7 tokens, then the first 6, and so on until it finds a match. I've recently updated it to handle subdirectories of the source folder for multi-file torrents, but again, it is very naive and you probably don't want to run this against a directory where non-media torrents are placed since it will delete the subdirectories after moving the media files.

I use NSSM to run the script as a service and log the output. https://nssm.cc/

If you can't understand what this script is doing, you probably shouldn't run it.

@psitem
Copy link
Author

psitem commented Nov 1, 2023

In the age of Sonarr, this script has been obsolete for years and is left here for historical purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment