(view source code of mergepdfs.bat as plain text)
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
:: At least 3 files must be specified: 2 to merge, 1 target file
IF "%~3"=="" GOTO Syntax
:: Check if GhostScript is available
SET GS=""
gswin32c.exe -? >NUL 2>&1 && SET GS="gswin32c.exe"
gswin64c.exe -? >NUL 2>&1 && SET GS="gswin64c.exe"
IF %GS%=="" (
FOR /F "tokens=8" %%A IN ('REG.EXE Query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" /f GhostScript ^| FIND.EXE "HKEY_"') DO (
FOR /F "tokens=3" %%B IN ('REG.EXE Query "%%~A" /v *UninstallString') DO (
FOR /F "tokens=*" %%C IN ('DIR "%%~B.\..\gswin*c.exe" /B /S') DO (
SET GS="%%~C"
)
)
)
)
IF %GS%=="" (
ECHO.
ECHO ←[0;1;31mERROR:←[0m GhostScript not found
GOTO Syntax
)
:: Count the NUMBER of input files
SET Count=1
:: List of input files
SET InputFile.=
:: List of page lists for each input file
SET InputFilter.=
:: All except the last argument are considered to be PDF input files or page lists
:Loop
IF /I "%~x1"==".pdf" (
IF "%~2"=="" (
SET OutputFile="%~1"
SET /A Count -= 1
) ELSE (
SET InputFile.!Count!="%~1"
SET /A Count += 1
SHIFT
GOTO Loop
)
) ELSE (
IF DEFINED InputFilter.!Count! (
ECHO.
ECHO ←[0;1;31mERROR:←[0m No more than one page list per input file allowed
GOTO Syntax
)
SET InputFilter.!Count!=-sPageList=%1
SHIFT
GOTO Loop
)
:: Empty pagelists should be replaced by "1-" to prevent reusing pagelist of previous input PDF
FOR /L %%A IN (1,1,%Count%) DO (
IF NOT DEFINED InputFilter.%%A (
SET InputFilter.%%A=-sPageList=1-
)
)
SET Input=
FOR /L %%A IN (1,1,%Count%) DO (
SET Input=!Input! !InputFilter.%%A! !InputFile.%%A!
)
:: ECHO %GS% -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=%OutputFile% -dBATCH %Input%
%GS% -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=%OutputFile% -dBATCH %Input%
ENDLOCAL
GOTO:EOF
:Syntax
ECHO.
ECHO MergePDFs.bat, Version 3.01
ECHO Merge multiple PDF files into a single new PDF file
ECHO.
ECHO Usage: ←[0;1mMergePDFs [pg1] in1.pdf [pg2] in2.pdf [[pg3] in3.pdf [...]] out.pdf←[0m
ECHO.
ECHO Where: ←[0;1min*.pdf←[0m a PDF file to be merged
ECHO ←[0;1mpg*←[0m list of pages of the PDF file with same number
ECHO ^(see notes; default: all pages^)
ECHO ←[0;1mout.pdf←[0m the target PDF file
ECHO.
ECHO Example: MergePDFs.bat "1,3-5" file1.pdf file2.pdf 12- file3.pdf output.pdf
ECHO output.pdf will contain pages 1, 3, 4 and 5 of file1.pdf,
ECHO all pages of file2.pdf, and page 12 and onwards of file3.pdf.
ECHO.
ECHO Notes: PDFs will be merged in the specified order.
ECHO There are 3 possible values for ←[0;1mpg*←[0m: "even", "odd" or a list of
ECHO pages to be processed; a list can include single pages or ranges of
ECHO pages; ranges of pages use the minus sign "-", individual pages and
ECHO ranges of pages are separated by commas ","; a trailing minus "-"
ECHO means process all remaining pages; doublequote the list if comma's
ECHO are used.
ECHO This batch file requires GhostScript, available at
ECHO ←[0;1;30mhttps://www.ghostscript.com/←[0m
ECHO Existing target files will be overwritten, no questions asked.
ECHO GhostScript command by Breno Polanski
ECHO ←[0;1;30mhttps://gist.github.com/brenopolanski/2ae13095ed7e865b60f5←[0m
ECHO Return code ^("ErrorLevel"^) 1 in case of errors
ECHO or if Ghostscript wasn't found, otherwise 0.
ECHO.
ECHO Written by Rob van der Woude
ECHO https://www.robvanderwoude.com
ENDLOCAL
EXIT /B 1
page last modified: 2024-04-16; loaded in 0.0082 seconds