Batch File Subcommands
Microcomputer/DOS Tutorial


Computer Studying

Navigation
{NavForms}

Virus Tutorial Map
Tutorial Map

In addition to the normal DOS commands, batch files have their own subcommand structure. Following are the subcommands in the order we will discuss them:

  • ECHO: Turns command display on/off or may display a message.
  • REM: Displays a message on the screen.
  • PAUSE: Temporarily stops the batch file execution.
  • GOTO: Jumps to a labeled set of commands in the batch file.
  • IF: Permits conditional operation of any command.
  • SHIFT: Reassigns the relationship of parameters to markers.
  • FOR..IN..DO: Allows iteration subject to defined conditions.
  • CALL: Runs another batch file then returns to first.
  • @: Turns display off for single command

ECHO

You have seen one facet of ECHO already: it can send a message to the screen. More importantly, it can help clear the clutter on the screen when a batch file executes. Normally, the batch file commands would show on the screen as if you were typing them. This can get distracting. If you want to suppress the command echoing type:

ECHO OFF

To restart echoing, type: ECHO ON

When ECHO is off no part of the actual DOS commands in the batch file will show on the screen. To display a message put it after the ECHO command:

ECHO Message

As you will see, the REMark command also displays messages; but NOT if ECHO is off! Use ECHO if you want the message to show no mater what.

REMark

REMark can be used to send messages to the screen or simply to document some part of your batch file's operation.

Use them extensively in long batch files. The computer operator (it won't always be you) wants to know what is happening and, over time, you might forget what a complicated set of commands really does.

The format is:

REM Message

REMarks can be up to 123 characters long, although you usually won't want to go over the screen width so you can control the display.

An undocumented, variation of this command is the period. In DOS 2.x, if a line in a batch file starts with one or more periods (.) it is treated as a remark. It is dangerous to use this however since in DOS 3.x and later that same line would be treated as the path to a command!

PAUSE

The last "simple" command is PAUSE. Its basic function is to stop the execution of the batch file until you press a key. This can allow you to perform a necessary task; like perhaps changing a disk or verifying that a particular disk configuration is in place in order to avoid errors as the remaining parts of the batch file are executed.

In early DOS versions PAUSE would optionally display a message. It does not now. In order to display a message you have to couple PAUSE with the ECHO command. The format in use now would require:

ECHO Message
PAUSE

The message will show, followed by the DOS message:

Strike a key when ready... (in early DOS versions)
Press any key to continue... (in later DOS versions)

GOTO

The format for this command is:

GOTO LABEL

where LABEL is a line in your batch file that starts with a colon (:) followed by up to an eight character name (actually, you can type any number of characters, but DOS only recognizes the first eight).

Want to create an endless loop? GOTO gives you an easy way. Here is one example:

:START
REM...This is being typed by an endless loop.
GOTO START

When executed, this file will continue to print the endless loop line and the GOTO command until you tap Control-Break.

Use this subcommand to transfer control in your batch files.

If you want to terminate any batch file before it is complete, issue the break command [Control-Break].

Interim Review

You have now seen the simple stuff...Let's see how much of it stuck. Following are a few true/false questions. Think of the answer then look below to see if you were right.

Questions:

1. The file AUTOEXEC.BAT must be in the root directory of the boot disk.
2. Batch files can execute a variety of subcommands in addition to the standard DOS commands.
3. Parameters are designated as %0 through %9.
4. When DOS finds a batch subcommand that is improperly phrased, it stops execution of the file and shows Syntax error.
5. You may interrupt a batch command by pressing Control-Home.
6. When you type Control-Break to stop a batch file, DOS will ask you if you want to terminate. If you say No the current command will be ignored but the rest will be processed.
7. The batch filename is substituted for marker %0.
8. REMark subcommands display a message regardless of the the condition of ECHO.
9. PAUSE causes the batch file to temporarily stop and wait for you to press a key.

Answers:

1. True, AUTOEXEC.BAT executes automatically when DOS boots so it must be in the root. (A trick question as it assumes knowledge from prior tutorials.[Smile])
2. True, you've seen some, with more to come.
3. False, those are markers. A parameter is information you type in on the command line that is substituted for a marker when commands are executed.
4. True, we didn't talk about that specifically, but that's what happens.
5. False, the correct command is Control-Break.
6. True, revealing another little quirk of batch file processing.
7. True, but there is a command you can use to change that as we'll see later.
8. False, REM only shows its message if ECHO is ON. If ECHO is OFF, only ECHO puts a message to the screen.
9. True.

That's enough. You should now understand the batch file subcommands you are likely to need most often.

Let's move on to the rest of the subcommands that allow you to program within a batch file.

Keep Reading Subcommands (cont.)


{NavBar}
Home | Products | Definitions | Newsletter | Virus Tutorial | Contact


FILExt is proudly hosted by DewaHost.com - a fast and reliable host for everyone!
DewaHost offers premium Web hosting starting from $8.95/month and a high speed file hosting service FileBurst!

Please use the contact form for questions or comments about this web site.
Copyright © 2001 Computer Knowledge, All Rights Reserved
Pray the Rosary for peace.