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

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

问题描述

我已经写了一些批处理文件,我跑进本用户指南,其中有是相当翔实。有一件事这表明我是行可以注释不仅仅是 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:

在批量code注释可以通过使用双冒号进行,这比使用REM命令,因为标签被重定向符号之前处理好。 ::<&句话GT; 不会造成问题,但 REM<&句话GT; 产生错误。

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;博士: REM 是文档和支持的方式嵌入在批处理文件注释。

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

本质上是一个空白标签永远不能跳楼,而 REM 是一个实际的命令只是什么都不做。在两种情况下(至少在Windows 7中)不重定向操作符的presence出现问题了。

:: 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.

修改

这里就是循环产生问题的例子。

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\test.bat') do (
    ::echo hello>C:\Users\%username%\Desktop\text.txt
)
pause

虽然这个例子将作为正确注释:

While this example will work as a comment correctly:

@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

这个问题似乎试图输出重定向到一个文件时要。我最好的猜测是,这是间preting 作为称为逃脱标签:回声

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

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