Windows Autoplay setup and scripts for backup
Posted: Thu Feb 29, 2024 7:57 am
Hello! long time lurker, first post but I've created some registry files and a batch script to automatically backup disks upon insertion utilizing Window's media Autoplay feature
I also made three registry files... one hijacks VLC's Autoplay of DVDs and Blurays to run the batch script, one to un-hijack, and one to create specific auto backup Autoplay listings for DVDs and Blurays
This is my first batch script in a while so please suggest any improvements to me
Backup.bat
hijack.reg
unhijack.reg
create autoplay.reg
Special thanks to:
I also made three registry files... one hijacks VLC's Autoplay of DVDs and Blurays to run the batch script, one to un-hijack, and one to create specific auto backup Autoplay listings for DVDs and Blurays
This is my first batch script in a while so please suggest any improvements to me
Backup.bat
Code: Select all
:: omit quotes and use back slashes for all paths, flipping of slashes for MKV are automatically handled...
:: DO NOT USE ANY RELITIVE PATHS IF USING WITH AUTORUN autorun will force you to stay in the drive directory
:: if you don't like comments, too bad, they're there for people to understand what's going on
::----------------------------------------Script init-------------------------------------------------
:: hide command inputs and completion outputs... stuff you don't need to see unless you're debugging
@echo off
:: makes all included variables held within the console session so you can run multiple instances
setlocal enableextensions
::---------------------------------------Variables-----------------------------------------------------
:: set path for MakeMKVcon
set mkvcon=C:\Program Files (x86)\MakeMKV\makemkvcon64.exe
:: set backup directory
set backuproot=E:\path\to\MKV\backup
:: set drive letter. use %cd% if using with autorun
set driveletter=%cd%
:: set path for NirCMD if you want auto eject
set nircmd=E:\path\to\nircmd.exe
:: optionally set cache size (MB)
set cache=16
::---------------------------------------Auto Prep-------------------------------------------------------
echo Preparing and cleaning variables
:: notifies about wait time
echo Working on scanning drives for neccecary information to simplify your setup
:: creates an random seed so multiple instances can run without file conflicts
set seed=%random%
title Backup %driveletter% / %seed%
:: filter Drive letter in case of %cd% because we can't accept slashes
set driveletter=%driveletter:~0,2%
:: use MakeMKV to scan drives, find the specified drive letter and output it into a temporary text file
echo Scanning drives for Automatic setup, this may take a while if you have multiple drives
"%mkvcon%" info -r dev:9999 | find "%driveletter%" > "%backuproot%"\driveinfo%seed%.txt
:: pull in drive information from the file so we can actually use it
set /p driveinfo= < "%backuproot%"\driveinfo%seed%.txt
:: remove the temporary drive info file
del "%backuproot%"\driveinfo%seed%.txt
:: set the drive number for our disc so MakeMKV knows which one to use
echo Setting up disc information
set drivenumber=%driveinfo:~4,1%
:: pull the disc name so we can rename the temporary folder later
FOR /f "tokens=6 delims=," %%a IN ("%driveinfo%") do set drivename=%%a
title Backup %driveletter% / %seed% / %drivename%
:: pull disc type number so we can handle extensions during the rename
FOR /f "tokens=4 delims=," %%a IN ("%driveinfo%") do set drivetype=%%a
:: moving to the disc definitions at the end of the script please know this is very rough as I only have a few drives on hand
if %drivetype%==1 goto dvd
if %drivetype%==12 goto bd
if %drivetype%==0 goto invalid
goto unknown
:postassign
::----------------------------------------Verification------------------------------------------------------
:: here so you can verify all information before starting the backup
:: delete or comment out for automated use
cls
echo Almost ready, please verify the information
echo Drive letter is: %driveletter%
echo Drive number is: %drivenumber%
echo Disc name is: %drivename:~1,-1%
echo Backup directory is: %backuproot%
echo Media type is: %mediatype%
echo Backup type: %mediaextension:\=folder%
echo Temporary file seed is: %seed%
:: pause
::-----------------------------------------Backup-----------------------------------------------------------
:: finally we now start the backup
cls
echo Beginning backup
"%mkvcon%" backup --cache=%cache% --decrypt --noscan --progress=-same disc:%drivenumber% "%backuproot:\=/%/curbak%seed%%mediaextension:\=/%"
cls
color 2
echo Backup complete, now to wrap up
:: rename the directory to disc name
ren "%backuproot%\curbak%seed%%mediaextension:\=%" %drivename:~1,-1%%mediaextension:\=%
:: eject disc to indicate finish.. you'll need NirCMD for this
"%nircmd%" cdrom open %driveletter%
goto :EOF
:: headed to the end of file aka were're done here, drive definitions follow
::-----------------------------------Drive Definitions--------------------------------------------------------
:bd
set mediatype=bluray
set mediaextension=\
goto postassign
:dvd
set mediatype=DVD
set mediaextension=.iso
goto postassign
:unknown
set mediatype=!!WARNING!!Unknown media inserted!! Please be aware that no extension will be added. it will either be a folder (\) or likely a file that needs .iso
set mediaextension=
color e
goto postassign
:invalid
set mediatype=!!ERROR!!Unsupported media insterted!! MakeMKV will likely error and if succesfull no extension will be added. you are on your own
set mediaextension=
color c
goto postassign
-------------------------------------End of File--------------------------------------------------------------
Code: Select all
Windows Registry Editor Version 5.00
;paths need to be double backward slashed
;hijack Bluray
[HKEY_CLASSES_ROOT\VLC.Bluray\shell\Open\command]
@="cmd /c \"\"E:\\path\\to\\MKV\\backup.bat\"\""
;hijack DVD
[HKEY_CLASSES_ROOT\VLC.DVDMovie\shell\Open\command]
@="cmd /c \"\"E:\\path\\to\\MKV\\backup.bat\"\""
;Created by DanHolli using information gathered by FER_Viborg on Stack Exchange
Code: Select all
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\VLC.Bluray\shell\Open\command]
@="\"C:\\Program Files\\VideoLAN\\VLC\\vlc.exe\" --started-from-file bluray:///%1"
[HKEY_CLASSES_ROOT\VLC.DVDMovie\shell\Open\command]
@="\"C:\\Program Files\\VideoLAN\\VLC\\vlc.exe\" --started-from-file dvd:///%1"
Code: Select all
Windows Registry Editor Version 5.00
;---------------DVD Movie listing-----------------------------------
[HKEY_CLASSES_ROOT\MKV.DVDMovie]
@="MKV DVD Movie"
[HKEY_CLASSES_ROOT\MKV.DVDMovie\DefaultIcon]
@="\"C:\\Program Files (x86)\\MakeMKV\\makemkvcon64.exe\",0"
[HKEY_CLASSES_ROOT\MKV.DVDMovie\shell]
@="Open"
[HKEY_CLASSES_ROOT\MKV.DVDMovie\shell\Open]
[HKEY_CLASSES_ROOT\MKV.DVDMovie\shell\Open\command]
@="cmd /c \"\"E:\\path\\to\\MKV\\auto\\backup.bat\"\""
[HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Explorer\AutoplayHandlers\Handlers\MKVPlayDVDMovieOnArrival]
"Action"="Backup DVD movie"
"DefaultIcon"="\"C:\\Program Files (x86)\\MakeMKV\\makemkvcon64.exe\",0"
"InvokeProgID"="MKV.DVDMovie"
"InvokeVerb"="Open"
"Provider"="MakeMKV"
[HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Explorer\AutoplayHandlers\EventHandlers\PlayDVDMovieOnArrival]
"MKVPlayDVDMovieOnArrival"=""
;----------------Bluray listing-------------------------------------
[HKEY_CLASSES_ROOT\MKV.Bluray]
@="MKV Bluray"
[HKEY_CLASSES_ROOT\MKV.Bluray\DefaultIcon]
@="\"C:\\Program Files (x86)\\MakeMKV\\makemkvcon64.exe\",0"
[HKEY_CLASSES_ROOT\MKV.Bluray\shell]
@="Open"
[HKEY_CLASSES_ROOT\MKV.Bluray\shell\Open]
[HKEY_CLASSES_ROOT\MKV.Bluray\shell\Open\command]
@="cmd /c \"\"E:\\path\\to\\MKV\\auto\\backup.bat\"\""
[HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Explorer\AutoplayHandlers\Handlers\MKVPlayBlurayOnArrival]
"Action"="Backup Bluray"
"DefaultIcon"="\"C:\\Program Files (x86)\\MakeMKV\\makemkvcon64.exe\",0"
"InvokeProgID"="MKV.Bluray"
"InvokeVerb"="Open"
"Provider"="MakeMKV"
[HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Explorer\AutoplayHandlers\EventHandlers\PlayBlurayOnArrival]
"MKVPlayBlurayOnArrival"=""
;Created by DanHolli using information gathered by FER_Viborg on Stack Exchange
- Nir Sofer for making NirCMD available at https://www.nirsoft.net/utils/nircmd.html
- FER_Viborg for his research into autoplay registry listings found at https://superuser.com/a/1502806/676913
- Timo Salmi who saved a batch script that pulls disc name information that was critical for use before I figured out how to use MakeMKVcon to do this. It can be found at https://stackoverflow.com/a/12143368/7397576
- And of course everyone who has worked on and supported this wonderful tool