我写了大量的MS DOS批处理文件。为了测试这个批处理文件,我只需要执行一些行,并想隐藏/注释掉剩下的部分。在DOS批处理文件中注释多行
我有一些现有的注释行开头::因此我不能再使用::因为它会搅乱所有评论。
在这方面的任何帮助将不胜感激。 在此先感谢, Jay
我写了大量的MS DOS批处理文件。为了测试这个批处理文件,我只需要执行一些行,并想隐藏/注释掉剩下的部分。在DOS批处理文件中注释多行
我有一些现有的注释行开头::因此我不能再使用::因为它会搅乱所有评论。
在这方面的任何帮助将不胜感激。 在此先感谢, Jay
您可以使用goto
跳过代码。
goto comment
...skip this...
:comment
试试这个:
@echo off 2>Nul 3>Nul 4>Nul
ben ali
mubarak
gadeffi
..next ?
echo hello Tunisia
pause
@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 ..)
另一种选择是将不需要的行放在一个不可能为真的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
)
如果你想在每行开头添加REM,而不是使用GOTO的,可以用记事本++做到这一点很容易以下步骤:
重复步骤以取消
break||(
code that cannot contain non paired closing bracket
)
虽然goto
解决方案是一个不错的选择,但它不会工作within brackets(包括FOR和IF命令)。但这会。尽管您应该小心关闭括号和FOR
和IF
命令的无效语法,因为它们将被解析。
更新
在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
)
谢谢主席先生! – user219628 2011-12-15 23:01:53
+1:为此使用“goto”很有趣,它工作正常! – 2013-11-26 10:20:07
我觉得有趣的是,在命令行中没有真正的注释定义,我不能接受'REM'行作为注释行,它使输出变得模糊 – mkb 2017-10-25 04:18:41