Batch convert files with pandoc in windows

喜你入骨 提交于 2019-12-03 08:51:30
@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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!