Last active
July 21, 2024 10:57
-
-
Save k-barton/f8314031054e868b92feb6dca7317a0d to your computer and use it in GitHub Desktop.
Windows batch script to combine mutiple pdfs into one using Ghostscript
This file contains 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 | |
set result= | |
set outfile= | |
set nextisoutfile= | |
set "self=%~n0" | |
:startHelp | |
if "%1" EQU "/?" goto :showHelp | |
if "%%j" EQU "--help" goto :showHelp | |
if "%1" EQU "" goto :endHelp | |
shift | |
goto :startHelp | |
:endHelp | |
for %%j in (%*) do ( | |
set "x=%%j" | |
if "!nextisoutfile!" NEQ "" ( | |
set "outfile=!x!" | |
set nextisoutfile= | |
) else if "!x:~0,2!"=="-o" ( | |
set nextisoutfile=true | |
) else if "!x:~0,9!"=="--outfile" ( | |
set nextisoutfile=true | |
) else ( | |
set "infile=%%~dpnxj" | |
if not exist "!infile!" ( | |
echo Warning: file "!infile!" does not exist. | |
) else ( | |
echo ^+ %%~nxj | |
set "result=!result! ^"!infile!^"" | |
) | |
set nextisoutfile= | |
) | |
) | |
if "!outfile!" EQU "" ( | |
echo Error: no output file given. | |
) else if exist "!outfile!" ( | |
echo Error: file "!outfile!" already exists. | |
) else ( | |
gswin64c -dNOPAUSE -sDEVICE=pdfwrite -dOptimize=true -dAutoRotatePages=/None -sOUTPUTFILE=!outfile! -dBATCH !result! | |
) | |
goto :end | |
:showHelp | |
echo. | |
echo. Combines two or more pdf files into one. | |
echo. Usage: | |
echo. %self% input_file [^+ ...] [--output= ^| -o] output_file | |
echo. | |
:end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
command line: