-1

I want to write down some notes in a .bat file. What codes can be added to the notes so that they will not be treated as commands to be run?

1

2 Answers 2

5

In cmd.exe scripts, comments are added using REM (or @REM to forcefully disable echoing).

rem This is a comment.

While not technically a comment prefix, :: can also be used the same way (it defines a "goto" label that remains unused):

:: This is a comment.

Caution:

When labels are used as comments within a bracketed code block or for command, the command processor expects every label to be followed by at least one command, so when a jump is made to the label it will have something to execute.

The cmd shell will try to execute the second line even if it is formatted as a label (and this causes an error):

(
echo This example will fail
:: some comment
)

When working within bracketed code blocks it is definitely safer to use rem for all comment lines.

Source Using Labels as Comments

You can also use variables as comments.

It is also possible to use variables as comments. This can be useful to conditionally prevent commands being executed:

@echo off 
setlocal
if /i "%~1"=="update" (set _skip=) Else (set _skip=REM)
%_skip% copy update.dat 
%_skip% echo Update applied 
... 

When using the above code snippet in a batch file the lines beginning with %_skip% are only executed if the batch file is called with update as a parameter.

Source Using Variables as Comments

0
2

You could simply append to the end of the file if the last code is a Goto :Eof Cmd doesn't care about lines the code never reaches. With a unique marker the batch itself can determine the begin of the text and display using more.

@Echo off
For /f "tokens=1 delims=:" %%A in (
  'findstr /N "^@@@" "%~f0"'
  ) Do Set TxtBegin=%%A
:: Echo TxtBegin=%TxtBegin%
More +%TxtBegin% "%~f0"
Pause
Goto :Eof
@@@Begin of text
Here Comes the Sun
The Beatles
Here comes the sun (doo doo doo doo)
Here comes the sun, and I say
It's all right
Little darling, it's been a long cold lonely winter
Little darling, it feels like years since it's been here
Here comes the sun
Here comes the sun, and I say
..snip..

Not the answer you're looking for? Browse other questions tagged .