0

In the FAQ on pandoc.org there is the instruction for Linux and Mac users:

for f in *.txt; do pandoc "$f" -s -o "${f%.txt}.rtf"; done

but there is no instruction for Windows users.

Nipayl
  • 21
  • 1
  • 4
  • see also https://stackoverflow.com/questions/17157638/batch-processing-pandoc-conversions-in-windows – mb21 Apr 05 '19 at 07:21

3 Answers3

3

I was frustrated by this issue, so I wrote a batch file that you can run from cmd or from PowerShell that invokes pandoc on all the files of a specific type in a folder/directory, and in all subdirectories (i.e., it's recursive). The code is below. Copy the code into notepad and save it as pancompile.bat. It's easiest to run from cmd. From PowerShell you invoke it as .\pancompile.bat. If you run the command without any parameters, it will spit out sample usage like so:

Usage: pancompile DIRECTORY FILENAME [filemask] ["options"]
Uses pandoc to compile all documents in specified directory and subdirectories to a single output document

DIRECTORY         the directory/folder to parse recursively (passed to pandoc -s);
                  use quotation marks if there are spaces in the directory name
FILENAME          the output file (passed to pandoc -o); use quotation marks if spaces
filemask          an optional file mask/filter, e.g. *.md; leave blank for all files
"options"         optional list of pandoc commands (must be in quotation marks)

Minimal example: pancompile docs complete_book.docx
Typical example: pancompile "My Documents" "Complete Book.docx" *.md "-f markdown -t docx --standalone --toc"

And here is the code for pancompile.bat. Note that it will only work as expected if the total number of characters for all the directory paths and files is less than 8092:

@echo off
:: Check if user entered required options
if $%1$==$$ goto usage
if $%2$==$$ goto usage
setlocal disableDelayedExpansion
set "mask=%3"
if $%3$==$$ set "mask=*"
:: Remove quotation marks from pandoc options 
set options=%4
if not $%4$==$$ set options=%options:"=%
set "files="
:: This will only work if the total characters of all the paths and filenames together is less than 8192 characters
for /r %1 %%F in (%mask%) do call set files=%%files%% "%%F"
echo/
echo The following pandoc command will be executed:
echo/ 
echo pandoc -s %files% -o %2 %options%
echo/
:ask
echo Would you like to run pandoc on the files listed above? (Y/N)
set INPUT=
set /P INPUT=?: %=%
if /I "%INPUT%"=="y" goto yes 
if /I "%INPUT%"=="n" goto no
goto ask
:yes
pandoc -s %files% -o %2 --wrap=none %options%
echo Done
goto exit
:no
echo Command was cancelled
goto exit
:usage
echo/
if $%1$==$$ (
    echo This batch file needs to be run from the command line or from PowerShell
    echo/
) 
echo Usage: pancompile DIRECTORY FILENAME [filemask] ["options"]
echo Uses pandoc to compile all documents in specified directory and subdirectories to a single output document
echo/
echo DIRECTORY         the directory/folder to parse recursively (passed to pandoc -s);
echo                   use quotation marks if there are spaces in the directory name
echo FILENAME          the output file (passed to pandoc -o); use quotation marks if spaces
echo filemask          an optional file mask/filter, e.g. *.md; leave blank for all files 
echo "options"         optional list of pandoc commands (must be in quotation marks)
echo/
echo Minimal example: pancompile docs complete_book.docx
echo Typical example: pancompile "My Documents" "Complete Book.docx" *.md "-f markdown -t docx --standalone --toc"
:exit
:: End with a pause so user can read messages
echo/
echo Press any key to exit ...
pause>nul
Jaifroid
  • 437
  • 3
  • 6
1

From pandoc-discuss:

for %F in (*.txt) do pandoc %F > %F~n.html
mb21
  • 34,845
  • 8
  • 116
  • 142
  • I tried to do it. But I can't open output files, in command prompt: `C:\Users\Admin\Documents\Folder>pandoc input.html 1>output.html~n.docx`. But if I use `pandoc output.docx input.html`, I can open the output file. But I don't know how to do this for many files – Nipayl Jan 05 '17 at 00:24
  • if you want do convert to docx you'd obviously have to replace `.html` with `.docx` in the line I posted... – mb21 Jan 05 '17 at 08:27
  • Of course I did so `for %F in (*.html) do pandoc %F > %F~n.docx`. But it does not work – Nipayl Jan 05 '17 at 16:16
0

I faced the same problem and I couldn't find a solution.

The problem is that in PowerShell you can't use wildcards like *.md. This simply is a Unix thing.

You will have to work with a single file in windows.

lf_araujo
  • 1,991
  • 2
  • 16
  • 39