2017-03-06 105 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