Documenting Your Scripts with Comments and Creating a Script Template  Hot PDF Print E-mail
Tag it:
Delicious
Furl it!
Digg
NewsVine
Reddit
YahooMyWeb
Technorati
Articles Reviews Microsoft Windows
Written by Adi Bach   
Saturday, 16 December 2006

{mos_sb_discuss:42}

Adding comments to your Windows scripts makes them easier for other peopleto understand. Comments provide you with the ability to embed documentationwith a script so that you can explain how and why you wrote it the way that youdid. Adding comments to scripts is a little bit like adding a trail of bread crumbs.


 

They give you something to follow if you find that you need to fix or modify ascript sometime down the road.Comments are added to Windows shell scripts using the REM statement, which hasthe following syntax:

            REM Comment


Comment is a text string representing the documentation that you wish to embed in the script. REM statements have no impact on the execution of your script. The Windows shell ignores them during script execution.

You can use the REM statement in either of two ways. One way to use the REM statement is to include it on a line by itself to describe or document the action of one or more statements that follow, as demonstrated below.

@ECHO off

REM Display the Welcome Screen
ECHO.
ECHO.
ECHO W E L C O M E T O T H E
ECHO.
ECHO F O R T U N E T E L L E R G A M E !
ECHO.
ECHO.
ECHO.

REM Make the player hit a key in order for the game to continue
PAUSE

A second way to use the REM statement is to place it at the end of another statement, as demonstrated below.

PAUSE REM Make the player hit a key in order for the game to continue

Creating a Script Template

Now that you know how to use the REM statement to add comments to your Windows shell scripts, consider a second application for this highly useful statement.

Instead of using the REM statement just to document your script’s logic, how about using it to improve your scripts overall organization? Specifically, I am suggesting that you create a Windows shell script template similar to the one I have created below.

@ECHO off
REM ******************************************************
REM
REM Script Name: Xxxxxxxx.bat
REM Author: Xxxx Xxxxx
REM Date: Xxxxx XX, XXXX
REM
REM Description: Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
REM
REM *****************************************************

REM Script Initialization Section

REM Main Processing Section

REM Subroutine and Procedure Section

In this example, the template begins with the @ECHO off statement and then uses the REM statement to format a script header in which you can document information about the script, including its name, author, creation date, and a description.

Three additional statements have been added to the bottom of the template and can be used to organize your scripts into three major sections. In the Initialization section, you would add statements that perform functions such as setting foreground and background colors or posting the name of the script in the Windows command console’s title bar.

By using the template I have provided or by creating one of your own, you lay down a foundation for all future script development with a consistent organizational structure that will be easy to follow and update.

For example, the following script demonstrates how to use the template in the creation of a new script.

@ECHO off
REM *********************************************************
REM
REM Script Name: ScriptInit.bat
REM Author: Jerry Ford
REM Date: June 21, 2003
REM
REM Description: Customize a Windows shell scripting work environment
REM
REM *********************************************************


REM Script Initialization Section

REM Modify the Windows command console title bar
TITLE = Script Environment Configuration

REM Set background color to white and foreground color to black

COLOR F0

REM Add C:Scripts to the search path
PATH %path%;C:Scripts

REM Modify the command prompt to display the greater than sign
PROMPT $g

REM Main Processing Section

REM Clear the screen
CLS

REM Tell the user that everything it set up
ECHO Script environment initialization complete

REM Subroutine and Procedure Section

As you can see, anyone who views the script can quickly identify the script’s purpose and its author. By looking for the three main script comments, you can also easily locate different sections of the script. By adding additional comments, you can create self-documenting scripts.

Note that while this particular example does not have any subroutines or procedures, you might still want to include that section comment in the script as a placeholder for possible future development.


User reviews

There are no user reviews for this item.

Add new review




Powered by jReviews

Last Updated ( Friday, 08 June 2007 )
 
< Prev   Next >