【问题标题】:How can I use pandoc for all files in the folder in Windows?如何对 Windows 文件夹中的所有文件使用 pandoc?
【发布时间】:2017-01-04 18:54:11
【问题描述】:

在 pandoc.org 上的常见问题解答中有 Linux 和 Mac 用户的说明:

for f in *.txt; do pandoc "$f" -s -o "${f%.txt}.rtf"; done

但没有针对 Windows 用户的说明。

【问题讨论】:

标签: windows pandoc


【解决方案1】:

我对这个问题感到沮丧,因此我编写了一个批处理文件,您可以从 cmd 或 PowerShell 运行该批处理文件,该文件在文件夹/目录和所有子目录(即,它是递归)。代码如下。将代码复制到记事本并保存为pancompile.bat。从 cmd 运行是最简单的。在 PowerShell 中,您将其调用为 .\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"

这是pancompile.bat 的代码。请注意,只有当所有目录路径和文件的字符总数小于 8092 时,它才会按预期工作:

@echo off
:: Check if user entered required options
if $%1$==$$ goto usage
if $%2$==$$ goto usage
setlocal disableDelayedExpansion
set "mask=%3"
if $%3$==$$ set "mask=*"
:: Remove quotation marks from pandoc options 
set options=%4
if not $%4$==$$ set options=%options:"=%
set "files="
:: This will only work if the total characters of all the paths and filenames together is less than 8192 characters
for /r %1 %%F in (%mask%) do call set files=%%files%% "%%F"
echo/
echo The following pandoc command will be executed:
echo/ 
echo pandoc -s %files% -o %2 %options%
echo/
:ask
echo Would you like to run pandoc on the files listed above? (Y/N)
set INPUT=
set /P INPUT=?: %=%
if /I "%INPUT%"=="y" goto yes 
if /I "%INPUT%"=="n" goto no
goto ask
:yes
pandoc -s %files% -o %2 --wrap=none %options%
echo Done
goto exit
:no
echo Command was cancelled
goto exit
:usage
echo/
if $%1$==$$ (
    echo This batch file needs to be run from the command line or from PowerShell
    echo/
) 
echo Usage: pancompile DIRECTORY FILENAME [filemask] ["options"]
echo Uses pandoc to compile all documents in specified directory and subdirectories to a single output document
echo/
echo DIRECTORY         the directory/folder to parse recursively (passed to pandoc -s);
echo                   use quotation marks if there are spaces in the directory name
echo FILENAME          the output file (passed to pandoc -o); use quotation marks if spaces
echo filemask          an optional file mask/filter, e.g. *.md; leave blank for all files 
echo "options"         optional list of pandoc commands (must be in quotation marks)
echo/
echo Minimal example: pancompile docs complete_book.docx
echo Typical example: pancompile "My Documents" "Complete Book.docx" *.md "-f markdown -t docx --standalone --toc"
:exit
:: End with a pause so user can read messages
echo/
echo Press any key to exit ...
pause>nul

【讨论】:

    【解决方案2】:

    来自pandoc-discuss

    for %F in (*.txt) do pandoc %F > %F~n.html
    

    【讨论】:

    • 我试过了。但我无法在命令提示符下打开输出文件:C:\Users\Admin\Documents\Folder>pandoc input.html 1>output.html~n.docx。但是如果我使用pandoc output.docx input.html,我可以打开输出文件。但我不知道如何对许多文件执行此操作
    • 如果您想转换为 docx,您显然必须在我发布的行中将 .html 替换为 .docx...
    • 我当然这样做了for %F in (*.html) do pandoc %F > %F~n.docx。但它不起作用
    【解决方案3】:

    我遇到了同样的问题,但找不到解决方案。

    问题是在 PowerShell 中您不能使用像 *.md 这样的通配符。这简直就是 Unix 的东西。

    您必须在 Windows 中处理单个文件。

    【讨论】:

      猜你喜欢
      • 2014-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-19
      • 1970-01-01
      • 1970-01-01
      • 2021-12-28
      相关资源
      最近更新 更多