Thursday, January 10, 2013

How to comment lines in DOS batch scripts

Although there is no real DOS prompt in Microsoft Windows operating systems since Vista, rather just it's emulation, don't let if trick you - DOS prompt still represents great and valuable resource for making scripts (which sometimes can grow quite in size and be quite complex) with wide application scope. But in this process you'll for sure be experimenting with various commands and before you'll accomplish your desired goal you'll probably want to (un)comment certail lines of code.

Also you'll probably want to leave some of the comments to help you clarify what and why did you use certain commands at the first place when you come back after certain period of time to look at your DOS batch script code. There is a general command for commenting in DOS batch scripts named "REM" (from word REMark). This post will present you few other tricks you can use for commenting lines of code in your DOS batch scripts. Recently I had a such need so it made me search around to find a suitable solution. Upon searching for soultion I came across great resource on Rob van der Woude's Scripting Pages (link to his pages is availabe at the end of this article). I'm not really a fan of copy-paste method but I really thought it cannot be made better nor have found a reason not to copy it as author stated it is freeware in his disclaimer so this post mostly contains copy-pasted content from there. The only difference is I made a selection of most important samples from his examples.

To make long things short you might use ( :: ) semicolons for commenting single lines of code instead of REM command as the first characters of the comment line. That way, the label is invalid but still treated as a label, and skipped (i.e. the next line is read immediately by COMMAND.COM, without the need to reopen the batch file). Labels, on the other hand, whether valid or not, should always start at the first non-whitespace character in a command line.
 
REM Comment line 1
 REM Comment line 2
:Label1
 :Label2
:: Comment line 3
 :: Comment line 4
IF EXIST C:\AUTOEXEC.BAT REM AUTOEXEC.BAT exists
are all allowed.
However,
IF EXIST C:\AUTOEXEC.BAT :: AUTOEXEC.BAT exists
will result in a Syntax error message.
A true pitfall are code blocks, several commands grouped between parentheses and interpreted as a single command line by CMD.EXE!
IF EXIST C:\AUTOEXEC.BAT (
 :: Comment line 1
 ECHO Do something
 :: Comment line 2
)
will result in an error message stating:
) was unexpected at this time.
and:
IF EXIST C:\AUTOEXEC.BAT (
 :: Comment line 1
 ECHO Do something
 :: Comment line 2
 :: Comment line 3
)
will result in another error message:
Do something
The system cannot find the drive specified.

The same is true for FOR loops.
Try and see for yourself:
FOR %%A IN (1 2 3) DO (
 :: Comment line 1
 ECHO Do something
 :: Comment line 2
)
and:
FOR %%A IN (1 2 3) DO (
 :: Comment line 1
 ECHO Do something
 :: Comment line 2
 :: Comment line 3
)
will also result in error messages.
The errors are caused by labels being used within code blocks.
Replace the double colons by REM statements and these samples will all run without a glitch.
Better still, don't use comments within code blocks at all, but place them just before the code blocks instead:
:: Comment line 1
:: Comment line 2
:: Comment line 3
FOR %%A IN (1 2 3) DO (
 ECHO Do something
)
or:
REM Comment line 1
REM Comment line 2
REM Comment line 3
FOR %%A IN (1 2 3) DO (
 ECHO Do something
)

Another quite neat trick is regarding commenting code blocks which are natively not supported in DOS batch scripts, though there are ways to accomplish the effect:

@ECHO OFF
REM Do something
  ...
  ...
REM End of code

REM Start of comment block 1
GOTO EndComment1
This line is comment.
And so is this line.
And this one...
:EndComment1


Or, if the comment block appears at the end of the batch file:

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

Leo Gutierrez Ramirez came up with an even shorter way to accomplish a comment block at the end of a batch file:

@ECHO OFF
REM Do something
  ...
  ...
REM End of code

(Start of comment block at end of batch file
This line is comment.
And so is this line.
And this one...
Just make sure you never use a closing parenthesis.

Note: This trick does have one disadvantage: the use of parentheses in the comment block is not allowed.

As I've already mentioned all code in this post was copied from Rob van der Woude's Scripting Pages, but in somewhat shorter form, not to loose it's functionality. If you need additional information regarding commenting in DOS batch scripts be sure to visit his page.

.

If you found this post to be useful please consider encouraging me to keep up the good work. You can easily do it by signing up with Copy.com cloud storage provider. Just click here and you will get 15GB + 5GB for following my link absolutely FREE. This way I'll get 5GB too.

No comments:

Post a Comment