Jumlins TechBlog

The coding hell and the daily IT of Niklas Jumlin

February 24th, 2011 by Niklas Jumlin

I couldn’t find any unpacker that would extract and delete archives for a torrent after a certain period of time or after a certain seed ratio had been reached. I’ve noticed quite alot of people complain about this on, for example, uTorrent’s official forums.

So I wrote this script that will wait for a set amount of hours when a torrent finishes before extracting, deleting archives, moving files into its right location, scraping with EMM or EMM-R (command-line) AND notify XBMC of your new download and update its library.

This script will NOT stop your torrents.

If you’re downloading TV-Shows in uTorrent, chances are high that you download it like this:
K:\TV-Series\Supernatural\Season 06\Supernatural.S06E04.HDTV.XviD-REL\*.rar

But afterwards you’ll have to extract the contents of the rar, and move it one folder up if you’re using XBMC or something else. Like this:
K:\TV-Series\Supernatural\Season 06\supernatural.s06e04.hdtv.xvid-rel.avi

This script does that job automatically.

The script will also work for movies, with multiple subdirectories. :nod:

Lets say you have it like this:
K:\Movies\Awesome.Movie.DVDRip.XviD-REL\CD 1\*.rar
K:\Movies\Awesome.Movie.DVDRip.XviD-REL\CD 2\*.rar

This script will then extract from both those folders into its parent folder. Like this:

It also works for single file movies.



The script will try to delete these files and folders:

* “<download_path>\CD{1-4}\”
* “<download_path>\CD {1-4}\”
* “<download_path>\Sample\”
* “<download_path>\*.rar”
* “<download_path>\*.sfv”
* “<download_path>\*.r0{1-9}”
* “<download_path>\*.r{10-99}”

Tested with uTorrent v2.2 Build 23703, Windows Server 2008 R2 x64.

System Requirements
* uTorrent Version 2.2 (build 21882+)
* timeout (Windows Vista/7)
* WinRAR (with Command-Line unrar.exe)
* XBMC 10 (Dharma)

A new command in Vista/7 is “timeout”. It will cause the command processor to wait for a specified number of seconds or until a key is pressed.

Call the script from within uTorrents GLOBAL settings.
Options, Preferences, Advanced, Run Program, Run this program when a torrent finishes:

"C:\Autoextract.bat" "%N" "%D" "%L"

uTorrent will parse it like this:

A movie download, labeled “Movie”:

"C:\AutoExtract.bat" "Awesome.Movie.DVDRip.XviD-REL" "K:\Movies\Awesome.Movie.DVDRip.XviD-REL" "Movie"

A tv-show download, labeled “TV-Show”:

"C:\AutoExtract.bat" "Supernatural.S06E04.HDTV.XviD-REL" "K:\TV-Series\Supernatural\Season 06\Supernatural.S06E04.HDTV.XviD-REL" "TV-Show"

A picture says more than a thousand words…

When a torrent finishes, the script starts

New movie notification in XBMC
XBMC New Movie

New episode notification in XBMC
XBMC New Episode

Edit config.cmd to suit your needs.
Below are the two sources: AutoExtract.bat and config.cmd


