2011-12-15 39 views
66

我寫了大量的MS DOS批處理文件。爲了測試這個批處理文件,我只需要執行一些行,並想隱藏/註釋掉剩下的部分。在DOS批處理文件中註釋多行

我有一些現有的註釋行開頭::因此我不能再使用::因爲它會攪亂所有評論。

在這方面的任何幫助將不勝感激。 在此先感謝, Jay

回答

129

您可以使用goto跳過代碼。

goto comment 
...skip this... 
:comment 
+0

謝謝主席先生! – user219628 2011-12-15 23:01:53

+5

+1:爲此使用「goto」很有趣,它工作正常! – 2013-11-26 10:20:07

+0

我覺得有趣的是,在命令行中沒有真正的註釋定義,我不能接受'REM'行作爲註釋行,它使輸出變得模糊 – mkb 2017-10-25 04:18:41

-1

試試這個:

@echo off 2>Nul 3>Nul 4>Nul 

    ben ali 
    mubarak 
    gadeffi 
    ..next ? 

    echo hello Tunisia 

    pause 
+0

+1,但它爲什麼起作用?並且在使用這個之後,stderr似乎不可訪問 – jeb 2011-12-16 10:54:02

+1

-1,這個「有效」,因爲echo 2> Nul將標準錯誤流重定向到NUL,掩埋它(3> Nul,4> Nul重定向輔助流爲no真正的原因)。這不會註釋掉這些行,它只是防止顯示錯誤消息。所以任何可以被解釋爲命令行的東西仍然會運行。 – pdubs 2011-12-16 17:47:44

0

@jeb

並使用此之後,標準錯誤似乎是不可訪問

沒有,試試這個:

@echo off 2>Nul 3>Nul 4>Nul 

    ben ali 
    mubarak 2>&1 
    gadeffi 
    ..next ? 

    echo hello Tunisia 

    pause 

但它爲什麼有效?

對不起,我回答frensh問題:

(LA重定向標準桿3> EST專用車ELLE persiste,在VA L'utiliser倒俘獲樂宮通量上erreurs VA樂變壓器連接聯合國2> EST flux persistantàl'ade de 3> ceci va nous permettre d'avoir une gestion des erreur pour tout notre environement de script.Par la suite si on veux recuperer le flux'stderr'il faut faire une autre redirection du handle 2 > au handle 1> qui n'est autre que la console ..)

9

另一種選擇是將不需要的行放在一個不可能爲真的IF塊中

if 1==0 (
... 
) 

當然,if塊中沒有內容會被執行,但它會被解析。所以你不能有任何無效的語法。另外,註釋不能包含),除非它被轉義或引用。由於這些原因,公認的GOTO解決方案更可靠。 (該溶液GOTO也可能更快)

更新2017年9月19日

這裏是化妝品增強pdub's GOTO solution。我定義了一個簡單的環境變量「macro」,它使GOTO註釋語法更好一些。儘管通常建議:標籤在批處理腳本中是唯一的,但在同一批處理腳本中嵌入多個註釋是確實可以的。

@echo off 
setlocal 

set "beginComment=goto :endComment" 

%beginComment% 
Multi-line comment 1 
goes here 
:endComment 

echo This code executes 

%beginComment% 
Multi-line comment 2 
goes here 
:endComment 

echo Done 

或者您可以使用npocmaka's solution這些變體之一。使用REM而不是BREAK使意圖更清晰一些。

rem.||(
    remarks 
    go here 
) 

rem^ ||(
    The space after the caret 
    is critical 
) 
90

如果你想在每行開頭添加REM,而不是使用GOTO的,可以用記事本++做到這一點很容易以下步驟:

  1. 選擇行塊
  2. 按Ctrl-Q

重複步驟以取消

9
break||(
code that cannot contain non paired closing bracket 
) 

雖然goto解決方案是一個不錯的選擇,但它不會工作within brackets(包括FOR和IF命令)。但這會。儘管您應該小心關閉括號和FORIF命令的無效語法,因爲它們將被解析。

更新

