What does :: (double colon) mean in DOS batch files?

Batch File

Batch File Problem Overview


I found this program 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

But I don't know what does the line

:: datetime.bat

at the end mean?

Batch File Solutions


Solution 1 - Batch File

:: is a label (inaccurately also known as comment label) can be, in practice, considered a comment just as REM is, because it is an "un-goto-able" label.

There are some differences between REM and ::, though. The main ones are:

  • With ECHO ON a REM line is shown but not a line commented with ::

  • A :: can execute a line end caret (that is, a ^ at the end of a line starting with :: makes the next line also a comment):

     :: This is a comment^
     echo but watch out because this line is a comment too
    
  • Labels and :: have a special logic and can cause problems in parentheses blocks - take care when using them inside ( ). Example:

     for %%D in (hi) do (
         echo Before...
         :: My comment
         :: Some other comment
         echo After...
     )
    

    Outputs:

     Before ...
     The system cannot find the drive specified.
     After...
    

Solution 2 - Batch File

A line that start in double colon represent an invalid label that is ignored by the command processor, so it may be used to insert a comment. For reasons that can't be traced, many people use :: to insert comments in Batch files, but you must be aware that there are several pitfalls in its use that are described in the link given in Koterpillar's answer. It seems that the first use of :: instead of REM command was with the purpose to speed up the execution of Batch files in slow machines (ie: floppy disks), but that reason is not a valid justification for the use of double colon since many years ago.

Any line that contain an invalid label will be ignored by the command processor and you may use practically any special character to generate an invalid label. For example:

@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

In other words: if you want to insert a comment and you want not to use REM command (although I can't think of any reason to do so), you have 32 possible character combinations to do so. Why you should use precisely this one: ::? Just because some old programs written 35 years ago did it?

Solution 3 - Batch File

A line starting with a colon is a label which you can jump to with goto:

goto end
:end

A line starting with a double colon is a label, except you can't, even accidentally, jump to it:

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

Thus, double colon is used to comment lines out.

Source: http://www.robvanderwoude.com/comments.php

Solution 4 - Batch File

As mentioned by acdcjunior Labels and :: have a special logic and can cause problems in parenthesis blocks

Here are couple of samples

Sample 1

IF 1==1 (
  ::
)

Output of sample 1

) was unexpected at this time.

Sample 2

IF 1==1 (
  ::
  ::
)

Output of sample 2

The system cannot find the drive specified.

Solution 5 - Batch File

The colon (:) is a label marker and can be used for got instructions.

Some people use : as a comment too so a double colon is simply a stylistic REM statement

Solution 6 - Batch File

If you use the traditional REM command to comment out a line in a DOS batch script then any output redirection in the comment is still done. For example, consider this script:

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

This script will truncate b.txt even though the line appears to be "commented out".

Using an invalid label prefix like the double colon will disable any redirection.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser310291View Question on Stackoverflow
Solution 1 - Batch FileacdcjuniorView Answer on Stackoverflow
Solution 2 - Batch FileAaciniView Answer on Stackoverflow
Solution 3 - Batch FileKoterpillarView Answer on Stackoverflow
Solution 4 - Batch FileSeva ParfenovView Answer on Stackoverflow
Solution 5 - Batch FilePreet SanghaView Answer on Stackoverflow
Solution 6 - Batch FileecmView Answer on Stackoverflow