Skip to content

Instantly share code, notes, and snippets.

@ScottJWalter
Last active February 1, 2024 16:22
Show Gist options
  • Save ScottJWalter/2d7082f0c2f09f10a3ac7185996c9979 to your computer and use it in GitHub Desktop.
Save ScottJWalter/2d7082f0c2f09f10a3ac7185996c9979 to your computer and use it in GitHub Desktop.
Batch file to bulk import a folder of files into Calibre with more extensive logging. #calibre #windows
@echo off
setlocal enabledelayedexpansion
rem The following variables set up the environment
set "work_drive=D:"
set "dir_in=D:\<folder-to-process>"
set "dir_out=D:\<processed-folder>"
set "log_file=_import.log"
rem ------------------------------ NO CHANGES NEEDED BELOW ------------------------------
rem Tags can be passed as parameters, quoted if there are embedded spaces.
rem
rem This chunk walks the parameter list, and converts any existing parameters
rem into a string: '-T "%1" -T "%2" ...'
et argCount=0
for %%x in (%*) do (
set /A argCount+=1
set "argVec[!argCount!]=%%~x"
)
if "%argCount%"=="0" goto NO_TAGS
for /L %%i in (1,1,%argCount%) do set tags=!tags! -T "!argVec[%%i]!"
:NO_TAGS
rem TODO: Need to check if there's already an instance of Calibre running, in which
rem case, don't try to process (wait for next cycle).
rem I use a crude way of tagging lines in the output log by prefixing them with 2 askterisks
rem and a third character to differentiate the different lines I'm interested in scanning
rem for later. The legend is simple:
rem
rem **+ Marks the start of a batch job
rem *** Marks the start of calibredb processing
rem **- Marks the end of a batch job
echo. >>%dir_out%\%log_file% 2>&1
echo ============================================================ >>%dir_out%\%log_file% 2>&1
echo **+ %DATE% %TIME% - BEGIN ... >>%dir_out%\%log_file% 2>&1
echo **+ Tags: %tags% >>%dir_out%\%log_file% 2>&1
echo ============================================================ >>%dir_out%\%log_file% 2>&1
rem calibredb doesn't provide alot of logging detail during a process, and during
rem a batch process, it's even worse. So, instead of giving calibredb a folder
rem to process, we spoon feed it one file at a time, so we can get distinct
rem import data that we can link to each import.
%work_drive%
cd %dir_in%
for %%i in (*) do (
echo *** Processing "%%i" ... >>%dir_out%\%log_file% 2>&1
calibredb add %tags% --automerge="ignore" "%%i" >>%dir_out%\%log_file% 2>&1
rem once done (one way or another), move the file out of inbound
move "%%i" %dir_out%\ >>%dir_out%\%log_file% 2>&1
)
echo. >>%dir_out%\%log_file% 2>&1
echo ============================================================ >>%dir_out%\%log_file% 2>&1
echo **- %DATE% %TIME% - END >>%dir_out%\%log_file% 2>&1
echo ============================================================ >>%dir_out%\%log_file% 2>&1
@ScottJWalter
Copy link
Author

A little tweak to enable tagging. Any parameters passed will be turned into calibre tags. Quote parameters if there are embedded spaces (and probably a good idea if a potential tag has hyphens as well).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment