Windows 我应该在批处理文件中使用哪种注释样式?

Windows 我应该在批处理文件中使用哪种注释样式?,windows,batch-file,coding-style,comments,Windows,Batch File,Coding Style,Comments,我一直在写一些批处理文件,我遇到了一些非常有用的信息。它告诉我的一件事是,行不仅可以用REM注释,还可以用:注释。它说: 批处理代码中的注释可以使用双冒号,这比使用REM命令要好,因为标签在重定向符号之前处理:不会导致任何问题,但rem会产生错误 那么,为什么我看到的大多数指南和示例都使用REM命令呢?:是否适用于所有版本的Windows?tl;dr:REM是在批处理文件中嵌入注释的有文档记录且受支持的方法 :本质上是一个不能跳转到的空白标签,而REM是一个实际的命令,它什么也不做。在这两种情

我一直在写一些批处理文件,我遇到了一些非常有用的信息。它告诉我的一件事是,行不仅可以用
REM
注释,还可以用
注释。它说:

批处理代码中的注释可以使用双冒号,这比使用REM命令要好,因为标签在重定向符号之前处理<代码>:不会导致任何问题,但
rem
会产生错误


那么,为什么我看到的大多数指南和示例都使用
REM
命令呢?
是否适用于所有版本的Windows?

tl;dr:
REM
是在批处理文件中嵌入注释的有文档记录且受支持的方法


本质上是一个不能跳转到的空白标签,而
REM
是一个实际的命令,它什么也不做。在这两种情况下(至少在Windows 7上),重定向操作符的存在都不会导致问题

但是,
在某些情况下会在块中表现不正常,被解析为某种驱动器号而不是标签。我对确切的位置有点模糊,但仅此一点就足以让我只使用
REM
。它是在批处理文件中嵌入注释的有文档记录且受支持的方式,而
只是特定实现的工件


下面是一个示例,
FOR
循环中产生问题

此示例不适用于桌面上名为
test.bat
的文件:

@echo off
for /F "delims=" %%A in ('type C:\Users\%username%\Desktop\test.bat') do (
    ::echo hello>C:\Users\%username%\Desktop\text.txt
)
pause
虽然此示例将正确用作注释:

@echo off
for /F "delims=" %%A in ('type C:\Users\%username%\Desktop\test.bat') do (
    REM echo hello>C:\Users\%username%\Desktop\text.txt
)
pause
问题似乎是在尝试将输出重定向到文件时。我最好的猜测是它将
解释为一个转义标签,名为
:echo

带有REM的注释
REM
可以标记一个完整的行,如果不是第一个标记的末尾,也可以在行的末尾标记一个多行插入符号

REM This is a comment, the caret is ignored^
echo This line is printed

REM This_is_a_comment_the_caret_appends_the_next_line^
echo This line is part of the remark
REM后跟一些字符:\/=的工作原理有点不同,它不注释符号AND,所以可以将其用作内联注释

echo First & REM. This is a comment & echo second
但为了避免现有文件出现问题,如
REM
REM.bat
REM;。bat
只能使用修改过的变体

REM^;<space>Comment
标签和注释标签
在括号中有一个特殊的逻辑块。
它们总是跨越两行。
因此,不建议将它们用于括号块,因为它们通常是导致语法错误的原因

ECHO打开时
会显示
REM
行,但没有用
注释的行:

两者都不能真正注释掉行的其余部分,因此简单的
%~
将导致语法错误

REM This comment will result in an error %~ ...
但是REM能够在早期阶段停止批处理解析器,甚至在特殊字符阶段完成之前

@echo ON
REM This caret ^ is visible
&REM或&::在命令行末尾添加注释。 这种方法之所以有效,是因为“&”在同一行上引入了一个新命令

带有百分号%的注释=注释=% 存在带有百分号的注释样式

实际上,这些都是变量,但它们被扩展为零。
但优点是,即使没有
&

等号确保这样的变量不存在

echo Mytest
set "var=3"     %= This is a comment in the same line=%
建议批处理宏使用百分比样式,因为它不会更改运行时行为,因为定义宏时注释将被删除

set $test=(%\n%
%=Start of code=% ^
echo myMacro%\n%
)

另一种方法是将注释表示为变量扩展,该变量扩展总是扩展为零

变量名不能包含
=
,除了像
%=ExitCode%
%=C:%
。任何变量名都不能在第一个位置后包含
=
。因此,我有时使用以下方法在括号中包含注释:

