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

发布于 2024-12-21 14:19:44 字数 156 浏览 2 评论 0原文

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

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

我该如何解决这个问题?

I have written huge MS DOS Batch file. To test this batch file I need to execute some lines only and want to hide/comment out remaining.

I have some existing comment lines starting with :: hence I cannot use :: anymore as it will scramble all comments.

How can I solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

半寸时光 2024-12-28 14:19:44

您可以使用 goto 跳过代码。

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

You can use a goto to skip over code.

goto comment
...skip this...
:comment
阳光①夏 2024-12-28 14:19:44

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

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

重复步骤以取消注释

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. Select the block of lines
  2. hit Ctrl-Q

Repeat steps to uncomment

孤云独去闲 2024-12-28 14:19:44

另一种选择是将不需要的行包含在永远不可能为 true 的 IF 块中。

if 1==0 (
...
)

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

更新 2017-09-19

这是对 pdub 的 GOTO 的外观增强解决方案。我定义了一个简单的环境变量“宏”,使 GOTO 注释语法更好地自我记录。尽管通常建议 :labels 在批处理脚本中是唯一的,但在同一个批处理脚本中嵌入多个这样的注释确实是可以的。

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

rem.||(
   remarks
   go here
)

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

Another option is to enclose the unwanted lines in an IF block that can never be true

if 1==0 (
...
)

Of course nothing within the if block will be executed, but it will be parsed. So you can't have any invalid syntax within. Also, the comment cannot contain ) unless it is escaped or quoted. For those reasons the accepted GOTO solution is more reliable. (The GOTO solution may also be faster)

Update 2017-09-19

Here is a cosmetic enhancement to pdub's GOTO solution. I define a simple environment variable "macro" that makes the GOTO comment syntax a bit better self documenting. Although it is generally recommended that :labels are unique within a batch script, it really is OK to embed multiple comments like this within the same batch script.

@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 npocmaka's solution. 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
)
愁以何悠 2024-12-28 14:19:44
break||(
 code that cannot contain non paired closing bracket
)

虽然 goto 解决方案是一个不错的选择,但它在 括号内 不起作用(包括FOR和IF命令)。但是这样就可以了。不过,您应该小心 FORIF 命令的右括号和无效语法,因为它们将被解析。

更新

dbenham 答案中的更新给了我一些想法。
首先 - 在两种不同的情况下,我们可能需要多行注释 - 在不能使用 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
)

<之间的混合strong>powershell 和 C 样式(< 无法使用,因为重定向具有更高的优先级。* 无法使用,因为 %*):

@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
)
break||(
 code that cannot contain non paired closing bracket
)

While the goto solution is a good option it will not work within brackets (including FOR and IF commands).But this will. Though you should be careful about closing brackets and invalid syntax for FOR and IF commands because they will be parsed.

Update

The update in the dbenham's answer gave me some ideas.
First - there are two different cases where we can need multi line comments - in a bracket's context where GOTO cannot be used and outside it.
Inside brackets context we can use another brackets if there's a condition which prevents the code to be executed.Though the code thede will still be parsed
and some syntax errors will be detected (FOR,IF ,improperly closed brackets, wrong parameter expansion ..).So if it is possible it's better to use 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
comments more symetrical and more pleasing (at least for me). For this I'll use two tricks - 1) you can put a single symbol in front of a label and goto will still able
to find it (I have no idea why is this.My guues it is searching for a drive). 2) you can put a single :
at the end of a variable name and a replacement/subtring feature will be not triggered (even under enabled extensions). Wich combined with the macros for brackets comments can
make the both cases to look almost the same.

So here are the examples (in the order I like them most):

With rectangular brackets:

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

With curly brackets:

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

With parentheses:

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

Mixture between powershell and C styles (< cannot be used because the redirection is with higher prio.* cannot be used because of the %*) :

@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 emphase 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
)
昨迟人 2024-12-28 14:19:44

只是想提一下,pdub 的 GOTO 解决方案在 :comment 标签多次出现的情况下并不完全正确。我修改这个问题中的代码作为示例。

@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

命令ECHO HERE AT TD_NEXT IN THE FIRST BLOCK被跳过。

Just want to mention that pdub's GOTO solution is not fully correct in case :comment label appear in multiple times. I modify the code from this question as the example.

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

The output will be

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

The command ECHO HERE AT TD_NEXT IN THE FIRST BLOCK is skipped.

巴黎盛开的樱花 2024-12-28 14:19:44

试试这个:

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

   ben ali
   mubarak
   gadeffi
   ..next ?

   echo hello Tunisia

  pause

try this:

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

   ben ali
   mubarak
   gadeffi
   ..next ?

   echo hello Tunisia

  pause
琴流音 2024-12-28 14:19:44

@jeb

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

不,试试这个:

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

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

   echo hello Tunisia

  pause

但是为什么它有效?

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

(la重定向par 3> est spécial car elle persiste, on val'utiliser pour capturer le Flux des erreurs 2> est on val le Transformer en un Flux persistant à l'ade de 3> 塞西娃
nous permettre d'avoir une gestion des erreur pour notre 环境脚本..par la suite si on veux recuperer le Flux 'stderr' il faut faire une autre redirection du handle 2>; au手柄1> qui n'est autre que la console..)

@jeb

And after using this, the stderr seems to be inaccessible

No, try this:

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

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

   echo hello Tunisia

  pause

But why it works?

sorry, i answer the question in frensh:

( la redirection par 3> est spécial car 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 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.. )

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文