在DOSbatch file中注释多行

我写了大量的MS DOSbatch file。 要testing这个batch file,我只需要执行一些行,并想隐藏/注释掉剩余的。

我有一些现有的注释行开始::因此,我不能再使用::因为它会争夺所有评论。

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

您可以使用goto跳过代码。

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

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

  1. select线块
  2. 按Ctrl-Q

重复步骤取消注释

另一个select是将不需要的行放在一个不可能是真的IF块中

 if 1==0 ( ... ) 

当然,if块中没有任何内容会被执行,但是会被parsing。 所以你不能有任何无效的语法。 另外,注释不能包含)除非它被转义或引用。 由于这些原因,接受的GOTO解决scheme更可靠。 (GOTO解决scheme也可能更快)

更新2017-09-19

这是对pdub的GOTO解决scheme的一个改进。 我定义了一个简单的环境variables“macro”,使GOTO注释语法更好一些。 尽pipe通常build议:标签在批处理脚本中是唯一的,但在同一个批处理脚本中embedded多个注释是确定的。

 @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的解决scheme的这些变种之一。 使用REM而不是BREAK使意图更清晰。

 rem.||( remarks go here ) rem^ ||( The space after the caret is critical ) 
 break||( code that cannot contain non paired closing bracket ) 

虽然goto解决scheme是一个不错的select,但它不会在括号内 (包括FOR和IF命令)工作。但是这样做。 虽然您应该小心closures括号和FORIF命令无效的语法,因为它们将被parsing。

更新

dbenham的回答更新给了我一些想法。 首先 – 有两种不同的情况,我们可能需要多行注释 – 在括号的上下文中,GOTO不能被使用,在它之外。 在括号里面,如果有一个条件阻止代码被执行,我们可以使用另一个括号。尽pipe代码仍然会被parsing,并且会检测到一些语法错误( FORIF ,不正确地closures括号,错误的参数扩展..)所以如果可能的话最好使用GOTO。

虽然不可能创build一个用作标签的macros/variables,但是可以使用macros作为括号的注释。还可以使用两个技巧使GOTO的注释更加对称和更令人愉悦(至less对我而言)。 为此,我将使用两个技巧 – 1)您可以在标签前面放一个符号,然后goto仍然可以find它(我不知道为什么是这个。我的意思是它正在寻找一个驱动器)。 2)你可以把一个单一的:在一个variables名称的末尾,一个replace/子串function将不会被触发(即使在启用的扩展下)。 结合macros的括号注释可以使这两种情况看起来几乎相同。

所以这里是例子(按我最喜欢的顺序):

矩形托架

 @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风格之间的混合( <由于redirect使用更高的优先级,因此无法使用%*由于%*而无法使用):

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

@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> estspécialcar elle persiste,on va l'utiliser pour capturer le flux des erreurs 2> est on va le transformer en un flux persistantàl ade de 3> ceci va nous permettre d'avoir une gestion在这种情况下,我们可以通过一个简单的方法来解决这个问题。

尝试这个:

  @echo off 2>Nul 3>Nul 4>Nul ben ali mubarak gadeffi ..next ? echo hello Tunisia pause