Last active
December 14, 2022 19:44
-
-
Save urbans0ft/5b14d5ed4f7d68e1947d26036ba78898 to your computer and use it in GitHub Desktop.
Batch file to facilitate the merging of pdf documents using qpdf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @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 [91mThe app 'qpdf' could not be found.[0m | |
| echo "https://github.com/qpdf/qpdf/releases/latest" | |
| goto end | |
| ) | |
| if not exist %MERGE_FOLDER% ( | |
| echo [91mThe merge folder '%MERGE_FOLDER%' does not exist.[0m | |
| 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 [91mThere must be at least 2 files to perform a merge.[0m | |
| 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