【问题标题】:Batch processing Pandoc conversions in Windows在 Windows 中批量处理 Pandoc 转换
【发布时间】:2021-09-16 06:26:32
【问题描述】:

我正在尝试在 Windows 中使用 Pandoc 将大量 HTML 文件转换为 Markdown,并在 how to do this on a Mac 上找到了答案,但在尝试在 Windows PowerShell 中运行以下内容时收到错误。

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

有人可以帮我把它翻译成在 Windows 中工作吗?

【问题讨论】:

    标签: powershell batch-file markdown pandoc


    【解决方案1】:

    要递归转换文件夹中的文件,试试这个(Windows 提示符命令行):

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

    要在批处理文件中使用,请加倍 %

    【讨论】:

    • 感谢您的回答!我收到了与我收到的 Mac 代码类似的错误:在关键字 'for' 之后缺少开头的 '('。
    • 这可能是一个菜鸟问题,但我注意到代码在命令提示符和 PowerShell 上的运行方式有所不同。这是在命令提示符中为我工作的代码行:for %i in (*.html) do pandoc -f html -t markdown %~ni.html > md/%~ni.md
    • 要将当前文件夹中的所有 md 文件转换为 html,请使用以下命令。 for /r "." %i in (*.md) do pandoc -o "%~i.html" "%~i"
    【解决方案2】:
    • 这里的大多数答案(for ... 解决方案)是针对 cmd.exe,而不是 PowerShell。
    • mb21's answer 在正确的轨道上,但在针对每个输入文件方面存在错误;此外,很难直观地解析。

    功能等效的PowerShell命令是:

    Get-ChildItem -File -Recurse -Filter *.md | ForEach-Object {
      pandoc -o ($_.FullName + '.txt') $_.FullName
    }
    

    【讨论】:

      【解决方案3】:

      Endoro 的回答很棒,不要被%i 添加的参数弄糊涂了。

      为了帮助他人,我需要将 RST(重组文本)转换为 dokuwiki 语法,所以我创建了一个 convert.bat

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

      适用于文件夹和子文件夹中的所有 rst 文件。

      【讨论】:

      • 您说不要对 Endoro 的参数感到困惑,但没有对这些参数的作用提供任何说明,并且在您的答案中使用了类似的参数。该命令不起作用。你能澄清一下吗?
      【解决方案4】:

      如果您想递归地通过一个目录及其子目录来编译所有类型的文件,例如*.md,那么您可以使用我在回答另一个问题How can I use pandoc for all files in the folder in Windows? 时编写的批处理文件。我称之为pancompile.bat,用法如下。转到代码的另一个答案。

      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"
      

      【讨论】:

        【解决方案5】:

        使用powershell内置gci

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

        来自https://github.com/jgm/pandoc/issues/5429

        【讨论】:

          【解决方案6】:

          我创建了一个 python 脚本,我一直使用它来将 Markdown 文件树转换为单个输出文件。可以在 github 上找到:

          https://github.com/andrewrproper/pandoc-folder

          【讨论】:

          • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
          猜你喜欢
          • 2012-05-06
          • 1970-01-01
          • 2011-07-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多