Batch convert files with pandoc in windows

前端 未结 1 2007
时光说笑
时光说笑 2021-02-11 00:10

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 t

1条回答
  •  执笔经年
    2021-02-11 00:47

    @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.

    0 讨论(0)
提交回复
热议问题