I wanted a batch script that would allow me to update and clean more than one Kodi box at the same time. I also wanted a quick and easy method to see which of my Kodi boxes were showing up on the network and if they were updated. I manage my files manually in Windows (as a matter of preference) and have more than one MySQL database. Up till now my process of adding content ended with bringing up multiple web interfaces and using those kick off a library update. Now I can just open a single batch file that is already setup to perform the tasks I want. I designed the script to be very flexible and easy to configure.
I’m not sure how many people other than myself are in need of such a thing but if you have more than one Kodi box and manage your files in Windows, this script may save you some time and effort. To use it you only need to copy the text in full and paste it into a text file that is named as <something>.bat
(If your hiding file extensions you may need to unhide those long enough to change the file extension). You will need to edit some information in the beginning of the file to add in the IP addresses of your Kodi boxes, which you want to do library updates on, as well as how many total clients and if you want the clean library dialog box to show.
@ECHO off
SETLOCAL enabledelayedexpansion
:: Created by DarwinDesign
:: The "num.clients=<x>" value should be set at the largest "kodi.<x>.ip=" number your using
:: The "clean.dialogs" set whether a library clean displays the progress dialog box in the GUI. This can only be "true" or "false".
:: Any unused kodi.<x>.ip= varibles must have a hard return after "=" or just comment out or remove the line.
:: If something doesn't look right check to make sure there is not a trailing space.
:: A setting of "yes" for the update variables is the only trigger. If not setting it to yes you don't need that line.
::
SET num.clients=5
SET clean.dialogs=false
::
:: John RPi
SET kodi.1.ip=http://osmc:osmc@192.168.254.224:80
SET Kodi.1.video.update=yes
SET Kodi.1.audio.update=yes
:: Cave Vero
SET kodi.2.ip=http://osmc:osmc@192.168.254.190:80
SET Kodi.2.video.update=no
SET Kodi.2.audio.update=no
:: Living Room
SET kodi.3.ip=http://osmc:osmc@192.168.254.221:80
SET Kodi.3.video.update=yes
SET Kodi.3.audio.update=no
:: Donna
SET kodi.4.ip=http://osmc:osmc@192.168.254.226:80
:: June
SET kodi.5.ip=http://osmc:osmc@192.168.254.222:80
::
:: All "SET kodi.<x>.ip=" linues need to be above this line.
::
:: Sets variables for the start of the json requests.
FOR /L %%I IN (1,1,%num.clients%) DO (
IF NOT [!kodi.%%I.ip!] == [] (
SET "kodi.%%I.curl=curl -s --connect-timeout 2 -X POST -H "content-type:application/json" !kodi.%%I.ip!/jsonrpc -d"
)
)
:: Sets client names. Empty ip's are set to "null" then ask Kodi for names and bad responces set ip address as the name.
FOR /L %%I IN (1,1,%num.clients%) DO (
IF NOT [!kodi.%%I.ip!] == [] (
SET "k=null"
FOR /f "tokens=* usebackq" %%I IN (`!kodi.%%I.curl! "{""jsonrpc"":""2.0"",""method"":""Settings.GetSettingValue"",""params"":{""setting"":""services.devicename""},""id"":1}"`) DO SET "k=%%I"
IF "!k!" == "null" (
FOR /F "tokens=4 delims=@:" %%c IN ("!kodi.%%I.ip!") DO SET "kodi.%%I.name.pad=%%c .............."
) ELSE (
FOR /F "delims=" %%x IN ('echo !k!') DO (
SET V=%%x
CALL SET V=%%V:"=µ%%
FOR /F "tokens=7 delims=:,{µ" %%a IN ('CALL ECHO.%%V%%') DO SET "kodi.%%I.name.pad=%%a .............."
)
)
) ELSE (
SET "kodi.%%I.name.pad=null"
)
)
SET "k="
:MENU
CLS
ECHO _______________________________________________________
ECHO _____ Kodi Utilities _____
ECHO.
ECHO 1 - Update Video Libraries
ECHO 2 - Update Music Libraries
ECHO 3 - Client Information
ECHO V - Clean Video Libraries
ECHO M - Clean Music Libraries
ECHO 9 - EXIT
ECHO _______________________________________________________
CHOICE /N /C:123456VM9 /M " _____ Make A Selection _____"
IF "%errorlevel%"=="1" GOTO UPDATEVIDEO
IF "%errorlevel%"=="2" GOTO UPDATEMUSIC
IF "%errorlevel%"=="3" GOTO VERSION
IF "%errorlevel%"=="4" GOTO MENU
IF "%errorlevel%"=="5" GOTO MENU
IF "%errorlevel%"=="6" GOTO MENU
IF "%errorlevel%"=="7" GOTO CLEANVIDEO
IF "%errorlevel%"=="8" GOTO CLEANMUSIC
IF "%errorlevel%"=="9" GOTO EOF
GOTO MENU
:UPDATEVIDEO
SET title=Update Video Libraries
SET json="{""jsonrpc"":""2.0"",""id"":1,""method"":""VideoLibrary.Scan""}"
SET json.responce=OK
GOTO VLIBRARYSTART
:UPDATEMUSIC
SET title=Update Music Libraries
SET json="{""jsonrpc"":""2.0"",""id"":1,""method"":""AudioLibrary.Scan""}"
SET json.responce=OK
GOTO ALIBRARYSTART
:CLEANVIDEO
SET title=Clean Video Libraries
SET json="{""jsonrpc"":""2.0"",""id"":1,""method"":""XBMC.GetInfoBooleans"",""params"":{""booleans"":[""library.isscanning""]}}"
SET json.responce=true
FOR /L %%I IN (1,1,%num.clients%) DO (
IF "!Kodi.%%I.video.update!"=="yes" IF NOT "!kodi.%%I.name.pad%!" == "null" (
!kodi.%%I.curl! "{""jsonrpc"":""2.0"",""id"":1,""method"":""VideoLibrary.Clean"",""params"":{""showdialogs"":%clean.dialogs%}}" --max-time 0.1 >nul
)
)
GOTO VLIBRARYSTART
:CLEANMUSIC
SET title=Clean Music Libraries
SET json="{""jsonrpc"":""2.0"",""id"":1,""method"":""XBMC.GetInfoBooleans"",""params"":{""booleans"":[""library.isscanning""]}}"
SET json.responce=true
FOR /L %%I IN (1,1,%num.clients%) DO (
IF "!Kodi.%%I.audio.update!"=="yes" IF NOT "!kodi.%%I.name.pad%!" == "null" (
!kodi.%%I.curl! "{""jsonrpc"":""2.0"",""id"":1,""method"":""AudioLibrary.Clean"",""params"":{""showdialogs"":%clean.dialogs%}}" --max-time 0.1 >nul
)
)
GOTO ALIBRARYSTART
:VLIBRARYSTART
CLS
ECHO _______________________________________________________
ECHO _____ %title% _____
ECHO.
FOR /L %%I IN (1,1,%num.clients%) DO (
IF "!Kodi.%%I.video.update!"=="yes" (
SET "k="
FOR /f "tokens=* usebackq" %%J IN (`!kodi.%%I.curl! %json%`) DO SET k=%%J
ECHO !k! | findstr /c:"%json.responce%" >nul
IF !errorlevel!==1 (
SET status.%%I=error
SET finished.%%I=1
ECHO !kodi.%%I.name.pad:~,15! ^(No response^)
) ELSE (
SET status.%%I=running
SET finished.%%I=0
ECHO !kodi.%%I.name.pad:~,15! ^(started^)
)
) ELSE (
SET finished.%%I=1
)
)
ECHO _______________________________________________________
TIMEOUT /t 5 >nul
GOTO UPDATESTATUS
:ALIBRARYSTART
CLS
ECHO _______________________________________________________
ECHO _____ %title% _____
ECHO.
FOR /L %%I IN (1,1,%num.clients%) DO (
IF "!Kodi.%%I.audio.update!"=="yes" (
SET "k="
FOR /f "tokens=* usebackq" %%J IN (`!kodi.%%I.curl! %json%`) DO SET k=%%J
ECHO !k! | findstr /c:"%json.responce%" >nul
IF !errorlevel!==1 (
SET status.%%I=error
SET finished.%%I=1
ECHO !kodi.%%I.name.pad:~,15! ^(No response^)
) ELSE (
SET status.%%I=running
SET finished.%%I=0
ECHO !kodi.%%I.name.pad:~,15! ^(started^)
)
) ELSE (
SET finished.%%I=1
)
)
ECHO _______________________________________________________
TIMEOUT /t 5 >nul
GOTO UPDATESTATUS
:UPDATESTATUS
CLS
ECHO _______________________________________________________
ECHO _____ %title% _____
ECHO.
FOR /L %%I IN (1,1,%num.clients%) DO (
IF "!status.%%I!"=="running" (
SET "k="
FOR /f "tokens=* usebackq" %%J IN (`!kodi.%%I.curl! "{""jsonrpc"":""2.0"",""id"":1,""method"":""XBMC.GetInfoBooleans"",""params"":{""booleans"":[""library.isscanning""]}}"`) DO SET k=%%J
ECHO !k! | findstr /c:"true" >nul
IF !errorlevel!==0 (
ECHO !kodi.%%I.name.pad:~,15! ^(!status.%%I!^)
) ELSE (
SET finished.%%I=1
SET status.%%I=complete
ECHO !kodi.%%I.name.pad:~,15! ^(!status.%%I!^)
)
)
)
SET "finished=0"
FOR /L %%I IN (1,1,%num.clients%) DO SET /a "finished=!finished.%%I! +!finished!"
IF "!finished!"=="%num.clients%" (GOTO UPDATEDONE)
ECHO _______________________________________________________
ECHO _____ Press X to stop checking status _____
CHOICE /c xr /n /t 5 /d r
if %errorlevel%==1 GOTO CANCELED
if %errorlevel%==2 GOTO UPDATESTATUS
:CANCELED
FOR /L %%I IN (1,1,%num.clients%) DO (
IF "!status.%%I!"=="running" (
SET status.%%I=cancel
)
)
GOTO UPDATEDONE
:UPDATEDONE
CLS
ECHO _______________________________________________________
ECHO _____ %title% _____
ECHO.
FOR /L %%I IN (1,1,%num.clients%) DO (
IF "!status.%%I!"=="complete" (ECHO !kodi.%%I.name.pad:~,18! ^(succeeded^))
IF "!status.%%I!"=="cancel" (ECHO !kodi.%%I.name.pad:~,18! ^(Schrodingers cat^))
IF "!status.%%I!"=="error" (ECHO !kodi.%%I.name.pad:~,18! ^(failed^))
SET "finished="
SET "finished.%%I="
SET "status.%%I="
)
ECHO _______________________________________________________
ECHO _____ Press the any key to continue _____
PAUSE >nul
GOTO MENU
:VERSION
CLS
ECHO _______________________________________________________
ECHO _____ Kodi Version Info _____
ECHO.
FOR /L %%I IN (1,1,%num.clients%) DO (
IF NOT "!kodi.%%I.name.pad%!" == "null" (
SETLOCAL enabledelayedexpansion
IF "!kodi.%%I.video.update%!" == "yes" (SET "u=*") ELSE (SET "u= ")
IF "!kodi.%%I.audio.update%!" == "yes" (SET "t=*") ELSE (SET "t= ")
FOR /f "tokens=* usebackq" %%J IN (`!kodi.%%I.curl! "{""jsonrpc"":""2.0"",""method"":""Application.GetProperties"",""params"":{""properties"":[""version""]},""id"":1}"`) DO SET k=%%J
ECHO !k! | findstr /c:"version" >nul
IF !errorlevel!==1 (ECHO !u!!t! !kodi.%%I.name.pad:~,15! ^(No response^)
) ELSE (
FOR /F "delims=" %%x IN ('echo !k!') DO (
SET V=%%x
CALL SET V=%%V:"=µ%%
FOR /F "tokens=8,10,12,14 delims=:,{µ" %%a IN ('CALL ECHO.%%V%%') DO (
ECHO !u!!t! !kodi.%%I.name.pad:~,15! Kodi %%a.%%b Revision^:%%c ^(%%d^)
)
)
)
ENDLOCAL
)
)
ECHO _______________________________________________________
ECHO _____ Press the any key to continue _____
PAUSE >nul
GOTO MENU
:EOF