在DOS批处理文件中::(双冒号)是什么意思?

问题描述 投票:40回答:5

我发现这个程序http://baiyunmanor.com/blog/work/get-current-date-time-in-dos-batch-file/

但我不知道该怎么回事

:: datetime.bat

到底意味着什么?

batch-file dos
5个回答
54
投票

::是一个标签(也是,不准确的,在评论标签的野外),在实践中,可以像REM一样被视为评论,因为它是一个“不可转动的”标签。

不过,REM::之间存在一些差异。主要是:

  • 随着ECHO ON显示REM线,但没有一行评论::
  • 一个::可以执行一个行末端插入符号(也就是说,从^开始的行尾的::使下一行也是注释): :: This is a comment^ echo but watch out because this line is a comment too
  • 标签和::有一个特殊的逻辑,可能会导致括号块出现问题 - 在( )中使用它们时要小心。例: for %%D in (hi) do ( echo Before... :: My comment :: Some other comment echo After... ) 输出: Before ... The system cannot find the drive specified. After...

53
投票

以双冒号开头的行表示命令处理器忽略的无效标签,因此可用于插入注释。由于无法跟踪的原因,许多人使用::在批处理文件中插入注释,但您必须意识到其使用中存在一些陷阱,这些陷阱在Koterpillar的答案中给出的链接中有所描述。似乎第一次使用::而不是REM命令的目的是加速在慢速机器(即:软盘)中执行批处理文件,但这个原因并不是使用双冒号的有效理由,因为许多几年前。

任何包含无效标签的行都将被命令处理器忽略,您几乎可以使用任何特殊字符来生成无效标签。例如:

@echo off

:~ This is a comment
:` This is a comment
:! This is a comment
:@ This is a comment
:# This is a comment
:$ This is a comment
:% This is a comment
:^ This is a comment
:& This is a comment
:* This is a comment
:( This is a comment
:) This is a comment
:_ This is a comment
:- This is a comment
:+ This is a comment
:= This is a comment
:{ This is a comment
:} This is a comment
:[ This is a comment
:] This is a comment
:| This is a comment
:\ This is a comment
:: This is a comment
:; This is a comment
:" This is a comment
:' This is a comment
:< This is a comment
:> This is a comment
:, This is a comment
:. This is a comment
:? This is a comment
:/ This is a comment

echo OK

换句话说:如果你想插入注释而你不想使用REM命令(虽然我想不出任何理由这样做),你有32个可能的字符组合来做到这一点。为什么你应该使用这个:::?仅仅因为35年前编写的一些旧程序呢?


15
投票

以冒号开头的行是一个标签,您可以使用goto跳转到该标签:

goto end
:end

以双冒号开头的行是一个标签,除非您不能,甚至意外地跳转到它:

goto :end REM this doesn't work
::end

因此,双冒号用于评论行。

资料来源:http://www.robvanderwoude.com/comments.php


7
投票

acdcjunior所述,标签和::有一个特殊的逻辑,可能会导致括号内的问题

这里有几个样本

样品1

IF 1==1 (
  ::
)

样品1的输出

) was unexpected at this time.

样本2

IF 1==1 (
  ::
  ::
)

样本2的输出

The system cannot find the drive specified.

2
投票

冒号(:)是标签标记,可用于获取指令。

有些人使用:作为评论,所以双冒号只是一种风格的REM语句

© www.soinside.com 2019 - 2024. All rights reserved.