Skip to content

Instantly share code, notes, and snippets.

@urbans0ft
Last active December 14, 2022 19:44
Show Gist options
  • Select an option

  • Save urbans0ft/5b14d5ed4f7d68e1947d26036ba78898 to your computer and use it in GitHub Desktop.

Select an option

Save urbans0ft/5b14d5ed4f7d68e1947d26036ba78898 to your computer and use it in GitHub Desktop.
Batch file to facilitate the merging of pdf documents using qpdf
@echo off
setlocal EnableDelayedExpansion
:: Constants
set "MERGE_FOLDER=C:\qpdf\merge"
set "OUT_DIR=%MERGE_FOLDER%\out"
for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined localdatetime set localdatetime=%%x
set "OUT_FILE=%OUT_DIR%\%localdatetime:~0,14%.pdf"
echo.
:: Prerequisites
where /q qpdf
if %ERRORLEVEL% neq 0 (
echo The app 'qpdf' could not be found.
echo "https://github.com/qpdf/qpdf/releases/latest"
goto end
)
if not exist %MERGE_FOLDER% (
echo The merge folder '%MERGE_FOLDER%' does not exist.
goto end
)
:: create output directory if necessary
if not exist %OUT_DIR% (
mkdir %OUT_DIR% 2>&1 >nul
)
pushd %MERGE_FOLDER%
echo Merging...
set /a "fileCount=0"
for %%i IN (*.pdf) do (
if not "%%i" == "%OUT_FILE%" (
echo %%i
set "files=!files! ^"%%i^""
set /a "fileCount+=1"
)
)
if 2 gtr !fileCount! (
echo There must be at least 2 files to perform a merge.
goto end
)
set "files=!files:~1!"
qpdf --empty --pages !files! -- %OUT_FILE%
echo Merged !fileCount! files to %OUT_FILE%.
popd
:end
echo.
pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment