Script: Move WMC and MediaPortal Recordings to NAS

I use a Home Theater PC (HTPC) to record, store, and share movies, television shows, and recordings.  All of the videos are stored on a Network Attached Storage and I needed a way to transfer my recordings from MediaPortal or Windows Media Center to a NAS device without breaking any databases or recording availability.

Download this script

@ECHO OFF

REM **********************************************************************
REM move_recordings.bat
REM Created by Paul Vreeland
REM digitalstimulus@gmail.com
REM This script may be freely distributed, copied, and modified.
REM Modified: 02 DEC 2013
REM **********************************************************************
REM This batch file will automate the following tasks:
REM 1. Move recorded TV files from local drive to network location
REM 2. Clean up additional recording files created by MediaPortal
REM 3. (Optional) Update the MediaPortal MySQL database file location to
REM	   point the new network location

REM This script has been tested and should work with Windows Media Center
REM recordings as well as MediaPortal recordings.

REM Requirements:
REM 1. "forfiles.exe" (comes with Vista/7/8, download separately for XP)
REM 2. TV recordings already setup to record using local storage
REM 3. Read/Write access to a network location
REM 4. Windows Media Center or MediaPortal setup to view network recordings
REM 5. (Optional) For MediaPortal recordings: mysql.exe client

REM Instructions:
REM 1. Download or copy this batch file to your computer
REM 2. Change options below to fit your setup
REM 3. Run Task Scheduler and create a new task called "move_recordings"
REM 4. Set task options as follows:
REM    a. Daily (whatever time you want)
REM    b. Start a program: C:\downloaded_file_location\"move_recordings.bat"
REM 5. Under advanced properties for the task make sure the following are set:
REM    a. Run as user account that has read/write privileges to network location
REM    b. Run whether user is logged on or not
REM    c. Wake the computer to run this task

REM Instructions for MediaPortal local and network recording storage:
REM 1. Setup MediaPortal TV Server to use your local storage
REM 2. Run MediaPortal "TV-Server Configuration"
REM 3. Navigate to Recording -> "Folders" tab
REM 4. Select an unused tuner option (I used "MediaPortal IPTV Source Filter")
REM 5. Select network location using \\HOST\SHARE network URI
REM 6. Modify this batch file below with same \\HOST\SHARE network URI

REM **********************************************************************
REM Change these options to fit your setup
REM **********************************************************************

REM Move recordings that are older than XX days: 1=Yesterday, 2=Two days ago, etc
SET _days_old=1

REM Source recording location
SET _source=%PUBLIC%\Recorded TV

REM Destination folder for recordings
SET _dest=\\NASTASTIC\Recorded_TV

REM (Optional for MediaPortal) Path to mysql.exe
REM Find the MySQL bin folder on your computer and paste
REM the folder name here.  It must contain "mysql.exe"!
SET _mysql_exec=C:\Program Files\MySQL\MySQL Server 5.6\bin

REM (Optional for MediaPortal) MySQL Hostname
REM Default: localhost
SET _mysql_host=localhost

REM (Optional for MediaPortal) MySQL MediaPortal Database
REM Default: mptvdb
SET _mysql_db=mptvdb

REM (Optional for MediaPortal) MySQL Username
REM Default: root
SET _mysql_user=MP_User

REM (Optional for MediaPortal) MySQL Password
REM Default: MediaPortal
SET _mysql_pass=MP_Password

REM **********************************************************************
REM Nothing below here should need to be edited
REM **********************************************************************

REM This is a temporary file we create to hold filenames to be moved due
REM to a 253 character constraint in the FORFILES /C argument
SET _mp_temp=%TEMP%\mp_tmp
DEL "%_mp_temp%" >NUL 2>&1

REM Loop through all files from _source and add them to the temporary file
REM if older than _days_old
FORFILES /p "%_source%" /d -%_days_old% /C "cmd /c ECHO @FILE>>^"%_mp_temp%^""

REM Loop through temporary files and move from _source to _dest
FOR /F "tokens=*" %%A IN ( %_mp_temp% ) DO (
	SET _string=%%~A
	
	@SetLocal EnableDelayedExpansion
	
	IF /I NOT "!_string!"=="desktop.ini" (
		ECHO Moving "%_source%\!_string!" to "%_dest%"
		MOVE "%_source%\!_string!" "%_dest%"
		
		REM Update MySQL MediaPortal database with new location after moving a .ts recording
		IF /I "!_string:~-3,3!"==".ts" (
			"%_mysql_exec%\mysql.exe" -h"%_mysql_host%" -u"%_mysql_user%" -p"%_mysql_pass%" -D"%_mysql_db%" -e "UPDATE recording SET fileName = REPLACE(fileName, '%_source:\=\\%', '%_dest:\=\\%') WHERE fileName LIKE '%%!_string!'"
		)
	)
	@EndLocal
)

REM Cleanup our temporary file
DEL "%_mp_temp%" >NUL 2>&1

REM MediaPortal creates files other than the .ts (Transport Stream video) file when
REM recording (.xml, .jpg, .txt, .log, and .edl).  This batch will move those files
REM to the network location automatically.  When you manually delete a recording in
REM MediaPortal, these files are leftover.  This section will locate leftover files
REM without a matching .ts recording and delete them.

REM Loop through .xml files and see if there is a matching .ts video recording file
FOR %%F IN ("%_dest%\*.xml") DO (
	IF NOT EXIST "%_dest%\%%~NF.ts" (
		DEL "%_dest%\%%~NF*"
	)
)

REM Loop through .jpg files and see if there is a matching .ts video recording file
FOR %%F IN ("%_dest%\*.jpg") DO (
	IF NOT EXIST "%_dest%\%%~NF.ts" (
		DEL "%_dest%\%%~NF*"
	)
)

REM Loop through .txt files and see if there is a matching .ts video recording file
FOR %%F IN ("%_dest%\*.txt") DO (
	IF NOT EXIST "%_dest%\%%~NF.ts" (
		DEL "%_dest%\%%~NF*"
	)
)

REM Loop through .log files and see if there is a matching .ts video recording file
FOR %%F IN ("%_dest%\*.log") DO (
	IF NOT EXIST "%_dest%\%%~NF.ts" (
		DEL "%_dest%\%%~NF*"
	)
)

REM Loop through .edl files and see if there is a matching .ts video recording file
FOR %%F IN ("%_dest%\*.edl") DO (
	IF NOT EXIST "%_dest%\%%~NF.ts" (
		DEL "%_dest%\%%~NF*"
	)
)

Download this script

Leave a Reply

Your email address will not be published. Required fields are marked *