(view source code of pdf2txt.bat as plain text)
@ECHO OFF
SETLOCAL
:: Check Command Line
IF "%~1"=="" GOTO Syntax
IF NOT EXIST "%~1" GOTO InvalidPDF
IF /I NOT "%~x1"==".pdf" GOTO InvalidPDF
IF "%~2"=="" (
SET TextFile="%~dpn1.txt"
) ELSE (
SET TextFile="%~2"
IF NOT EXIST "%~dp2*.*" (
ECHO.
ECHO ←[0;1;31mERROR:←[0m Directory "%~dp2" not found.
GOTO Syntax
)
)
IF EXIST %TextFile% GOTO FileExists
IF NOT "%~3"=="" GOTO Syntax
:: Find GhostScript executable
SET ProgDir=
SET ProgFile=
FOR /F "tokens=*" %%A IN ('REG.EXE Query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall /f Ghostscript ^| FIND.EXE /I "HKEY_"') DO (
FOR /F "tokens=2*" %%B IN ('REG.EXE Query "%%~A" /v UninstallString') DO (
SET ProgDir=%%~dpC
)
)
IF "%ProgDir%"=="" GOTO NoGhostScript
FOR /F "tokens=*" %%A IN ('DIR /B /S "%ProgDir%gswin*c.exe"') DO SET ProgFile=%%~fA
IF "%ProgFile%"=="" GOTO NoGhostScript
:: Run GhostScript
"%ProgFile%" -sDEVICE=txtwrite -o %TextFile% "%~f1"
ENDLOCAL
GOTO:EOF
:InvalidPDF
ECHO.
ECHO ←[0;1;31mERROR:←[0m Please specify a valid PDF file
GOTO Syntax
:FileExists
ECHO.
ECHO ←[0;1;31mERROR:←[0m The specified output file already exists.
ECHO Move or rename %TextFile% and try again.
GOTO Syntax
:NoGhostScript
ECHO.
ECHO ←[0;1;31mERROR:←[0m GhostScript not found
:Syntax
ECHO.
ECHO PDF2Txt.bat, Version 1.00 for Windows 10
ECHO Use GhostScript to extract text from a PDF file
ECHO.
ECHO Usage: ←[0;1m%~nx0 "pdffile" [ "textfile" ]←[0m
ECHO.
ECHO Where: ←[0;1mpdffile←[0m is the PDF file whose text will be extracted
ECHO ←[0;1mtextfile←[0m is the text file to be created
ECHO.
ECHO Notes: This batch file requires GhostScript to extract text from
ECHO the PDF file.
ECHO GhostScript can be found at ←[0;1;30mhttps://www.ghostscript.com/←[0m
ECHO If a text file is specified, its directory must exist,
ECHO but the file itself must not.
ECHO If no text file is specified, the text file will get the
ECHO name and location of the PDF file, with extension ".txt".
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.0081 seconds