1234

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 useREM. 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's user avatar
T.Todua
57.1k22 gold badges261 silver badges266 bronze badges
askedJun 29, 2012 at 21:40
user1397044's user avatar
3
  • 11
    See also excellent answers herestackoverflow.com/q/12407800/1011025CommentedFeb 11, 2017 at 18:22
  • 4
    You can UseRem Sth Command Or Use This Mark ::: SthCommentedAug 6, 2019 at 10:36
  • I prefer to use: - REM for comments - &REM for inline commentsCommentedSep 20, 2021 at 15:42

12 Answers12

1402

Use:: orREM

::   commentttttttttttREM  commenttttttttttt

BUT (as people noted):

  • if they are not in the beginning of line, then add& character:
    your commands here & :: commenttttttttttt
  • Inside nested parts (IF/ELSE,FOR loops, etc...):: should be followed with normal line, otherwise it gives error (useREM there).
  • :: may also fail withinsetlocal ENABLEDELAYEDEXPANSION
answeredMay 30, 2013 at 15:02
T.Todua's user avatar
Sign up to request clarification or add additional context in comments.

19 Comments

The double colon :: Is the cleanest .bat comment there is. And it can be used at the start or middle of a line!
Didn't work for me when done inline like this.cd "C:\Folder" ::this throws a syntax error
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 rundir ::blah which doesn't list the contents of. and compare withdir & ::blah, which does
Warning, using:: will bug scripts withsetlocal ENABLEDELAYEDEXPANSION andfor
Examples of how using:: labels as comments can cause difficult-to-anticipate errors. Calling this clean is a bit misleading @ATSiem
|
1012

Therem command is indeed for comments. It doesn't inherently update anyone after running the script. Some script authors might use it that way instead ofecho, though, because by default the batch interpreter willprint out each command before it's processed. Sincerem 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'secho off to avoid printing further commands; the@ is to avoid printingthat command prior to the echo setting taking effect.)

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

@echo offREM To skip the following Python commands, put "REM" before them:python foo.pypython bar.py
answeredJun 29, 2012 at 21:49
Rob Kennedy's user avatar

6 Comments

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 I feel this should be the correct answer. When using syntax highlighting the REM command does not actually highlight the text as "commented out".
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):robvanderwoude.com/comments.php
Interesting. I though REM was the 'whole line comment' and the double colon was the 'inline comment', likeREM This whole line is a comment@echo off :: This comment is inline
@Richard Smith, I get an error runningecho off & :: TEST. Even when putting an@ in front ofecho, it still seems to give the same error which is,echo was unexpected at this time. Is there a way to comment afterecho off (inline comment in other words)?
|
60

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

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

REM it starts here the section below can be safely erased once the file is customisedECHO Hey you need to edit this file before running it!  Check the instructions insideECHO Now press ctrl-c to interrupt execution or enter to continuePAUSEREM erase the section above once you have customised the filepython executed1.pyECHO Skipping some stuff nowGOTO Endpython skipped1.pypython skipped2.py:ENDpython executed2.py

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

You can read moreon this website.

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

answeredJun 29, 2012 at 21:46
fvu's user avatar

Comments

40

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 CODEGOTO LABEL  ::REM OUT THIS LINE TO EXECUTE THE CODE BETWEEN THIS GOTO AND :LABELSOME CODE TO SKIP.LAST LINE OF CODE TO SKIP:LABELCODE TO EXECUTE
answeredNov 2, 2013 at 21:33
Kees's user avatar

8 Comments

@RobKennedy as I read the answer, it does not read anything after the ::, where everything after REM is read.
If the command interpreterstops 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:: andrem equally.
@RobKennedy, I also thought it was implied that it only applied to the line the command was on.
@RobKennedy it doesread but it does notparse (or process) what it reads, it just skips until the next\n and then starts parsing again [citation needed]
@JamesJenkins The voice of reason. (And reading comments properly!!)
|
33

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.

Seethis 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 EndComment1This line is comment.And so is this line.And this one...:EndComment1

You can useGOTO Label and :Label for making block comments.

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

