2017-03-06 52 views
0

我有一个目录中有子目录,我试图将docx转换为html。 到目前为止,我发现这个命令:需要pandoc批处理命令

@ECHO off 
:selectfile 
:: Clear any preexisting filename variables 
SET filename= 
:: Ask which file we're converting. 
SET /p filename=Which file? (Don't include the .docx file extension): 
CALL pandoc -o -s "%filename%".html --self-contained "%filename%".docx 
GOTO selectfile 

的问题是它的问我的文件名,我必须把这个蝙蝠文件中的每个子文件夹内做的工作。 我想进行一些更改,以便自动检测子文件夹并将所有docx文件转换为具有相同名称的html文件。 这是可能的家伙? 任何人都可以修改此脚本? 非常感谢。

+3

提示:https://stackoverflow.com/documentation/batch-file/3695/for-loops-in-batch -files#t = 201703061006249850917 – DavidPostill

+1

并且请将整个文件名用双引号括起来,而不仅仅是其中的一部分,它强制Windows命令解释器分别启动应用程序的启动代码来修复参数字符串,即使用'“%filename%.html” '和'“%filename%.docx”'。 – Mofi

+0

set filename命令会搞砸了。它的罚款和这样的工作,但不能解决我的问题。 – user3551620

回答

1

下面的注释代码片段可以帮助:

@echo OFF 
SETLOCAL EnableExtensions 

rem iterate all *.docx recursively 
for /f "delims=" %%G in ('dir /B /S *.docx 2^>NUL') do (

    rem check .html existence 
    if not exist "%%~dpnG.html" (

     rem change the current directory and store the previous path for use by the POPD 
     pushd "%%~dpG" 

     rem `CALL pandoc` is merely displayed for debugging purposes 
     rem    remove ECHO no sooner than debugged   
     ECHO CALL pandoc -o -s "%%~nG.html" --self-contained "%%~nxG" 

     rem change directory back to the path most recently stored by the PUSHD command 
     popd 
    ) 
) 

编辑

我想做出一些改变,这样它会检测子 自动转换所有docx文件到html文件 具有相同的名称。

您可以嵌入上面的代码片段到你的脚本如下:

@echo OFF 
SETLOCAL EnableExtensions 
:selectfile 
:: Clear any preexisting filename variables 
SET filename= 
:: Ask which file we're converting. 
SET /p filename=Which file? (Don't include the .docx file extension): 

if not defined filename GOTO selectfile 

rem iterate all %filename%.docx recursively 
for /f "delims=" %%G in ('dir /B /S "%filename%.docx" 2^>NUL') do (
    rem check .html existence 
    if not exist "%%~dpnG.html" (
     rem change the current directory and store the previous path for use by the POPD 
     pushd "%%~dpG" 
     rem `CALL pandoc` is merely displayed for debugging purposes 
     rem    remove ECHO no sooner than debugged   
     ECHO CALL pandoc -o -s "%%~nG.html" --self-contained "%%~nxG" 
     rem change directory back to the path most recently stored by the PUSHD command 
     popd 
    ) 
) 

GOTO selectfile 

资源(必读):

+0

openFile:不存在(没有这样的文件或目录)得到这个错误。 – user3551620

+0

其搜索所有子文件夹tho .. @ JosefZ – user3551620

+0

刚刚删除-s和工作..谢谢一吨。男人真的很感激:D – user3551620