我应该在批处理文件中使用哪种注释样式? [英] Which comment style should I use in batch files?

查看:22
本文介绍了我应该在批处理文件中使用哪种注释样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在编写一些批处理文件,但遇到了本用户指南,其中包含信息量很大.它向我展示的一件事是,行不仅可以用 REM 注释,还可以用 :: 注释.它说:

I've been writing some batch files, and I ran into this user guide, which has been quite informative. One thing it showed me was that lines can be commented not just with REM, but also with ::. It says:

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

Comments in batch code can be made by using a double-colon, this is better than using the REM command because labels are processed before redirection symbols. ::<remark> causes no problems but rem <remark> produces errors.

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

Why then, do most guides and examples I see use the REM command? Does :: work on all versions of Windows?

推荐答案

tl;dr: REM 是在批处理文件中嵌入注释的记录和支持方式.

tl;dr: REM is the documented and supported way to embed comments in batch files.

:: 本质上是一个永远无法跳转到的空白标签,而 REM 是一个实际的命令,什么也不做.在这两种情况下(至少在 Windows 7 上),重定向运算符的存在都不会导致问题.

:: is essentially a blank label that can never be jumped to, whereas REM is an actual command that just does nothing. In neither case (at least on Windows 7) does the presence of redirection operators cause a problem.

然而,众所周知,:: 在某些情况下会在块中行为异常,被解析为某种驱动器号而不是标签.我对确切的位置有点模糊,但仅此一项就足以让我专门使用 REM.这是在批处理文件中嵌入注释的记录和支持的方式,而 :: 只是特定实现的工件.

However, :: is known to misbehave in blocks under certain circumstances, being parsed not as a label but as some sort of drive letter. I'm a little fuzzy on where exactly but that alone is enough to make me use REM exclusively. It's the documented and supported way to embed comments in batch files whereas :: is merely an artifact of a particular implementation.

这是一个示例,其中 ::FOR 循环中产生问题.

Here is an example where :: produces a problem in a FOR loop.

这个例子将不能在你桌面上名为 test.bat 的文件中工作:

This example will not work in a file called test.bat on your desktop:

@echo off
for /F "delims=" %%A in ('type C:Users\%username%Desktop	est.bat') do (
    ::echo hello>C:Users\%username%Desktop	ext.txt
)
pause

虽然此示例将正确用作注释:

While this example will work as a comment correctly:

@echo off
for /F "delims=" %%A in ('type C:Users\%username%Desktop	est.bat') do (
    REM echo hello>C:Users\%username%Desktop	ext.txt
)
pause

问题似乎出在尝试将输出重定向到文件时.我最好的猜测是它将 :: 解释为一个名为 :echo 的转义标签.

The problem appears to be when trying to redirect output into a file. My best guess is that it is interpreting :: as an escaped label called :echo.

这篇关于我应该在批处理文件中使用哪种注释样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