Skip to content

Instantly share code, notes, and snippets.

@urbans0ft
Last active January 4, 2023 08:49
Show Gist options
  • Select an option

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

Select an option

Save urbans0ft/e1b29b806da806b261bfef68b8caaa06 to your computer and use it in GitHub Desktop.
Batch file to facilitate the splitting of pdf documents using qpdf
@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 The app 'qpdf' could not be found.
echo "https://github.com/qpdf/qpdf/releases/latest"
goto end
)
if not exist %SPLIT_FOLDER% (
echo The merge folder '%SPLIT_FOLDER%' does not exist.
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 There's no file to to split up.
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