在DOS批处理文件中注释多行

时间:2011-12-15 21:36:11

标签: batch-file comments dos

我写了很大的MS DOS批处理文件。要测试这个批处理文件,我只需执行一些行,并希望隐藏/注释掉剩余的行。

我有一些以::开头的现有注释行,因此我不能再使用::因为它会扰乱所有注释。

在这方面的任何帮助将不胜感激。 提前致谢, 杰

7 个答案:

答案 0 :(得分:153)

您可以使用goto跳过代码。

goto comment
...skip this...
:comment

答案 1 :(得分:105)

如果你想在每一行的开头添加REM而不是使用GOTO,你可以使用Notepad ++按照以下步骤轻松完成:

  1. 选择行块
  2. 按Ctrl-Q
  3. 重复步骤取消注释

答案 2 :(得分:11)

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

虽然goto解决方案是一个不错的选择,但它不起作用within brackets(包括FOR和IF命令)。但是这样做。虽然您应该注意关闭FORIF命令的括号和无效语法,因为它们将被解析。

<强> 更新

dbenham's答案中的更新给了我一些想法。 首先 - 有两种不同的情况我们需要多行注释 - 在括号的上下文中,GOTO不能在其外部使用。 在括号上下文中,如果存在阻止代码执行的条件,我们可以使用另一个括号。尽管代码仍将被解析 并且会检测到一些语法错误(FORIF,不正确地关闭括号,错误的参数扩展......)。所以如果可能的话,最好使用GOTO。

虽然不可能创建用作标签的宏/变量 - 但是可以使用宏来进行括号的注释。可以使用两个技巧制作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
)

powershell和C 样式(<之间的混合不能使用,因为重定向具有更高的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
)

强调这是一个评论(认为它不是那么短):

@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
)

答案 3 :(得分:10)

另一种选择是将不需要的行封装在永远不会为真的IF块中

if 1==0 (
...
)

当然,if块中的任何内容都不会执行,但会被解析。所以你不能在其中有任何无效的语法。此外,评论不能包含),除非它被转义或引用。由于这些原因,接受的GOTO解决方案更可靠。 (GOTO解决方案也可能更快)

更新2017-09-19

以下是pdub's GOTO solution的美容增强功能。我定义了一个简单的环境变量“宏”,使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
)

答案 4 :(得分:1)

只想提一下pdub's GOTO solution并不完全正确,以防:comment标签多次出现。我以this question为例修改代码。

@ECHO OFF
SET FLAG=1
IF [%FLAG%]==[1] (
    ECHO IN THE FIRST IF...
    GOTO comment
    ECHO "COMMENT PART 1"
:comment
    ECHO HERE AT TD_NEXT IN THE FIRST BLOCK
)

IF [%FLAG%]==[1] (
    ECHO IN THE SECOND IF...
    GOTO comment
    ECHO "COMMENT PART"
:comment
    ECHO HERE AT TD_NEXT IN THE SECOND BLOCK
)

输出将为

IN THE FIRST IF...
HERE AT TD_NEXT IN THE SECOND BLOCK

将跳过命令在第一个块中TD_NEXT处回显此命令

答案 5 :(得分:0)

@jeb

使用此功能后,stderr似乎无法访问

不,试试这个:

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

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

   echo hello Tunisia

  pause

但为什么会有效?

抱歉,我在frensh回答了这个问题:

(la redirection par 3&gt;estspécialcarelle persiste,on va l'utiliser pour capturer le flux des erreurs 2&gt; est on va le transformer en un fluxpersistantàl'adede 3&gt; 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&gt; au handle 1&gt; qui n'est autre que la console ..)

答案 6 :(得分:-1)

试试这个:

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

   ben ali
   mubarak
   gadeffi
   ..next ?

   echo hello Tunisia

  pause
相关问题