::This comment hack is not always safe within parentheses.
(
  %= This comment hack is always safe, even within parentheses =%
)
这也是一种很好的合并在线评论的方法

dir junk >nul 2>&1 && %= If found =% echo found || %= else =% echo not found
前导的
=
是不必要的,但我喜欢if的对称性

有两个限制:

1) 注释不能包含
%


2) 在我意识到我可以使用标签
::
来发表评论并注释掉code
REM
之后,评论不能包含

。如前所述,在
()
阻塞代码中使用双冒号可能会导致问题,但我发现了一种解决方法,即在标签
::
空格之间交替使用

它不像
REM
那样难看,实际上为代码增加了一点风格

因此,在代码块之外,我使用
,在它们内部,我在
之间交替使用

@echo off 
echo This is an example of an %= Inline Comment =% in the middle of a line.
顺便说一句,对于大量的注释,比如批处理文件的头,您可以通过简单地
goto
忽略注释来完全避免特殊的命令和字符。这让您可以使用任何想要的标记方法或样式,尽管事实上如果
CMD
实际尝试处理这些行,它会发出嘶嘶声

@echo off
goto :TopOfCode

=======================================================================
COOLCODE.BAT

Useage:
  COOLCODE [/?] | [ [/a][/c:[##][a][b][c]] INPUTFILE OUTPUTFILE ]

Switches:
       /?    - This menu
       /a    - Some option
       /c:## - Where ## is which line number to begin the processing at.
         :a  - Some optional method of processing
         :b  - A third option for processing
         :c  - A forth option
  INPUTFILE  - The file to process.
  OUTPUTFILE - Store results here.

 Notes:
   Bla bla bla.

:TopOfCode
CODE
.
.
.

使用你想要的任何符号,
*
@
,等等。

詹姆斯·K,很抱歉,我说的话有相当一部分是错的。我做的测试如下:

@ECHO OFF
(
  :: But
   : neither
  :: does
   : this
  :: also.
)
@ECHO OFF
(
   : But
   : neither
   : does
   : this
   : cause
   : problems.
)
这符合您对Alternative的描述,但失败的原因是“)此时意外。”错误消息

我今天做了一些进一步的测试,发现Alternate不是关键,但它似乎有偶数行,一行中没有任何两行以双冒号(:)开头,也没有以双冒号结尾。考虑以下事项:

@ECHO OFF
(
  :: But
   : neither
  :: does
   : this
  :: also.
)
@ECHO OFF
(
   : But
   : neither
   : does
   : this
   : cause
   : problems.
)
这管用

也可考虑:

@ECHO OFF
(
   : Test1
   : Test2
   : Test3
   : Test4
   : Test5
   ECHO.
)
以命令结尾时,注释数为偶数的规则似乎不适用

不幸的是,这只是松鼠足够,我不确定我想使用它。
::(^
this is a multiline^
comment... inside a null label!^
dont forget the ^caret at the end-of-line^
to assure continuity of text^ 
)
@echo off

for %%i in ("dummy loop") do (

  :: This works: ONE comment line only, followed by a DIFFERENT, NONBLANK line.
  date /t

  REM If you followed a :: line directly with another one, the *2nd* one
  REM would generate a spurious "The system cannot find the drive specified."
  REM error message and potentially execute commands inside the comment.
  REM In the following - commented-out - example, file "out.txt" would be
  REM created (as an empty file), and the ECHO command would execute.
  REM   :: 1st line
  REM   :: 2nd line > out.txt & echo HERE

  REM NOTE: If :: were used in the 2 cases explained below, the FOR statement
  REM would *break altogether*, reporting:
  REM  1st case: "The syntax of the command is incorrect."
  REM  2nd case: ") was unexpected at this time."

  REM Because the next line is *blank*, :: would NOT work here.

  REM Because this is the *last line* in the block, :: would NOT work here.
)
@echo off

echo hello
(
   :;(^
   this^
   is^
   a^
   comment^
   )
   :;
)
   :;^
   this^
   is^
   a^
   comment
   :;
) 
@echo off

(
echo hello
:;
:; comment
:; comment
:;
)
echo hello

@echo off

(
echo hello & :;yes
echo hello & :;yes
:;
)

echo hello
@echo off 
echo This is an example of an %= Inline Comment =% in the middle of a line.
@echo off
(
echo hello
;this is a comment 2> nul
;this is another comment  2> nul
)
@echo off
(
echo hello
)

(this is a comment
this is a comment
this is a comment