Commenting multiple lines in DOS batch file

我已经写了巨大的 MS DOS 批处理文件。为了测试这个批处理文件,我只需要执行一些代码行,并想隐藏/注释掉剩余的代码行。

我有一些现有的评论行开始与 ::,因此我不能再使用 ::,因为它会扰乱所有的评论。

我怎样才能解决这个问题?

128325 次浏览

You can use a goto to skip over code.

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

试试这个:

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


ben ali
mubarak
gadeffi
..next ?


echo hello Tunisia


pause

@ Jeb

在使用这个之后,stderr 似乎无法访问

不,试试这个:

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


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


echo hello Tunisia


pause

但为什么会有用呢?

抱歉,我用法语回答问题:

(3级重定向是特殊的,因为它会持续,我们将使用它来捕获错误的通量。2级重定向是在3级持续的通量中进行的 我们允许在我们的剧本环境中出现错误。.接下来,如果我们想要恢复“紧急”的流量,就需要将手柄2 > 重定向到手柄1 > ,这个手柄除了控制台以外没有其他东西。. )

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

if 1==0 (
...
)

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

更新2017-09-19

这是对 Pdub 的 GOTO 解决方案的美容增强。我定义了一个简单的环境变量“宏”,使 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

Or you could use one of these variants of 恩波马卡的解决方案. The use of REM instead of BREAK makes the intent a bit clearer.

rem.||(
remarks
go here
)


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

If you want to add REM at the beginning of each line instead of using GOTO, you can use Notepad++ to do this easily following these steps:

  1. 选择行块
  2. 按 Ctrl-Q

重复取消注释的步骤

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

虽然 goto解决方案是一个很好的选择,但它不能工作于 括号内(包括 FOR 和 IF 命令)。但这个可以。但是对于 FORIF命令,您应该注意括号和无效语法,因为它们将被解析。

更新

The update in the dbenham's answer gave me some ideas. 首先-有两种不同的情况下,我们可以需要多行注释-在一个括号的上下文中,GOTO 不能被使用,并在它之外。 如果存在一个阻止代码执行的条件,我们可以在方括号内使用另一个方括号。尽管仍将解析代码 thede 并且会检测到一些语法错误(FORIF,不正确的闭括号,错误的参数扩展。.).所以如果可能的话,最好使用 GOTO。

Though it is not possible to create a macro/variable used as a label - but is possible to use macros for bracket's comments.Still two tricks can be used make the GOTO 评论更对称,更令人愉快(至少对我来说)。为此,我将使用两个技巧-1),你可以把一个标签前的单一符号,goto 仍然能够 找到它(我不知道这是为什么。我猜它正在寻找驱动器)。你可以把一个单一的 : 在变量名的末尾和替换/子串特性将不会被触发(即使在启用了扩展名的情况下)。与用于括号注释的宏结合使用的 让两个箱子看起来几乎一样。

下面是一些例子(按照我最喜欢的顺序) :

矩形支架矩形支架:

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

To 强调 that's a comment (thought it is not so short):

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

只是想提一下,Pdub 的 GOTO 解决方案是不完全正确的情况下: 注释标签出现在多次。作为示例,我修改了来自 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 的 ECHO