Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate multiple Markdown files using Pandoc on Windows

I have several Markdown (.md) files in a folder and I want to concatenate them and get a final Markdown file using Pandoc. I wrote a bash file like this:

#!/bin/bash 
pandoc *.md > final.md

But I am getting the following error when I double-click on it:

pandoc: *.md: openBinaryFile: invalid argument (Invalid argument)

and the final.md file is empty.

If I try this:

pandoc file1.md file2.md .... final.md 

I am getting the results I expect: a final.md file with the contents of all the other Markdown files.

On macOS it works fine. Why doesn't this work on Windows?

like image 395
ziselos Avatar asked Nov 08 '22 08:11

ziselos


1 Answers

On Unix-like shells (like bash, for which your script is written) glob expansion (e.g. turning *.md into file1.md file2.md file3.md) is performed by the shell, not the application you're running. Your application sees the final list of files, not the wildcard.

However, glob expansion in cmd.exe is performed by the application:

The Windows command interpreter cmd.exe relies on a runtime function in applications to perform globbing.

As a result, Pandoc is being passed a literal *.md when it expects to see a list of files like file1.md file2.md file3.md. It doesn't know how to expand the glob itself and tries to open a file whose name is *.md.

You should be able to run your bash script in a unix-like shell like Cygwin or bash on Windows. It may also work on PowerShell, though I don't have a machine handy to test. As a last resort you could jump through some hoops to write a batch file that expands the glob and passes file names to Pandoc.

like image 160
Chris Avatar answered Nov 14 '22 21:11

Chris