Batch processing Pandoc conversions in Windows
Asked Answered
C

5

9

I am trying to convert a large number of HTML files into Markdown using Pandoc in Windows, and have found an answer on how to do this on a Mac, but receive errors when attempting to run the following in Windows PowerShell.

find . -name \*.md -type f -exec pandoc -o {}.txt {} \;

Can someone help me translate this to work in Windows?

Chamonix answered 17/6, 2013 at 22:3 Comment(0)
S
9

to convert files in folders recursively try this (Windows prompt command line):

for /r "startfolder" %i in (*.htm *.html) do pandoc -f html -t markdown "%~fi" -o "%~dpni.txt"

For use in a batch file double the %.

Salta answered 17/6, 2013 at 22:25 Comment(3)
Thanks for your answer! I received a similar error to what I received with the Mac code: Missing opening '(' after keyword 'for'.Chamonix
This might have been a noob problem, but I've noticed a difference in how the code runs on Command Prompt vs. PowerShell. Here's the line of code that worked for me in Command Prompt: for %i in (*.html) do pandoc -f html -t markdown %~ni.html > md/%~ni.mdChamonix
For converting all md files in current folder to html use below command. for /r "." %i in (*.md) do pandoc -o "%~i.html" "%~i"Illsuited
S
1
  • Most of the answers here (for ... solutions) are for cmd.exe, not PowerShell.
  • mb21's answer is on the right track, but has a bug with respect to targeting each input file; also, it is hard to parse visually.

The functionally equivalent PowerShell command is:

Get-ChildItem -File -Recurse -Filter *.md | ForEach-Object {
  pandoc -o ($_.FullName + '.txt') $_.FullName
}
Sd answered 10/1, 2020 at 16:52 Comment(0)
A
0

Endoro's answer is great, don't get confused by the parameters added to %i.

For helping others, I needed to convert from RST (restructured text) to dokuwiki syntax, so I created a convert.bat with:

FOR /r "startfolder" %%i IN (*.rst) DO pandoc -f rst -t dokuwiki "%%~fi" -o "%%~dpni.txt"

Works for all rst files in folders and subfolders.

Aphasia answered 13/5, 2016 at 11:19 Comment(1)
You said don't get confuse by Endoro's parameters, but didn't offer any clarification for what those parameters do and used similar parameters in your answer. This command doesn't work. Can you clarify?Gossipmonger
C
0

If you want to go recursively through a directory and its subdirectories to compile all the files of type, say, *.md, then you can use the batch file I wrote in answer to another question How can I use pandoc for all files in the folder in Windows? . I call it pancompile.bat and the usage is below. Go to the other answer for the code.

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"
Confiscable answered 18/10, 2018 at 23:31 Comment(0)
H
0

Using the powershell built-in gci:

gci -r -i *.md |foreach{$docx=$_.directoryname+"\"+$_.basename+".docx";pandoc $_.name -o $docx}

from https://github.com/jgm/pandoc/issues/5429

Houseman answered 5/4, 2019 at 7:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.