@echo off
SetLocal EnableDelayedExpansion
mode 90,15
color 0a
rem ------------------------------------------------------
rem -
rem -    Version: 1.0 build 18 (2011-03-27 18:13)
rem -
rem Dont edit this file unless you know what you are doing
rem -
rem ------------------------------------------------------
set version=1.0 build 18
set title=%1
set config=y
set currentpath=%~dp0
call %currentpath%config.cmd || color 0c && echo could not load config.cmd, check website for info. && pause && exit
set torrentpath=%2
set label=%3
if "%~3" LEQ "" set label=none
rem remove " (quotes) from label
set label=%label:"=%
rem remove " (quotes) from title
set title=%title:"=%
rem if title is a path, strip down to folder name
for /f "Tokens=* Delims=\" %%a in ("%title%") do set title=%%~nxa
rem remove " (quotes) from torrentpath
set torrentpath=%torrentpath:"=%
rem Wait time in hours
set /a wait_h=%wait%/%wait%
rem convert hours to minutes
set /a wait_m=%wait_h%*60
rem convert minutes to seconds
set /a wait_s=%wait_m%*60
rem if wait is 0, dont wait
if %wait%==0 set wait_s=0
rem set TV torrent path to one folder up from torrent path
for /f "Tokens=*" %%i in ('dir /w "%torrentpath%\..\" ^| find /i "Directory of"') do (
	set tv_torrentpath=%%i
	set tv_torrentpath=!tv_torrentpath:Directory of =!
	if %label%==%tvshow_label% set extract=!tv_torrentpath!
	if %label%==%movie_label% set extract=%torrentpath%
rem process number
for /f "Tokens=*" %%i in ('tasklist ^| find /i /c "cmd.exe"') do (
	set /a proc=%%i-1
if %showcountdown%==y set sc=
if %showcountdown%==n set sc=^> nul
if "%label%" EQU "none" goto nolabel
if %label%==%movie_label% goto start
if %label%==%tvshow_label% goto start
color 0e
echo Title: %title%
echo Label: %label%
echo Label was not recognized
echo Will not do anything!
echo Exiting...
color 0a
@FOR /L %%t IN (1,1,%wait%) DO (
	set /a hleft=%wait%-%%t+1
	title !hleft! hours remaing before unrar starts for: %title%
	echo AutoExtract.bat v%version%
	echo Process %proc%
	echo !hleft! hours remaing before unrar starts for:
	echo %title%
	echo Path: %torrentpath%
	echo Extract to: %extract%\
	echo Press any key to skip 1 hour ^(or cancel timeout when it is 1 hour left^)
	timeout /t %wait_s% %sc%
@FOR /L %%t IN (0,1,9) DO (
	set num=%%t
	rem for .rar
	if exist "%torrentpath%\*.rar" set rar=rar
	if exist "%torrentpath%\CD 1\*.rar" set rar=cd_1rar
	if exist "%torrentpath%\CD 2\*.rar" set rar=cd_2rar
	if exist "%torrentpath%\CD1\*.rar" set rar=cd1rar
	if exist "%torrentpath%\CD2\*.rar" set rar=cd2rar
	rem for .r01-09
	if exist "%torrentpath%\*.r0!num!" set rar=r0x
	if exist "%torrentpath%\CD 1\*.r0!num!" set rar=cd_1r0x
	if exist "%torrentpath%\CD 2\*.r0!num!" set rar=cd_2r0x
	if exist "%torrentpath%\CD1\*.r0!num!" set rar=cd1r0x
	if exist "%torrentpath%\CD2\*.r0!num!" set rar=cd2r0x
@FOR /L %%t IN (10,1,99) DO (
	set num=%%t
	rem for .r10-99
	if exist "%torrentpath%\*.r!num!" set rar=rxx
	if exist "%torrentpath%\CD 1\*.r!num!" set rar=cd_1rxx
	if exist "%torrentpath%\CD 2\*.r!num!" set rar=cd_2rxx
	if exist "%torrentpath%\CD1\*.r!num!" set rar=cd1rxx
	if exist "%torrentpath%\CD2\*.r!num!" set rar=cd2rxx
if not defined rar goto nounrar
rem else continue
title Unraring: %title%
"%unrar_path%\unrar.exe" x -y -r -o%overwrite% "%torrentpath%\*.rar" "%torrentpath%\" && set delete=%delete_after_unrar% || color 0c && set delete=n && echo Something 
went wrong extracting files && pause
if exist "%torrentpath%\*sub*" "%unrar_path%\unrar.exe" x -y -r -o%overwrite% "%torrentpath%\*sub*" "%torrentpath%\"
rem move extracted TV-Show and NFO one folder back
if %label%==%tvshow_label% goto move
if not %label%==%tvshow_label% goto nomove
color 0a
if not defined rar goto nomove
if not defined delete goto nomove
if not %delete%==y goto nomove
if exist "%torrentpath%\*.avi" move "%torrentpath%\*.avi" "%torrentpath%\..\"
if exist "%torrentpath%\*.mkv" move "%torrentpath%\*.mkv" "%torrentpath%\..\"
if exist "%torrentpath%\*.nfo" move "%torrentpath%\*.nfo" "%torrentpath%\..\"
if exist "%torrentpath%\*.srt" move "%torrentpath%\*.srt" "%torrentpath%\..\"
if exist "%torrentpath%\*.idx" move "%torrentpath%\*.idx" "%torrentpath%\..\"
if exist "%torrentpath%\*.sub" move "%torrentpath%\*.sub" "%torrentpath%\..\"
color 0a
if not defined delete goto emmupdate
if not %delete%==y goto emmupdate
if %delete%==y goto clean
rem start cleaning up
echo cleaning up archive files, should take about 2-20 seconds...
if exist "%torrentpath%\*.dat" del /f /q "%torrentpath%\*.dat"
if exist "%torrentpath%\*sample*" del /f /q "%torrentpath%\*sample*"
if exist "%torrentpath%\*.rar" del /f /q "%torrentpath%\*.rar"
if exist "%torrentpath%\*.sfv" del /f /q "%torrentpath%\*.sfv"
@FOR /L %%t IN (0,1,9) DO (
	set num=%%t
	if exist "%torrentpath%\*.r0!num!" del /f /q "%torrentpath%\*.r0!num!"
@FOR /L %%t IN (10,1,99) DO (
	set num=%%t
	if exist "%torrentpath%\*.r!num!" del /f /q "%torrentpath%\*.r!num!"
rem clear subdirs
echo cleaning up subdirectories (CD1,CD2), should take less than 4 seconds...
@FOR /L %%t IN (0,1,4) DO (
	set num=%%t
	if exist "%torrentpath%\CD!num!" rmdir /s /q "%torrentpath%\CD!num!"
	if exist "%torrentpath%\CD !num!" rmdir /s /q "%torrentpath%\CD !num!"
if exist "%torrentpath%\Sample" rmdir /s /q "%torrentpath%\Sample"
rem If files were locked
if exist "%torrentpath%\*.dat" del /f /q "%torrentpath%\*.dat"
if exist "%torrentpath%\*sample*" del /f /q "%torrentpath%\*sample*"
if exist "%torrentpath%\*.rar" del /f /q "%torrentpath%\*.rar"
if exist "%torrentpath%\*.sfv" del /f /q "%torrentpath%\*.sfv"
if exist "%torrentpath%\*sample*" echo. && echo "*sample*" File/folder was in use... && echo. && timeout /t 60 && del /f /q "%torrentpath%\*sample*"
if exist "%torrentpath%\*.dat" echo. && echo ".dat" File/folder was in use... && echo. && timeout /t 60 && del /f /q "%torrentpath%\*.dat"
if exist "%torrentpath%\*.rar" echo. && echo ".rar" File/folder was in use... && echo. && timeout /t 60 && del /f /q "%torrentpath%\*.rar"
if exist "%torrentpath%\*.sfv" echo. && echo ".sfv" File/folder was in use... && echo. && timeout /t 60 && del /f /q "%torrentpath%\*.sfv"
@FOR /L %%t IN (0,1,9) DO (
	set num=%%t
	if exist "%torrentpath%\*.r0!num!" del /f /q "%torrentpath%\*.r0!num!"
	if exist "%torrentpath%\*.r0!num!" echo. && echo ".r0!num!" File/folder was in use... && echo. && timeout /t 60 && del /f /q "%torrentpath%\*.r0!num!"
@FOR /L %%t IN (10,1,99) DO (
	set num=%%t
	if exist "%torrentpath%\*.r!num!" del /f /q "%torrentpath%\*.r!num!"
	if exist "%torrentpath%\*.r!num!" echo. && echo ".r!num!" File/folder was in use... && echo. && timeout /t 60 && del /f /q "%torrentpath%\*.r!num!"
@FOR /L %%t IN (0,1,4) DO (
	set num=%%t
	if exist "%torrentpath%\CD!num!" rmdir /s /q "%torrentpath%\CD!num!"
	if exist "%torrentpath%\CD !num!" rmdir /s /q "%torrentpath%\CD !num!"
	if exist "%torrentpath%\CD!num!" echo. && echo "CD!num!" File/folder was in use... && echo. && timeout /t 60 && rmdir /s /q "%torrentpath%\CD!num!"
	if exist "%torrentpath%\CD !num!" echo. && echo "CD !num!" File/folder was in use... && echo. && timeout /t 60 && rmdir /s /q "%torrentpath%\CD !num!"
if exist "%torrentpath%\Sample" rmdir /s /q "%torrentpath%\Sample"
if exist "%torrentpath%\Sample" echo. && echo "Sample" File/folder was in use... && echo. && timeout /t 60 && rmdir /s /q "%torrentpath%\Sample"
if not %emm_scrape%==y goto xbmcupdate
if %label%==%tvshow_label% goto xbmcupdate
echo Running Ember Media Manager command line scrape...
if exist "%torrentpath%\*.tbn" goto xbmcupdate
if not exist "%torrentpath%\*.tbn" "%emm_path%\Ember Media Manager.exe" -newauto -all && goto xbmcupdate || color 0c && echo Error updating with EMM ("%emm_path%\Ember 
Media Manager.exe") && pause
color 0a
if not %update_xbmc%==y goto quit
echo Running XBMC notify and library update...
if exist "%userprofile%\UpdateXBMC_%title%.vbs" del /f /q "%userprofile%\UpdateXBMC_%title%.vbs"
echo Dim objSvrHTTP >> "%userprofile%\UpdateXBMC_%title%.vbs"
echo WScript.Sleep 1000 * 5 >> "%userprofile%\UpdateXBMC_%title%.vbs"
echo Set objSvrHTTP = CreateObject("MSXML2.XMLHTTP") >> "%userprofile%\UpdateXBMC_%title%.vbs"
if %label%==%movie_label% echo objSvrHTTP.open "GET", "http://%xbmc_user%:%xbmc_pass%@%xbmc_ip%:%xbmc_port%/xbmcCmds/xbmcHttp?command=ExecBuiltIn(Notification(""New 
movie added"",""%title%"",4000,c:\movie.png))", False >> "%userprofile%\UpdateXBMC_%title%.vbs"
if %label%==%tvshow_label% echo objSvrHTTP.open "GET", "http://%xbmc_user%:%xbmc_pass%@%xbmc_ip%:%xbmc_port%/xbmcCmds/xbmcHttp?command=ExecBuiltIn(Notification(""New 
episode added"",""%title%"",4000,c:\tvshow.png))", False >> "%userprofile%\UpdateXBMC_%title%.vbs"
echo objSvrHTTP.send >> "%userprofile%\UpdateXBMC_%title%.vbs"
if %label%==%movie_label% echo objSvrHTTP.open "GET", "http://%xbmc_user%:%xbmc_pass%@%xbmc_ip%:%xbmc_port%/xbmcCmds/xbmcHttp?command=ExecBuiltIn(UpdateLibrary
(video,""%torrentpath%""/))", False >> "%userprofile%\UpdateXBMC_%title%.vbs"
if %label%==%tvshow_label% echo objSvrHTTP.open "GET", "http://%xbmc_user%:%xbmc_pass%@%xbmc_ip%:%xbmc_port%/xbmcCmds/xbmcHttp?
command=ExecBuiltIn&parameter=XBMC.UpdateLibrary(video,!tv_torrentpath!)", False >> "%userprofile%\UpdateXBMC_%title%.vbs"
echo objSvrHTTP.send >> "%userprofile%\UpdateXBMC_%title%.vbs"
rem run script if label is your movie label or tv-show label
if %label%==%movie_label% "%userprofile%\UpdateXBMC_%title%.vbs"
if %label%==%tvshow_label% "%userprofile%\UpdateXBMC_%title%.vbs"


@echo off
if not defined config goto error
if %config%==y goto conf
	echo Do not execute this file directly, it does nothing else than setting variables
rem ------------------------------------------------------
rem -
rem -    Version: 1.0 build 17
rem -
rem -    Edit the settings below here, not above
rem -
rem ------------------------------------------------------
rem set waiting time in hours to wait before unraring
set wait=24
rem show countdown in seconds?
set showcountdown=y
rem path to folder containing unrar.exe from WinRAR
set unrar_path=C:\Program Files (x86)\WinRAR\
rem set to either y or n to delete archive files and other files after unrar (CAREFUL!!!)
rem this will delete archive-files, .dat, .sfv, *sample*-files, CD1, CD2, Sample folder
set delete_after_unrar=n
rem overwrite file(s) to be extracted if it allready exist + for yes, - for no
set overwrite=-
rem uTorrent uses labels, my rss downloader in uTorrent sets 'TV-Show' as label for my tv-show downloads
set tvshow_label=TV-Show
rem uTorrent label for movies
set movie_label=Movie
rem path to ember media manager
set emm_path=C:\Users\Test\Desktop\emm-r v2398_x64
rem scrape with emm?
set emm_scrape=y
rem xbmc connection
set xbmc_ip=
set xbmc_port=8080
set xbmc_user=
set xbmc_pass=
rem update library of xbmc?
set update_xbmc=y
rem dir /w c:\foo\bar generates a header that says Directory of c:\foo\bar, change this into your localization (no space at the end)
set dirheader=Directory of
rem below is experimental! filter out some strings from title
rem set title=%title:.hdtv.=%
rem set title=%title:.hdtv-=%
rem set title=%title:.xvid.=%
rem set title=%title:.xvid-=%
rem set title=%title:.r5.=%
rem set title=%title:.r5-=%
rem set title=%title:.dvdrip.=%
rem set title=%title:.dvdrip-=%
rem set title=%title:.bluray.=%
rem set title=%title:.bluray-=%
rem set title=%title:.repack.=%
rem set title=%title:.repack-=%
rem set title=%title:.proper.=%
rem set title=%title:.proper-=%
rem set title=%title:.limited.=%
rem set title=%title:.limited-=%
rem set title=%title:.x264.=%
rem set title=%title:.x264-=%
rem set title=%title:.dvd9.=%
rem set title=%title:.dvd9-=%
rem set title=%title:.hddvd.=%
rem set title=%title:.hddvd-=%
rem set title=%title:.bdrip.=%
rem set title=%title:.bdrip-=%
rem set title=%title:.stv.=%
rem set title=%title:.stv-=%
rem set title=%title:.ac3.=%
rem set title=%title:.ac3-=%
rem set title=%title:.XXX.=%
rem set title=%title:.XXX-=%
rem set title=%title:.unrated.=%
rem set title=%title:.unrated-=%
rem set title=%title:.extended.=%
rem set title=%title:.extended-=%
rem set title=%title:hdtv=%
rem set title=%title:xvid=%
rem set title=%title:r5=%
rem set title=%title:dvdrip=%
rem set title=%title:bluray=%
rem set title=%title:repack=%
rem set title=%title:proper=%
rem set title=%title:limited=%
rem set title=%title:x264=%
rem set title=%title:dvd9=%
rem set title=%title:hddvd=%
rem set title=%title:bdrip=%
rem set title=%title:stv=%
rem set title=%title:ac3=%
rem set title=%title:XXX=%
rem set title=%title:unrated=%
rem set title=%title:extended=%
rem set title=%title:.= %
rem set title=%title:  =%

Build 1: 2011-02-22
* Forgot to add:
set label=%label:”=% (to remove quotes from label)

Build 2: 2011-02-22
* Added delete subfolders (CD1, CD2, CD 1, CD 2)
* Fixed TV-Show move didn’t work.

Build 3: 2011-02-23
* Fixed timeout, if wait=0 it would crash

Build 4: 2011-02-23
* Major Updates.
* Added scraping with EMM/EMM-r, and then notify XBMC and update its library
Inspiration from watzen’s post @ (http://forum.xbmc.org/showthread.php?t=87472)

Build 5: 2011-02-24
* Added text replacement in title to make it look better (“Awesome.Movie.DVDRip.XviD-REL” would be “Awesome Movie REL”)
* I have also rewritten many parts of the script

Build 6: 2011-02-25
* Removed text replacement in title, can’t figure out how to filter out REL, LiMITED, REPACK, LiNE etc.
* Rewrote major code of the cleanup part

Build 7: 2011-02-26
* Fixed: If no label it would crash
* Added a new variable for TV torrent path, XBMC update script would try to update library from c:\foo\bar\..\ instead of c:\foo

Build 8: 2011-02-27
* Added extraction of rars that were unpacked from a rar (usually subs)

Build 9 2011-02-28
* Added option to not scrape with ember media manager and/or update library of xbmc, including notification to xbmc
* Added option to show countdown in seconds during wait

Build 10 2011-02-28
* Added process numbering (Dunno why yet)
* Script will now append UpdateXBMC.vbs with title, UpdateXBMC_title.vbs to manually re-run if failed

Build 11 2011-03-01 09:43
* Compiled the script to an executable, having a seperate config file.

Build 12 2010-03-02 14:45
* No more compiled script, it messed up some environment variables
* Lots of other bug fixes
* Wrote a total new algorithm for the rar-cleanup, it will work MUCH better now

Build 13 2011-03-03 17:09
* Added version information inside the script
* Added colors and fixed window size

Build 14 2011-03-05 09:27
* The script will now re-loop the cleanup process if files are locked and timeout for 30 seconds before deleting locked files

Build 15 2011-03-15 10:50
* Re-wrote the loop for the cleanup process (again). If files are locked it will timeout for 60 seconds and only for the locked file. This will greatly speed up the process.

Build 16 2011-03-25 18:33
* Added: Check if rar files exist then unrar, else go to EMM-r scrape
* Added: Cleanup of .dat and *sample* files
* Fixed: If not unrared dont move files (keep utorrent running)
* Added: Seperate config-file download

Build 17 2011-03-26 12:37
* Fixed: ‘x’ is not recognized as an internal or external command, operable program or batch file.

Build 18 2011-03-27 18:13
* Fixed: A bug for a very unlikely scenario, whereas delete_after_unrar is set to y and nothing is to be unrared, it would clean up files anyway
* Added: Just a lazy thing really. If the first argument is a path and not a title, it will strip down the path and use the last occuring token after \ (backslash) as title

Share on Facebook

5 Responses to “ Unrar finished torrents (uTorrent) and delete archives (rar) ”

  1. Tobbe says:

    Bra skit men det blir ju lite av en moot-point när XBMC kan spela rar-filer direkt utan att packa upp.

  2. Helt rätt, men då måste XBMC buffra. Spelar du över nätverket, t.ex WLAN så kommer det gå trögt – speciellt för MKV-filer.
    Många har enklare tunna XBMC-klienter med ett trådlöst kort som stödjer 802.11g (54mbit). Deras lagring har dom på en NAS i garderoben eller liknande.

  3. Henning says:

    That XBMC skin looks awesome! What’s its name?

  4. Miro says:

    A little addition: send a prowl notification using curl

    In AutoExtract.bat:
    color 0a
    if not %use_prowl%==y goto quit
    echo Sending Prowl Push Notification Notification…
    C:\curl.exe –data “apikey=%prowl_apikey%&application=%label%&event=Finished downloading&description=%title%” http://api.prowlapp.com/publicapi/add

    In config.cmd:
    rem setting prowl variables, if you don’t want it, change use_prowl to n
    set use_prowl=y
    set prowl_apikey=yourHexAPI

  5. Cool to see an interest after 2 years :) I have this exact script still running though, but I haven’t modified it since mars 2011.

Leave a Reply