@ECHO OFFREM Do something  •  •REM End of code; use GOTO:EOF instead of EXIT for Windows NT and laterEXITStart of comment block at end of batch fileThis line is comment.And so is this line.And this one...
Martijn Pieters's user avatar
Martijn Pieters
1.1m326 gold badges4.2k silver badges3.4k bronze badges
answeredAug 24, 2016 at 10:50
Somnath Muluk's user avatar

1 Comment

this is often used in hybrid batch & VBS/JS scripts, although they used ifif instead ofgotostackoverflow.com/q/9074476/995714ss64.com/vb/syntax-hybrid.htmlstackoverflow.com/a/34512715/995714
18

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

color C          & :: set red font colorecho IMPORTANT INFORMATIONcolor            & :: reset the color to default

Explanation:

& separates two commands, so in this casecolor 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 isnot a valid use of the comment. It works only becausegoto ignores all arguments past the first one. The proof is easy, thisgoto 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 thecolor command:::,grey,on,blue.

It will only work as:

color 17     &      :: grey on blue

So the ampersand is inevitable.

answeredJun 9, 2017 at 14:38
miroxlav's user avatar

3 Comments

From other answers, it seems that the ampersand is not needed?
@Snaptastic – The other answers are actually wrong in this point, see my recent edit.
This seems to be the correct way; It even gets highlighted as comment in my IDE.
10

You can comment something out using:: orREM:

your commands here:: commenttttttttttt

or

your commands hereREM  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, useREM instead.
answeredSep 21, 2018 at 5:24
Pikamander2's user avatar

1 Comment

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.
5

I prefer to use:

  • REM for comments
  • &REM for inline comments

Example:

@echo offset parameter1=%1%REM test if the parameter 1 was receivedif defined parameter1 echo The parameter 1 is %parameter1% &REM Display the parameter
answeredSep 20, 2021 at 15:48
Jorge Cribb's user avatar

3 Comments

I can't see where your answer add any more information. There are already answers with the same content, at least one is 8 years old
It's a simplest answer...without complexity
@JorgeCribb not very clear
4

Commenting a line

For commenting line useREM or :: though:: mightfail inside brackets

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

@echo offsetlocal enableDelayedExpansionecho delayed expansion activated!;delayed expansion commented lineecho end of the demonstration

Comment at the end of line

For comments at the end of line you can again userem 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 theexit command you can use it to put comments at the end of the file:

@echo offecho commandsexit /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 canuse this for inline comments:

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

Multiline comments

For multiline commentsGOTO (for outside brackets) andREM with conditional execution (for inside brackets) can be used.More details here:

@echo offecho starting scriptgoto :end_comments comented line  one more commented line:end_commentsecho 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 macroset "[:=goto :]%%"::brackets comment macrosset "[=rem/||(" & set "]=)"::testingecho 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 forfor %%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)
answeredNov 9, 2020 at 0:44
npocmaka's user avatar

3 Comments

Interesting... Wanted to comment out a wrong batch var expansion and using:REM echo ~dp*= %~dp* or%= echo ~dp*= %~dp* =% didn't work. Combining both it finally worked:rem %= echo ~dp*= %~dp* =%
@hexaae - arguments expansion is with higher priority during parsing. Good that you've found a way to comment it.
@hexaae - it works because cmd takes%= echo ~dp*= % as a variable. The lonely~ as a command and then it is commented with REM. Though a good way to avoid theREM %~ bug
3

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 followingFOR syntax

The basic FOR syntax is as follows

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

here<Command> can be any valid commandSo we can write the following valid command line asrem 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
answeredFeb 13, 2019 at 7:12
jyao's user avatar

1 Comment

technically,:: is an (invalid) label (which explains it's behavior) and so should not be used (although it's still quite common)
3

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 commentSo 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 onhttps://www.robvanderwoude.com/comments.php

answeredJan 12, 2020 at 16:40
avarice's user avatar

Comments

2

You can use:: orrem for comments.

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

Only if comments are inif, userem, as the colons could make errors, because they are a label.

answeredJun 14, 2020 at 17:01
Anic17's user avatar

Comments

Protected question. To answer this question, you need to have at least 10 reputation on this site (not counting theassociation bonus). The reputation requirement helps protect this question from spam and non-answer activity.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.