64

我找到了这个程序web.archive.org:http://baiyunmanor.com/blog/work/get-current-date-time-in-dos-batch-file/

::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::
:: This uses Windows Scripting Host to set variables
:: to the current date/time/day/day_number
:: for Win9x/ME/NT/W2K/XP etc
:: Thanks go to Todd Vargo for his scripting
::::::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off
set TmpFile=”%temp%.\tmp.vbs”
echo> %TmpFile% n=Now
echo>>%TmpFile% With WScript
echo>>%TmpFile% .Echo “set year=” + CStr(Year(n))
echo>>%TmpFile% .Echo “set yr=” + Right(Year(n),2)
...
cscript //nologo “%temp%.\tmp.vbs” > “%temp%.\tmp.bat”
call “%temp%.\tmp.bat”
...
echo date F [ddmmyy] [%day%%month%%yr%]
:: datetime.bat

但我不知道这条线是什么

:: datetime.bat

到底是什么意思?

4

6 回答 6

81

::是一个标签(不准确地也称为评论标签)在实践中可以被认为是一个评论REM,因为它是一个“不可跳转”的标签。

但是,和之间存在一些差异。主要有:REM::

  • ECHO ON显示一行REM但没有注释的行::

  • A::可以执行一个行尾插入符(即,^在行尾以 开头的 a::使下一行成为注释):

     :: 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...
    
于 2013-05-19T08:02:30.147 回答
65

以双冒号开头的行表示命令处理器忽略的无效标签,因此它用于插入注释。由于无法追踪的原因,许多人::习惯在批处理文件中插入注释,但您必须注意,在 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 年前写的老程序做到了?

于 2013-05-19T21:56:56.580 回答
19

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

goto end
:end

以双冒号开头的行是一个标签,除了你不能,甚至不小心,跳到它:

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

因此,双冒号用于注释行。

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

于 2013-05-19T08:02:38.070 回答
8

正如acdcjunior 标签所提到的,::具有特殊的逻辑,可能会导致括号块中的问题

这是几个示例

样品 1

IF 1==1 (
  ::
)

样本 1 的输出

) was unexpected at this time.

样品 2

IF 1==1 (
  ::
  ::
)

样本 2 的输出

The system cannot find the drive specified.
于 2016-02-09T07:38:25.067 回答
3

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

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

于 2013-05-19T08:02:48.977 回答
1

如果您使用传统REM命令注释掉 DOS 批处理脚本中的一行,那么注释中的任何输出重定向仍然会完成。例如,考虑这个脚本:

echo 1 > a.txt
rem echo 2 > b.txt
echo 3 > c.txt

b.txt即使该行似乎已“注释掉”,此脚本也会截断。

使用像双冒号这样的无效标签前缀将禁用任何重定向。

于 2021-04-07T20:59:44.130 回答