61

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?

jeb
  • 70,992
  • 15
  • 159
  • 202
user310291
  • 33,174
  • 71
  • 241
  • 439
  • Beware of using :: as rem in batch programs that are called by Task Scheduler. When writting and testing a batch program it worked as expected from the command prompt, but when running the same thing from Task Scheduler it throws error: 'if' is not recognized as an internal or external command, operable program or batch file. – user2954660 Apr 11 '20 at 16:09

6 Answers6

77

:: 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...
    
acdcjunior
  • 114,460
  • 30
  • 289
  • 276
  • 9
    When you cite that `::` "is also called _comment label_" you should include a link to the _official_ source where you got that cite; otherwise you are contributing to spread _popular beliefs_ that may be wrong. – Aacini Nov 07 '15 at 19:07
  • 1
    The most important difference is the special logic inside of parenthesis blocks. Would be great if you could add a resource for more information on that. – McK Jan 27 '16 at 09:18
  • 4
    The special handling in blocks is described at [SO:goto command not working](http://stackoverflow.com/a/4006006/463115) and more about [SO:Which comment style should I use in batch files?](http://stackoverflow.com/a/12408045/463115) – jeb Feb 10 '16 at 10:09
  • 2
    Wow, I used :: in a for loop and it took me a whole day to reach this post. The behavior of double colon in () in my case seemed undetermined. If it is just by itself in for(), it either crashes execution or says "...cannot find specified drive" (to that effect, different language) while "chdir" clearly showed my "cd" worked. – Sean Mar 17 '16 at 06:17
64

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?

Aacini
  • 59,374
  • 12
  • 63
  • 94
  • 1
    At all you are right (as nearly always), but I can't see why you made this list? `"~``!@#$%%*()_-{}[]\'.?/^&|<>` are legal labels, only `+=,;:` can't be used as labels. – jeb Oct 20 '15 at 09:29
  • @jeb: I just want that the reader realize that the use of `::` to insert "comments" have not reasonable basis. – Aacini Oct 20 '15 at 18:23
  • 9
    Since you can't think of reasons I'll give you two: **readability** (the `::` stands out as non-alphabetic in what's mostly alphabetic characters in a batch script ... and if you come from C++ where `//` is a single line comment, `::` kinda makes sense for the same purpose) and **saving space** (if you've ever hit the upper size limit - somewhere beyond 60 kB - with a script you'll appreciate being able to save some bytes ... and yes, there are also good reasons for such big monolithic scripts). – 0xC0000022L Jan 20 '16 at 08:13
  • 3
    @0xC0000022L: It seems that you didn't read my post nor my comment. If you want **readability** and you come from C++, then `://` makes _much more sense_ to insert a single line comment! From this point of view, why don't use `:'` like in VBScript, or `:#` like in PowerShell and several other script languages, or `:--` like in SQL, or `: – Aacini Feb 08 '16 at 17:54
  • 7
    @Aacini: I did read it and it *prompted* me to comment. And out of your five alternatives **all** require more movement of my fingers than `::` and even worse, three out of the five use more than two characters. Now while that's a *logical* explanation, I can't help with "reasonable" or "acceptable", because these are entirely subjective. But out of those alternatives `://` would be the most acceptable to me, if I didn't also have the option to use `::`. – 0xC0000022L Feb 09 '16 at 21:20
  • 4
    `REM` errors out on comments like `REM /?a=1` ("`1 was unexpected at this time.`") whereas `::` seems to handle them just fine. – genpfault Jan 02 '18 at 21:30
18

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

Koterpillar
  • 7,032
  • 2
  • 23
  • 37
8

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.
Community
  • 1
  • 1
Seva Parfenov
  • 961
  • 1
  • 8
  • 6
3

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

Preet Sangha
  • 61,126
  • 17
  • 134
  • 202
1

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.

ecm
  • 2,092
  • 2
  • 15
  • 22
  • Sorry that's wrong, at least since MS-DOS6.22. `REM` is even *stronger* than label comments. The parser stops parsing a line if `REM` is used, but not for labels – jeb Apr 08 '21 at 06:23
  • @jeb: I just tried on FreeCOM and `rem > a` does create the file `a`. So at least FreeDOS does do redirection for the `rem` command. Later today I can test on my MS-DOS machine. – ecm Apr 08 '21 at 07:14
  • @jeb: Just remembered I can try it [on pcjs](https://www.pcjs.org/machines/pcx86/ibm/5170/vga/cdrom/). Load the MS-DOS 6.22 diskette, restart, exit setup, do `rem > a`, then `dir a` lists the file created from this redirection. – ecm Apr 08 '21 at 07:21
  • As said, it works on MS-DOS 6.22 (just tested in virtual box), but since then `REM` doesn't redirect anymore. The question was about `cmd.exe`. Once upon a time: Your answer *was* correct :-) – jeb Apr 08 '21 at 07:39
  • @jeb: The question title includes the term "DOS batch files". And it is tagged #dos. Forgive me for giving a reply concerning DOS. (I'm fairly sure my answer is also correct for MS-DOS 7.xx as bundled with MS Windows, so the "since then" isn't entirely accurate either. Perhaps NT's `cmd.exe` does work as you suggest.) – ecm Apr 08 '21 at 07:44
  • 1
    You are right, I'm wrong, even with Window98 the REM redirection still works (also tested). But I edited the question to be more accurate, as the sample code expected at least Windows NT – jeb Apr 08 '21 at 07:50
  • 1
    @jeb: It doesn't expect "at least Windows NT", it is intended "for Win9x/ME/NT/W2K/XP". In MS Windows 4.x (the 95/98/Me line) batch files are actually interpreted by DOS's `command.com` and, as you tested, this shell does do redirection on `rem` lines. So even if there is no reason to prefer `::` over `rem` for MSW NT with `cmd.exe` then the question still includes "Win9x/ME" where there is this difference. – ecm Apr 08 '21 at 07:57