Last active
January 4, 2023 08:49
-
-
Save urbans0ft/e1b29b806da806b261bfef68b8caaa06 to your computer and use it in GitHub Desktop.
Batch file to facilitate the splitting 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 "SPLIT_FOLDER=C:\qpdf\split" | |
| set "OUT_DIR=%SPLIT_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 %SPLIT_FOLDER% ( | |
| echo [91mThe merge folder '%SPLIT_FOLDER%' does not exist.[0m | |
| goto end | |
| ) | |
| :: create output directory if necessary | |
| if not exist %OUT_DIR% ( | |
| mkdir %OUT_DIR% >nul 2>&1 | |
| ) | |
| pushd %SPLIT_FOLDER% | |
| echo Splitting... | |
| set /a "fileCount=0" | |
| for %%i IN (*.pdf) do ( | |
| echo %%i | |
| qpdf --split-pages "%%i" "%OUT_DIR%\%%i_%%d.pdf" | |
| set "files=!files! ^"%%i^"" | |
| set /a "fileCount+=1" | |
| ) | |
| if 0 equ !fileCount! ( | |
| echo [91mThere's no file to to split up.[0m | |
| goto end | |
| ) | |
| set "files=!files:~1!" | |
| echo Split !fileCount! file(s) to %OUT_DIR%. | |
| popd | |
| :end | |
| echo. | |
| pause |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment