Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for mergepdfs.bat

(view source code of mergepdfs.bat as plain text)

  1. @ECHO OFF
  2. SETLOCAL ENABLEDELAYEDEXPANSION
  3. :: At least 3 files must be specified: 2 to merge, 1 target file
  4. IF "%~3"=="" GOTO Syntax
  5. :: Check if GhostScript is available
  6. SET GS=""
  7. gswin32c.exe -? >NUL 2>&1 && SET GS="gswin32c.exe"
  8. gswin64c.exe -? >NUL 2>&1 && SET GS="gswin64c.exe"
  9. IF %GS%=="" (
  10. 	FOR /F "tokens=8" %%A IN ('REG.EXE Query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" /f GhostScript ^| FIND.EXE "HKEY_"') DO (
  11. 		FOR /F "tokens=3" %%B IN ('REG.EXE Query "%%~A" /v *UninstallString') DO (
  12. 			FOR /F "tokens=*" %%C IN ('DIR "%%~B.\..\gswin*c.exe" /B /S') DO (
  13. 				SET GS="%%~C"
  14. 			)
  15. 		)
  16. 	)
  17. )
  18. IF %GS%=="" (
  19. 	ECHO.
  20. 	ECHO ←[0;1;31mERROR:←[0m GhostScript not found
  21. 	GOTO Syntax
  22. )
  23.  
  24. :: Count the NUMBER of input files
  25. SET Count=1
  26. :: List of input files
  27. SET InputFile.=
  28. :: List of page lists for each input file
  29. SET InputFilter.=
  30. :: All except the last argument are considered to be PDF input files or page lists
  31. :Loop
  32. IF /I "%~x1"==".pdf" (
  33. 	IF "%~2"=="" (
  34. 		SET OutputFile="%~1"
  35. 		SET /A Count -= 1
  36. 	) ELSE (
  37. 		SET InputFile.!Count!="%~1"
  38. 		SET /A Count += 1
  39. 		SHIFT
  40. 		GOTO Loop
  41. 	)
  42. ) ELSE (
  43. 	IF DEFINED InputFilter.!Count! (
  44. 		ECHO.
  45. 		ECHO ←[0;1;31mERROR:←[0m No more than one page list per input file allowed
  46. 		GOTO Syntax
  47. 	)
  48. 	SET InputFilter.!Count!=-sPageList=%1
  49. 	SHIFT
  50. 	GOTO Loop
  51. )
  52. :: Empty pagelists should be replaced by "1-" to prevent reusing pagelist of previous input PDF
  53. FOR /L %%A IN (1,1,%Count%) DO (
  54. 	IF NOT DEFINED InputFilter.%%A (
  55. 		SET InputFilter.%%A=-sPageList=1-
  56. 	)
  57. )
  58. SET Input=
  59. FOR /L %%A IN (1,1,%Count%) DO (
  60. 	SET Input=!Input! !InputFilter.%%A! !InputFile.%%A!
  61. )
  62. :: ECHO %GS% -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=%OutputFile% -dBATCH %Input%
  63. %GS% -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=%OutputFile% -dBATCH %Input%
  64.  
  65. ENDLOCAL
  66. GOTO:EOF
  67.  
  68.  
  69. :Syntax
  70. ECHO.
  71. ECHO MergePDFs.bat, Version 3.01
  72. ECHO Merge multiple PDF files into a single new PDF file
  73. ECHO.
  74. ECHO Usage:    ←[0;1mMergePDFs [pg1] in1.pdf [pg2] in2.pdf [[pg3] in3.pdf [...]] out.pdf←[0m
  75. ECHO.
  76. ECHO Where:    ←[0;1min*.pdf←[0m   a PDF file to be merged
  77. ECHO           ←[0;1mpg*←[0m       list of pages of the PDF file with same number
  78. ECHO                     ^(see notes; default: all pages^)
  79. ECHO           ←[0;1mout.pdf←[0m   the target PDF file
  80. ECHO.
  81. ECHO Example:  MergePDFs.bat  "1,3-5" file1.pdf file2.pdf 12- file3.pdf output.pdf
  82. ECHO           output.pdf will contain pages 1, 3, 4 and 5 of file1.pdf,
  83. ECHO           all pages of file2.pdf, and page 12 and onwards of file3.pdf.
  84. ECHO.
  85. ECHO Notes:    PDFs will be merged in the specified order.
  86. ECHO           There are 3 possible values for ←[0;1mpg*←[0m: "even", "odd" or a list of
  87. ECHO           pages to be processed; a list can include single pages or ranges of
  88. ECHO           pages; ranges of pages use the minus sign "-", individual pages and
  89. ECHO           ranges of pages are separated by commas ","; a trailing minus "-"
  90. ECHO           means process all remaining pages; doublequote the list if comma's
  91. ECHO           are used.
  92. ECHO           This batch file requires  GhostScript, available at
  93. ECHO           ←[0;1;30mhttps://www.ghostscript.com/←[0m
  94. ECHO           Existing target files will be overwritten, no questions asked.
  95. ECHO           GhostScript command by Breno Polanski
  96. ECHO           ←[0;1;30mhttps://gist.github.com/brenopolanski/2ae13095ed7e865b60f5←[0m
  97. ECHO           Return code ^("ErrorLevel"^) 1 in case of errors
  98. ECHO           or if Ghostscript wasn't found, otherwise 0.
  99. ECHO.
  100. ECHO Written by Rob van der Woude
  101. ECHO https://www.robvanderwoude.com
  102.  
  103. ENDLOCAL
  104. EXIT /B 1
  105.  

page last modified: 2024-04-16; loaded in 0.0082 seconds