dbenham's答案的更新給了我一些想法。 首先 - 有兩種不同的情況,我們可能需要多行註釋 - 在括號的上下文中,GOTO不能使用並且在其外部。 如果有一個條件阻止代碼被執行,那麼我們可以使用另一個括號。雖然代碼仍然會被解析 並且會檢測到一些語法錯誤(FOR,IF,括號不正確,參數展開錯誤。 )如果有可能,最好使用GOTO。

儘管不可能創建用作標籤的宏/變量,但可以使用宏作爲括號的註釋。儘管可以使用兩個技巧使得註釋更加對稱和更令人愉悅(至少對於我)。爲此,我將使用兩個技巧 - 1)您可以在標籤前放一個符號,goto仍然可以通過 找到它(我不知道爲什麼是這樣。我的想法是它正在尋找一個驅動器)。 2)您可以將一個單獨的: 置於變量名的末尾,替換/子串功能將不會被觸發(即使在啓用的擴展下)。結合宏的括號註釋可以使 使這兩種情況看起來幾乎相同。

因此,這裏的例子(順序我最喜歡他們):

隨着矩形支架

@echo off 

::GOTO comment macro 
set "[:=goto :]%%" 
::brackets comment macros 
set "[=rem/||(" & set "]=)" 

::testing 
echo not commented 1 

%[:% 
    multi 
    line 
    comment outside of brackets 
%:]% 

echo not commented 2 

%[:% 
    second multi 
    line 
    comment outside of brackets 
%:]% 

::GOTO macro cannot be used inside for 
for %%a in (first second) do (
    echo first not commented line of the %%a execution 
    %[% 
     multi line 
     comment 
    %]% 
    echo second not commented line of the %%a execution 
) 

隨着大括號

@echo off 

::GOTO comment macro 
set "{:=goto :}%%" 
::brackets comment macros 
set "{=rem/||(" & set "}=)" 

::testing 
echo not commented 1 

%{:% 
    multi 
    line 
    comment outside of brackets 
%:}% 

echo not commented 2 

%{:% 
    second multi 
    line 
    comment outside of brackets 
%:}% 

::GOTO macro cannot be used inside for loop 
for %%a in (first second) do (
    echo first not commented line of the %%a execution 
    %{% 
     multi line 
     comment 
    %}% 
    echo second not commented line of the %%a execution 
) 

隨着括號

@echo off 

::GOTO comment macro 
set "(:=goto :)%%" 
::brackets comment macros 
set "(=rem/||(" & set ")=)" 

::testing 
echo not commented 1 

%(:% 
    multi 
    line 
    comment outside of brackets 
%:)% 

echo not commented 2 

%(:% 
    second multi 
    line 
    comment outside of brackets 
%:)% 

::GOTO macro cannot be used inside for loop 
for %%a in (first second) do (
    echo first not commented line of the %%a execution 
    %(% 
     multi line 
     comment 
    %)% 
    echo second not commented line of the %%a execution 
) 

混合物C之間的powershell和樣式(<不能使用,因爲重定向是具有較高PRIO。*不能使用,因爲的%*):

@echo off 

::GOTO comment macro 
set "/#:=goto :#/%%" 
::brackets comment macros 
set "/#=rem/||(" & set "#/=)" 

::testing 
echo not commented 1 

%/#:% 
    multi 
    line 
    comment outside of brackets 
%:#/% 

echo not commented 2 

%/#:% 
    second multi 
    line 
    comment outside of brackets 
%:#/% 

::GOTO macro cannot be used inside for loop 
for %%a in (first second) do (
    echo first not commented line of the %%a execution 
    %/#% 
     multi line 
     comment 
    %#/% 
    echo second not commented line of the %%a execution 
) 

emphase這是一個註釋(認爲它不是那麼短):

@echo off 

::GOTO comment macro 
set "REM{:=goto :}REM%%" 
::brackets comment macros 
set "REM{=rem/||(" & set "}REM=)" 

::testing 
echo not commented 1 

%REM{:% 
    multi 
    line 
    comment outside of brackets 
%:}REM% 

echo not commented 2 

%REM{:% 
    second multi 
    line 
    comment outside of brackets 
%:}REM% 

::GOTO macro cannot be used inside for 
for %%a in (first second) do (
    echo first not commented line of the %%a execution 
    %REM{% 
     multi line 
     comment 
    %}REM% 
    echo second not commented line of the %%a execution 
)