在Windows中使用Pandoc批量转换文件 [英] Batch convert files with pandoc in windows

查看:133
本文介绍了在Windows中使用Pandoc批量转换文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我环顾四周,我发现的唯一答案是pandoc(如果有人知道任何具有ui的程序都可以批量执行此操作,那就太好了.)

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

如果我不想做的是,在目录中有两个bat文件,一个可以将目录中的所有文件以及任何子目录从markdown转换为HTML,另一个可以将其转换回来.

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.

我只是不知道如何在命令提示符下(此处为noob)获取"for in"递归函数.我了解了如何使用pandoc的示例来回转换单个文件:

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

或后退:

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

但是我还不能在命令提示符或bat文件中进行批处理.用Google搜索它,从这里得到一些类似的问题,我试图为我修改一些答案,但仍然无法解决.

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.

推荐答案

@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

您需要更改sourcedir的设置以适合您的情况.

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

此过程将要执行的操作是简单地将echo pandoc命令显示在屏幕上.这是为了让您在实际执行之前先看到要执行的操作.要在确实确定命令正确之后执行命令 ,只需将echo pandoc更改为pandoc

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

我假设pandoc将处理"quoted filenames"-允许文件名包含空格.

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

按现状,仅当不存在具有相同名称部分的已转换"文件时,代码才会执行​​转换.如果无论如何都要进行转换,请在echo之前删除if not exist....缺点是pandoc会将x.html转换为x.md,然后将结果x.md转换为x.html.

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.

如果要进行两批处理,只需复制文件,并从相应的dir...行中删除*.md*.html.

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

这篇关于在Windows中使用Pandoc批量转换文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