问题
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 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.
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
or backwards:
pandoc test.html -f html -t markdown -o test.md
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.
回答1:
@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.
来源:https://stackoverflow.com/questions/27852067/batch-convert-files-with-pandoc-in-windows