Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for pdf2txt.bat

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

  1. @ECHO OFF
  2. SETLOCAL
  3. :: Check Command Line
  4. IF "%~1"=="" GOTO Syntax
  5. IF NOT EXIST "%~1" GOTO InvalidPDF
  6. IF /I NOT "%~x1"==".pdf" GOTO InvalidPDF
  7. IF "%~2"=="" (
  8. 	SET TextFile="%~dpn1.txt"
  9. ) ELSE (
  10. 	SET TextFile="%~2"
  11. 	IF NOT EXIST "%~dp2*.*" (
  12. 		ECHO.
  13. 		ECHO ←[0;1;31mERROR:←[0m  Directory "%~dp2" not found.
  14. 		GOTO Syntax
  15. 	)
  16. )
  17. IF EXIST %TextFile% GOTO FileExists
  18. IF NOT "%~3"=="" GOTO Syntax
  19.  
  20. :: Find GhostScript executable
  21. SET ProgDir=
  22. SET ProgFile=
  23. FOR /F "tokens=*" %%A IN ('REG.EXE Query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall /f Ghostscript ^| FIND.EXE /I "HKEY_"') DO (
  24. 	FOR /F "tokens=2*" %%B IN ('REG.EXE Query "%%~A" /v UninstallString') DO (
  25. 		SET ProgDir=%%~dpC
  26. 	)
  27. )
  28. IF "%ProgDir%"=="" GOTO NoGhostScript
  29. FOR /F "tokens=*" %%A IN ('DIR /B /S "%ProgDir%gswin*c.exe"') DO SET ProgFile=%%~fA
  30. IF "%ProgFile%"=="" GOTO NoGhostScript
  31.  
  32. :: Run GhostScript
  33. "%ProgFile%" -sDEVICE=txtwrite -o %TextFile% "%~f1"
  34. ENDLOCAL
  35. GOTO:EOF
  36.  
  37.  
  38. :InvalidPDF
  39. ECHO.
  40. ECHO ←[0;1;31mERROR:←[0m  Please specify a valid PDF file
  41. GOTO Syntax
  42.  
  43.  
  44. :FileExists
  45. ECHO.
  46. ECHO ←[0;1;31mERROR:←[0m  The specified output file already exists.
  47. ECHO         Move or rename %TextFile% and try again.
  48. GOTO Syntax
  49.  
  50.  
  51. :NoGhostScript
  52. ECHO.
  53. ECHO ←[0;1;31mERROR:←[0m  GhostScript not found
  54.  
  55.  
  56. :Syntax
  57. ECHO.
  58. ECHO PDF2Txt.bat,  Version 1.00 for Windows 10
  59. ECHO Use GhostScript to extract text from a PDF file
  60. ECHO.
  61. ECHO Usage:  ←[0;1m%~nx0  "pdffile"  [ "textfile" ]←[0m
  62. ECHO.
  63. ECHO Where:  ←[0;1mpdffile←[0m     is the PDF file whose text will be extracted
  64. ECHO         ←[0;1mtextfile←[0m    is the text file to be created
  65. ECHO.
  66. ECHO Notes:  This batch file requires GhostScript to extract text from
  67. ECHO         the PDF file.
  68. ECHO         GhostScript can be found at ←[0;1;30mhttps://www.ghostscript.com/←[0m
  69. ECHO         If a text file is specified, its directory must exist,
  70. ECHO         but the file itself must not.
  71. ECHO         If no text file is specified, the text file will get the
  72. ECHO         name and location of the PDF file, with extension ".txt".
  73. ECHO.
  74. ECHO Written by Rob van der Woude
  75. ECHO https://www.robvanderwoude.com
  76.  
  77. ENDLOCAL
  78. EXIT /B 1
  79.  

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