Page 1 of 1

qBittorrent using external HDD [prevent starting unless drive is online] -script

Posted: Wed Nov 21, 2018 1:54 pm
by xelemorf
Hi All,

I am using qBittorrent to download stuff onto my external hard drive, and it happens, that sometimes I forget to power on the external drive, and in that case, all of the items are going to be missing items, and everything needs to be re-checked to be able to continue seeding or downloading. When you have multiple terabytes of data to seed (or just a lot of huge torrents), this can be a real pain and takes a lot of time, so I have decided to write a script, which would prevent running qBittorrent until the drive is available, and I thought I would share with the community, maybe someone else finds it useful too.

It might not be the best method to handle this, and someone might want to use other scripting languages to achieve the same, but I just wrote this in windows batch in a few minutes, as it's pretty simple to handle such situations, and does not need fancy scripts in powershell or something else. I have tested it on Win7-x64.

//Before you begin:
You'll need to adjust some variables for your system, you can use the disk label, or the serial number if that suits you better.
#define the drive
SET DISKDRIVE=Z:\
# define the temp file path, this temp file is being used by the stringmatch, desirably C:\ drive would be preferred
SET TEMPFILE=R:\DiskCheckTempFile.txt
#this part comes from the output of "dir z:\" command, choose the disk label or serial as you please
SET STRINGMATCH="T4 - Big HDD Array"

And here is the script itself below (save it as *.bat or *.cmd):

Code: Select all

setlocal
@echo off
del %TEMPFILE% >nul
cls
color e
TITLE qBittorrent startup external hard disk monitor

  echo.
  echo   [qBittorrent startup external hard disk monitor]
  echo.

SET DISKDRIVE=Z:\
SET TEMPFILE=R:\DiskCheckTempFile.txt
SET STRINGMATCH="A meghajtóban (Z) lév? kötet T4 - Big HDD Array"

:HOME
  echo   This will check if disk %DISKDRIVE% is online before starting qBittorrent.
  echo   This may prevent re-checking all of the items in qBitorrent.
rem timeout 1
  echo.
  echo      Target drive: %DISKDRIVE%
  echo      Stringmatch: %STRINGMATCH%
  echo      Log file: %TEMPFILE%
:RECHECK
cls
  echo.
  echo   Checking the availability of %DISKDRIVE%..
  echo.
dir %DISKDRIVE% >nul >>%TEMPFILE%
findstr /i %STRINGMATCH% %TEMPFILE%

IF %ERRORLEVEL% EQU 0 (
:GREEN
  color a

  echo.
  echo   The %DISKDRIVE% host seems online, proceeding..
  GOTO PROCEED
) else (

:RED
  color c

  echo.
  echo   The %DISKDRIVE% does not seems to be available.
  echo   Have you turned on the external hard disk?
  echo.
  echo   Press any key if it's ready to use and we can proceed.
  echo.
  echo      Opening My Computer..
  explorer.exe /e,::{20D04FE0-3AEA-1069-A2D8-08002B30309D}
  pause>nul
rem  echo.
rem  echo      Looping..
rem  echo.
timeout 3
GOTO RECHECK
)
del %TEMPFILE% >nul
cls

:PROCEED
@echo off
color a
del %TEMPFILE%>nul
cls

  echo.
  echo.
  echo      Starting qBitorrent..
  echo.
  timeout 3

  rem This below method resolves the stuck console window after starting qBitorrent  
  start "" "C:\Program Files\qBittorrent\qbittorrent.exe"

rem pause
exit

-xelemorf