Skip to content

Instantly share code, notes, and snippets.

@arthurattwell
Last active August 7, 2019 06:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save arthurattwell/46ada90de216c762adef6221518b3fb8 to your computer and use it in GitHub Desktop.
Save arthurattwell/46ada90de216c762adef6221518b3fb8 to your computer and use it in GitHub Desktop.
Batch file to save Word docs as .txt with Libreoffice
:: doc2txt.bat
::
:: This batch file converts all the files in a folder to .txt,
:: as long as LibreOffice supports them (e.g. .doc, .docx, .odt)
::
:: 1. Make sure soffice.exe is in your PATH, e.g.
:: Edit Environment Variables, add New in user variables, and add C:\Program Files (x86)\LibreOffice 5\program
:: 2. Save this doc2txt.bat file to the folder alongside the files you want to convert.
:: 3. Make sure Libreoffice is not running. (A bug prevents soffice from working while Libreoffice is open.)
:: 4. Double-click doc2txt.bat.
::
::
:: Don't show these commands to the user
@ECHO off
:: Set the title of the window
TITLE Convert to text
:: Ask if we want to convert all the files in the folder, or one by one
ECHO Remember to make sure LibreOffice is not running.
SET option=
SET /p option=Hit return to convert all the files in this folder. Or type the filename of the file you want to convert.
IF "%option%"=="" GOTO batchconvert
:: Okay, we're doing one file at a time
ECHO Saving %option% as txt...
:singleconvert
soffice --convert-to "txt:Text (encoded):UTF8" "%option%" --headless
SET option=
SET /p option=Done. Hit return to exit, or enter the name of another file to save.
IF "%option%"=="" GOTO:eof
GOTO singleconvert
:: This is where we start a batch conversion
:batchconvert
:: Tell the user what's happening
ECHO Saving all files as text...
:: Loop through all files in the folder and convert -- except for this file
FOR %%f in (.\*) DO (
IF NOT "%%f"==".\doc2txt.bat" soffice --convert-to "txt:Text (encoded):UTF8" "%%f" --headless
)
:: Let the user choose to exit or run again
SET repeat=
SET /p repeat=Hit return to run again, or any other key and enter to exit.
IF "%repeat%"=="" GOTO batchconvert
GOTO:eof
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment