Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for jpgs2pdf.bat

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

  1. @ECHO OFF
  2. SETLOCAL ENABLEDELAYEDEXPANSION
  3. :: Check Command Line
  4. IF "%~1"=="" GOTO Syntax
  5. SET PDFFile=""
  6. SET GSCommand=
  7. FOR %%A IN (%*) DO (
  8. 	IF /I "%%~xA"==".jpg" (
  9. 		IF NOT EXIST "%%~A" GOTO InvalidJPG
  10. 		REM Make sure to append a trailing space after showpage
  11. 		SET GSCommand=!GSCommand! ^(%%~A^) viewJPEG showpage 
  12. 	) ELSE IF /I "%%~xA"==".jpeg" (
  13. 		IF NOT EXIST "%%~A" GOTO InvalidJPG
  14. 		REM Make sure to append a trailing space after showpage
  15. 		SET GSCommand=!GSCommand! ^(%%~A^) viewJPEG showpage 
  16. 	) ELSE IF /I "%%~xA"==".pdf" (
  17. 		IF !PDFFile!=="" (
  18. 			SET PDFFile="%%~A"
  19. 		) ELSE (
  20. 			GOTO OnePDFOnly
  21. 		)
  22. 	) ELSE (
  23. 		GOTO Syntax
  24. 	)
  25. )
  26. IF %PDFFile%=="" SET PDFFile="%~dpn1.pdf"
  27. IF EXIST %PDFFile% GOTO FileExists
  28.  
  29.  
  30. :: Find GhostScript executable
  31. SET ProgDir=
  32. SET ProgFile=
  33. FOR /F "tokens=*" %%A IN ('REG.EXE Query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall /f Ghostscript ^| FIND.EXE /I "HKEY_"') DO (
  34. 	FOR /F "tokens=2*" %%B IN ('REG.EXE Query "%%~A" /v UninstallString') DO (
  35. 		SET ProgDir=%%~dpC
  36. 	)
  37. )
  38. IF "%ProgDir%"=="" GOTO NoGhostScript
  39. FOR /F "tokens=*" %%A IN ('DIR /B /S "%ProgDir%gswin*c.exe"') DO SET ProgFile=%%~fA
  40. IF "%ProgFile%"=="" GOTO NoGhostScript
  41.  
  42. :: Run GhostScript
  43. "%ProgFile%" -dNOSAFER -sPAPERSIZE=a4 -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -o %PDFFile% "%ProgDir%lib\viewjpeg.ps" -c "%GSCommand%"
  44.  
  45. ENDLOCAL
  46. GOTO:EOF
  47.  
  48.  
  49. :InvalidJPG
  50. ECHO.
  51. ECHO ←[0;1;31mERROR:←[0m  Please specify valid JPG files
  52. GOTO Syntax
  53.  
  54.  
  55. :OnePDFOnly
  56. ECHO.
  57. ECHO ←[0;1;31mERROR:←[0m  Please specify no more than 1 PDF file
  58. GOTO Syntax
  59.  
  60.  
  61. :FileExists
  62. ECHO.
  63. ECHO ←[0;1;31mERROR:←[0m  The output file already exists.
  64. ECHO         Move or rename %PDFFile% and try again.
  65. GOTO Syntax
  66.  
  67.  
  68. :NoGhostScript
  69. ECHO.
  70. ECHO ←[0;1;31mERROR:←[0m  GhostScript not found
  71.  
  72.  
  73. :Syntax
  74. ECHO.
  75. ECHO JPGs2PDF.bat,  Version 1.00 for Windows 10
  76. ECHO Use GhostScript to convert/merge JPG image(s) into a single PDF file
  77. ECHO.
  78. ECHO Usage:  ←[0;1m%~nx0  "jpegfile"  [ "jpegfile"  [ .. ] ]  [ "pdffile" ]←[0m
  79. ECHO.
  80. ECHO Where:  ←[0;1mjpegfile←[0m    JPG file(s) to be converted/merged into a single PDF file
  81. ECHO         ←[0;1mpdffile←[0m     output PDF file (default: name and location of first JPG,
  82. ECHO                               but with extension .pdf)
  83. ECHO.
  84. ECHO Notes:  This batch file requires GhostScript to convert JPG to PDF.
  85. ECHO         GhostScript can be found at ←[0;1;30mhttps://www.ghostscript.com/←[0m
  86. ECHO.
  87. ECHO Written by Rob van der Woude
  88. ECHO https://www.robvanderwoude.com
  89.  
  90. ENDLOCAL
  91. EXIT /B 1
  92.  

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