@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 ERROR: 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 ERROR: Please specify a valid PDF file GOTO Syntax :FileExists ECHO. ECHO ERROR: The specified output file already exists. ECHO Move or rename %TextFile% and try again. GOTO Syntax :NoGhostScript ECHO. ECHO ERROR: 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: %~nx0 "pdffile" [ "textfile" ] ECHO. ECHO Where: pdffile is the PDF file whose text will be extracted ECHO textfile 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 https://www.ghostscript.com/ 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