4

I looked around and the only answer I found was pandoc (if anybody knows any programs with a ui that can do this in batches that would be great).

If not what I'd like to do is have two bat files in a directory, one to be able to convert all the files in a directory and any subdirectories from markdown to HTML, the other to convert back.

I just don't get how to get the "for in" recursive thing working in the command prompt (noob here). I got how to convert single files to and from with the examples from pandoc:

pandoc test.md -f markdown -t html -o test.html

or backwards:

pandoc test.html -f html -t markdown -o test.md

but I've yet to be able to do batches in either the command prompt or with a bat file. Googled it, got the few similar questions from here and I've tried to modify a few answers for me and still couldn't manage it.

If someone could give me the basic format it's supposed to be in, I'd really appreciate it.

bythelake
  • 55
  • 1
  • 6
  • Note that converting from HTML to Markdown will not work perfectly, as HTML is semantically richer than Markdown. – ChrisGPT was on strike Jan 09 '15 at 01:18
  • 1
    Try showing us a sample convert-from-html-to-markdown command and a convert-from-markdown-to-html command by editing them into your question. This shows us that you've done as you claim, the way that you've proved works for a file on your machine. Please also edit-out your call for other-product recommendations - it invites opinionated "language-wars" – Magoo Jan 09 '15 at 02:44
  • I'm planning on only putting html I already converted back to md. I'm aware there's limitations, but it can indeed to it for what I need. – bythelake Jan 09 '15 at 19:17
  • I added the back/forth code examples. I'm not sure what you mean by opinionated language wars. I literally found no other options but pandoc and this isn't about languages... *confused* I edited to specify I'm looking for something simpler with a ui that can batch convert because I had found nothing but pandoc which is a command line program as far as i understand. If you still want me to edit it out I can though, but I don't see how it would start any type of argument... – bythelake Jan 09 '15 at 19:22
  • 1
    Also see here http://stackoverflow.com/q/17157638 – Avatar May 13 '16 at 14:22

1 Answers1

4
@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
PUSHD "%sourcedir%"
FOR /f "delims=" %%a IN (
 'dir /b /s /a-d *.md *.html '
 ) DO (
 IF /i "%%~xa"==".md" (
  IF NOT EXIST "%%~dpna.html" ECHO pandoc "%%a" -f markdown -t html -o "%%~dpna.html"
 ) ELSE (
  IF NOT EXIST "%%~dpna.MD" ECHO pandoc "%%a" -f html -t markdown -o "%%~dpna.MD"
 )
)
popd
GOTO :EOF

You would need to change the setting of sourcedir to suit your circumstances.

What this procedure would do is to simply echo the pandoc command to the screen. This is to allow you to see what would be executed before actually executing it. To actually execute the commands after you're sure they're correct simply change echo pandoc to pandoc

I've assumed that pandoc will deal with "quoted filenames" - which allows filenames to contain spaces.

As it stands, the code will execute the conversion only if there isn't already a "converted" file in existence with the same name part. If you want to convert regardless, remove the if not exist... before the echo. Downside would be that pandoc will convert x.html to x.md then reconvert the resultant x.md to x.html.

If you want to have two batches, simply duplicate the file and remove the *.md or *.html from the dir... line as appropriate.

Magoo
  • 77,302
  • 8
  • 62
  • 84