964

I have a batch file that runs several python scripts that do table modifications.

  1. I want to have users comment out the 1-2 python scripts that they don't want to run, rather than removing them from the batch file (so the next user knows these scripts exist as options!)

  2. I also want to add comments to bring to their attention specifically the variables they need to update in the Batch file before they run it. I see that I can use REM. But it looks like that's more for updating the user with progress after they've run it.

Is there a syntax for more appropriately adding a comment?

T.Todua
  • 44,747
  • 17
  • 195
  • 185
user1397044
  • 10,099
  • 6
  • 17
  • 16

11 Answers11

1023

Use :: or REM

::   commenttttttttttt
REM  commenttttttttttt

BUT (as people noted):

  • :: doesn't work inline; add & character:
    your commands here & :: commenttttttttttt
  • Inside nested parts (IF/ELSE, FOR loops, etc...) :: should be followed with normal line, otherwise it gives error (use REM there).
  • :: may also fail within setlocal ENABLEDELAYEDEXPANSION
T.Todua
  • 44,747
  • 17
  • 195
  • 185
  • 79
    The double colon :: Is the cleanest .bat comment there is. And it can be used at the start or middle of a line! – ATSiem Aug 23 '13 at 19:23
  • 10
    Didn't work for me when done inline like this. `cd "C:\Folder" ::this throws a syntax error` – Shaun Rowan Oct 27 '14 at 14:48
  • 3
    Also fails in `git add :: blah this is not a comment` with "fatal: pathspec 'blah' did not match any files" – Bob Stein May 02 '15 at 16:01
  • this is meant for **`cmd`** mainly.. i dont know if it works for other software (except **command prompt**).. – T.Todua Dec 05 '15 at 23:21
  • This should be the answer - +1 – Kolob Canyon Mar 24 '16 at 03:12
  • 21
    For a comment on the same line as the example illustrates, you need to add `&` between the code and the comment `::`. To illustrate, open cmd prompt and run `dir ::blah` which doesn't list the contents of `.` and compare with `dir & ::blah`, which does – Rado Apr 08 '16 at 19:41
  • 1
    @Rado this is essential! If you don't know it, `::`-comments will produce an error. Thus, I am using `rem` instead which seemingly doesn't need a `&` – phil294 Apr 24 '16 at 15:42
  • Thankd @tazotodua The double colon :: Is the cleanest .bat comment there is. And it can be used at the start or middle of a line! – shridutt kothari Apr 28 '16 at 09:57
  • 20
    Warning, using `::` will bug scripts with `setlocal ENABLEDELAYEDEXPANSION` and `for` – Azevedo Jan 24 '17 at 21:23
  • 1
    The double colon can also cause problems inside an IF block. Problems like ') was unexpected at this time', or 'The system cannot find the drive specified.' – david Sep 15 '17 at 00:48
  • 7
    [Examples](http://www.robvanderwoude.com/comments.php) of how using `::` labels as comments can cause difficult-to-anticipate errors. Calling this clean is a bit misleading @ATSiem – Jean-François Corbett Mar 02 '18 at 08:31
  • @Jean-FrançoisCorbett btw, seems this question to be old than another. so which one you mean in duplicate? – T.Todua Mar 02 '18 at 09:16
  • @T.Todua Doesn't really matter which is older or newer. They both ask about pretty much the same thing, and get very similar answers. So the one should be closed as duplicate of the other. The other one, though, has an accepted answer that is better than this one's accepted answer, so I think this one should be closed and point to the other. – Jean-François Corbett Mar 02 '18 at 09:28
  • There aren't any problems with `::` and delayed expansion – jeb Nov 08 '18 at 15:20
  • @jeb and why #Azevedo's comment has 17 upvotes then? – T.Todua Nov 08 '18 at 15:26
  • 2
    The amount of upvotes makes @Azevedo comment not correct. There is only a problem inside of parenthesis blocks with `::`, see also [Unexpected “The system cannot find the drive specified.”](https://stackoverflow.com/a/19847135/463115) – jeb Nov 08 '18 at 15:32
  • I've tested `::` on my scripts. It may may bug some scripts as I mentioned. Ok. go ahead and use `::` because it "looks cool" – Azevedo Nov 08 '18 at 19:59
  • @Azevedo Let's talk here [chat](https://chat.stackoverflow.com/rooms/183550/room-for-jeb-and-azevedo) – jeb Nov 13 '18 at 10:47
944

The rem command is indeed for comments. It doesn't inherently update anyone after running the script. Some script authors might use it that way instead of echo, though, because by default the batch interpreter will print out each command before it's processed. Since rem commands don't do anything, it's safe to print them without side effects. To avoid printing a command, prefix it with @, or, to apply that setting throughout the program, run @echo off. (It's echo off to avoid printing further commands; the @ is to avoid printing that command prior to the echo setting taking effect.)

So, in your batch file, you might use this:

@echo off
REM To skip the following Python commands, put "REM" before them:
python foo.py
python bar.py
Rob Kennedy
  • 156,531
  • 20
  • 258
  • 446
  • 434
    you can also use two colons "::". It is one of two ways of adding remarks into the batch file without displaying or executing that line when the batch file is run. Unlike REM this line will not show regardless if ECHO off is in the batch file. – Brent81 Nov 06 '13 at 02:52
  • 12
    @Brent81 I feel this should be the correct answer. When using syntax highlighting the REM command does not actually highlight the text as "commented out". – Automatico May 29 '14 at 08:42
  • 18
    The double-colon is an "invalid label"; here's an article that explains it, and why it's performance may be better than REM (especially on floppy disks!), and it won't work in indented code blocks (only on the first character): http://www.robvanderwoude.com/comments.php – Michael Paulukonis Sep 12 '14 at 13:04
  • 4
    Interesting. I though REM was the 'whole line comment' and the double colon was the 'inline comment', like `REM This whole line is a comment` `@echo off :: This comment is inline` – Richard Smith Jun 11 '17 at 04:55
  • @Alhadis, Perl's Configure script is written in Bourne shell, not Windows batch, so how is it relevant here? Furthermore, the colon command being valid Bourne syntax is not a "bonus side effect"; it's precisely _why_ it's used in that _Bourne script_. It's used in favor of `#` for comments because `#`, evidently, might not be a valid comment designator in _all_ the shells Configure is intended to support; see line 3. – Rob Kennedy Jul 18 '19 at 16:22
  • @RobKennedy I was high when I wrote that, please ignore –  Jul 19 '19 at 12:53
51

No, plain old batch files use REM as a comment. ECHO is the command that prints something on the screen.

To "comment out" sections of the file you could use GOTO. An example of all these commands/techniques:

REM it starts here the section below can be safely erased once the file is customised
ECHO Hey you need to edit this file before running it!  Check the instructions inside
ECHO Now press ctrl-c to interrupt execution or enter to continue
PAUSE
REM erase the section above once you have customised the file
python executed1.py
ECHO Skipping some stuff now
GOTO End
python skipped1.py
python skipped2.py
:END
python executed2.py

What can I say? batch files are a relic of times long gone, they're clunky and ugly.

You can read more on this website.

EDIT: modified the example a bit to have it contain the elements you are apparently looking for.

fvu
  • 31,143
  • 5
  • 57
  • 77
34

The :: instead of REM was preferably used in the days that computers weren't very fast. REM'ed line are read and then ingnored. ::'ed line are ignored all the way. This could speed up your code in "the old days". Further more after a REM you need a space, after :: you don't.

And as said in the first comment: you can add info to any line you feel the need to

SET DATETIME=%DTS:~0,8%-%DTS:~8,6% ::Makes YYYYMMDD-HHMMSS

As for the skipping of parts. Putting REM in front of every line can be rather time consuming. As mentioned using GOTO to skip parts is an easy way to skip large pieces of code. Be sure to set a :LABEL at the point you want the code to continue.

SOME CODE

GOTO LABEL  ::REM OUT THIS LINE TO EXECUTE THE CODE BETWEEN THIS GOTO AND :LABEL

SOME CODE TO SKIP
.
LAST LINE OF CODE TO SKIP

:LABEL
CODE TO EXECUTE
Kees
  • 551
  • 5
  • 3
  • 1
    How can a double-colon line be ignored without being read? Mustn't the interpreter first read the line before recognizing it as a double-colon line? – Rob Kennedy Nov 06 '13 at 13:50
  • 2
    @RobKennedy as I read the answer, it does not read anything after the ::, where everything after REM is read. – James Jenkins Mar 12 '14 at 15:44
  • 5
    If the command interpreter *stops reading*, @James, then it would never execute any more of the file. It obviously needs to continue reading to discover where the comment ends and where next line of the file begins. It has to do that with `::` and `rem` equally. – Rob Kennedy Mar 12 '14 at 15:52
  • 2
    @RobKennedy, I also thought it was implied that it only applied to the line the command was on. – James Jenkins Mar 12 '14 at 15:56
  • Either way, the interpreter has to read the line. If it's `rem` it also has to execute it, but if it's `::` it doesn't. – Ken Williams Jun 15 '16 at 14:00
  • 7
    @RobKennedy it does **read** but it does not **parse** (or process) what it reads, it just skips until the next `\n` and then starts parsing again [citation needed] – xDaizu Oct 31 '16 at 12:29
  • And when it /does/ parse a label, (as it does sometimes inside IF blocks), you get errors like: ') was unexpected at this time', or 'The system cannot find the drive specified.' – david Sep 15 '17 at 00:50
  • 1
    @JamesJenkins The voice of reason. (And reading comments properly!!) – SteveCinq Jul 09 '18 at 03:50
28

Multi line comments

If there are large number of lines you want to comment out then it will be better if you can make multi line comments rather than commenting out every line.

See this post by Rob van der Woude on comment blocks:

The batch language doesn't have comment blocks, though there are ways to accomplish the effect.

GOTO EndComment1
This line is comment.
And so is this line.
And this one...
:EndComment1

You can use GOTO Label and :Label for making block comments.

Or, If the comment block appears at the end of the batch file, you can write EXIT at end of code and then any number of comments for your understanding.

@ECHO OFF
REM Do something
  •
  •
REM End of code; use GOTO:EOF instead of EXIT for Windows NT and later
EXIT

Start of comment block at end of batch file
This line is comment.
And so is this line.
And this one...
Martijn Pieters
  • 889,049
  • 245
  • 3,507
  • 2,997
Somnath Muluk
  • 46,917
  • 28
  • 204
  • 217
  • this is often used in hybrid batch & VBS/JS scripts, although they used if `if` instead of `goto` http://stackoverflow.com/q/9074476/995714 http://ss64.com/vb/syntax-hybrid.html http://stackoverflow.com/a/34512715/995714 – phuclv Jan 08 '17 at 06:45
14

Putting comments on the same line with commands: use & :: comment

color C          & :: set red font color
echo IMPORTANT INFORMATION
color            & :: reset the color to default

Explanation:

& separates two commands, so in this case color C is the first command and :: set red font color is the second one.


Important:

This statement with comment looks intuitively correct:

goto error1         :: handling the error

but it is not a valid use of the comment. It works only because goto ignores all arguments past the first one. The proof is easy, this goto will not fail either:

goto error1 handling the error

But similar attempt

color 17            :: grey on blue

fails executing the command due to 4 arguments unknown to the color command: ::, grey, on, blue.

It will only work as:

color 17     &      :: grey on blue

So the ampersand is inevitable.

Community
  • 1
  • 1
miroxlav
  • 10,935
  • 5
  • 48
  • 86
6

You can comment something out using :: or REM:

your commands here
:: commenttttttttttt

or

your commands here
REM  commenttttttttttt

 

To do it on the same line as a command, you must add an ampersand:

your commands here      & ::  commenttttttttttt

or

your commands here      & REM  commenttttttttttt

 

Note:

  • Using :: in nested logic (IF-ELSE, FOR loops, etc...) will cause an error. In those cases, use REM instead.
Pikamander2
  • 4,767
  • 3
  • 37
  • 53
  • This is based on T.Todua's answer. I felt that their answer was good but left out some important details, but they rolled back my edit, so I'm turning my revision into its own answer. – Pikamander2 Sep 21 '18 at 05:25
  • Pikamander2, your revision (also, pointed out by user `@Rado` was merged in answer that time and thanks for that, just I preferred another styling for answer and used different styling. – T.Todua Nov 08 '18 at 12:31
2

You can add comments to the end of a batch file with this syntax:

@echo off
:: Start of code
...
:: End of code

(I am a comment
So I am!
This can be only at the end of batch files

Just make sure you never use a closing parentheses.

Attributions: Leo Guttirez Ramirez on https://www.robvanderwoude.com/comments.php

programmer365
  • 12,641
  • 3
  • 7
  • 28
1

This is an old topic and I'd like to add my understanding here to expand the knowledge of this interesting topic.

The key difference between REM and :: is:

REM is a command itself, while :: is NOT.

We can treat :: as a token that as soon as CMD parser encounters the first non-blank space in a line is this :: token, it will just skip the whole line and read next line. That's why REM should be followed by at least a blank space to be able to function as a comment for the line, while :: does not need any blank space behind it.

That REM is a command itself can be best understood from the following FOR syntax

The basic FOR syntax is as follows

FOR %v in (set) DO <Command> [command param] 

here <Command> can be any valid command So we can write the following valid command line as rem is a command

FOR %i in (1,2,3) DO rem echo %i

However, we CANNOT write the following line as :: is not a command

FOR %i in (1,2,3) DO :: echo %i
jyao
  • 1,326
  • 2
  • 16
  • 25
  • 4
    technically, `::` is an (invalid) label (which explains it's behavior) and so should not be used (although it's still quite common) – Stephan Feb 13 '19 at 15:14
0

You can use :: or rem for comments.

When commenting, use :: as it's 3 times faster. An example is shown here

Only if comments are in if, use rem, as the colons could make errors, because they are a label.

Anic17
  • 501
  • 3
  • 12
0

Commenting a line

For commenting line use REM or :: though :: might fail inside brackets

within delayed expansion lines starting with !<delimiter> will be ignored so this can be used for comments:

@echo off

setlocal enableDelayedExpansion

echo delayed expansion activated
!;delayed expansion commented line
echo end of the demonstration

Comment at the end of line

For comments at the end of line you can again use rem and :: combined with &:

echo --- &:: comment (should not be the last line in the script)
echo --- &rem comment

Commenting at the end of file

As noting will be parsed after the exit command you can use it to put comments at the end of the file:

@echo off

echo commands

exit /b 

-------------------
commnts at the end 
of the file
------------------

Inline comments

Expansion of not existing variables is replaced with nothing ,and as setting a variable with = rather hard you can use this for inline comments:

@echo off

echo long command %= this is a comment =% with an inline comment

Multiline comments

For multiline comments GOTO (for outside brackets) and REM with conditional execution (for inside brackets) can be used. More details here:

@echo off

echo starting script

goto :end_comments
 comented line 
 one more commented line
:end_comments

echo continue with the script

(
    echo demonstration off
    rem/||(
      lines with
      comments
    )
    echo multiline comment inside
    echo brackets
)

And the same technique beautified with macros:

@echo off

::GOTO comment macro
set "[:=goto :]%%"
::brackets comment macros
set "[=rem/||(" & set "]=)"

::testing
echo not commented 1

%[:%
  multi 
  line
  comment outside of brackets
%:]%

echo not commented 2

%[:%
  second multi 
  line
  comment outside of brackets
%:]%

::GOTO macro cannot be used inside for
for %%a in (first second) do (
    echo first not commented line of the %%a execution
    %[%
        multi line
        comment
    %]%
    echo second not commented line of the %%a execution
)
npocmaka
  • 51,748
  • 17
  • 123
  • 166