Batch File Subcommands (cont.)
Microcomputer/DOS Tutorial


Computer Studying

Navigation
{NavForms}

Virus Tutorial Map
Tutorial Map

Subcommands in this section are generally used to create batch programs. As such, they are a bit more complex than the simply ones studied on the last page.

IF

The IF subcommand allows you to make decisions within your batch file. Its syntax is:

IF Condition Command where,

Command = Any legal DOS command or batch file subcommand
Condition = One of three tests that yield true or false:

1. The ERRORLEVEL of a program.
2. Two strings are equivalent.
3. A file exists in the current directory.

Unlike programming languages, which allow many logical tests in an IF statement, the batch IF statement is limited to only the three above.

Condition 1: ERRORLEVEL is a number that indicates to DOS whether the last program run was successful.

A zero (0) indicates a good run, anything above zero indicates an error condition. (Only DOS commands BACKUP and RESTORE have an exit code.)

You can create small programs to capture the keyboard output and report it as an error level (see Batch Sample #3 later).

Condition 2: String comparison is indicated by double equal signs:

String1 == String2

compares the two strings.

This comparison is often used with parameters and markers to check for a particular entry.

For example,

IF %1 == 40 MODE C40

checks parameter one for 40 and, if found, changes the display to 40-columns wide.

Condition 3: The logical test checking for a file has the format:

EXIST d:Filename

You can use this test to check and see if a DOS disk is in the active drive (as one example). Another use might be to check for a particular file to see if the user has a valid disk in the drive. Pathnames are NOT allowed.

Password Example

The following batch file can be used to establish a password for running a program. The batch file is named START.BAT and calls the program named WP.COM.

ECHO OFF
IF %1==XYZ GOTO GOOD
ECHO BAD PASSWORD...ENDING
GOTO END
:GOOD
ECHO YOU'RE OK...STARTING
WP
:END

Below you'll see the response of the computer to various commands...

First the bad password. At the prompt type START ABC.

A>START ABC
A>ECHO OFF
BAD PASSWORD...ENDING
A>_

Now use the correct password. Type the correct command at the prompt.

A>START XYZ
A>ECHO OFF
YOU'RE OK...STARTING

At this point the WP program starts. You don't see the command because echo is off.

SHIFT

The limit of 10 parameters for a batch file can be raised through use of the SHIFT command. The syntax is the single word:

SHIFT

When that subcommand is encountered, all parameter/marker pairings are shifted one to the left. Whatever was assigned to %0 is lost, the contents of %1 are moved to %0, %2 moves to %1 ... %9 moves to %8 and a new parameter from the command line is moved into %9.

While this brings in a new parameter, all have shifted and you must anticipate the effect on your batch file "program."

The effect of SHIFT:

Shift Effect

Remember, this command seems very simple, but its effects are far ranging and the logical consequence of mismatching parameters and markers and be disastrous!

FOR..IN..DO

This command is similar to the programmer's FOR..NEXT loop in that a particular action can be repeated a given number of times. The syntax (which may look a bit complicated) is:

FOR %%Variable IN (Set) DO Command

%%Variable is one-letter with the mandatory %% before it. Two percentage signs are used so this variable won't be confused with a marker.

(Set) is one or more filenames or commands you want %%Variable to assume while the command is being executed. Use a space between entries, and pathnames are allowed. Don't forget the parenthesis around the set. Wildcards may be used within (Set) if you are using filenames.

Command is the particular DOS command or batch subcommand you want to have performed. Usually one or more of these commands will contain the %%Variable in it. If (Set) contains DOS commands, only %%Variable is used.

In effect, what this subcommand does is cause %%Variable to be an index into the (Set) for use by Command.

An example:

Problem: Compare all files on the disk in drive A: with those on the disk in drive B: and report those that match.

Answer: The following two-line batch file run from the A: drive will do that task:

ECHO OFF
FOR %%Z IN (*.*) DO IF EXIST B:%%Z ECHO %%Z is on A: and B:

Let's see how...

The first line turns command display off to clear the clutter.

The second line is executed as many times as there are files on the disk in A: [the set (*.*) assures this]. Each of those filenames are assigned to %%Z in turn and then checked for presence on drive B: with the EXIST logical statement. If EXIST is true, then the message at the end of the IF subcommand is sent to the screen, otherwise nothing is printed and the next file on drive A: is assigned and checked.

The following will diagram this process...

Files on drive A:

COMMAND.COM
FILE.ONE
FILE.TWO

Files on drive B:

COMMAND.COM
FILE.ONE
FILE.LTR

The batch subcommand we are investigating is:

FOR %%Z IN (*.*) DO IF EXIST B:%%Z ECHO %%Z is on A: and B:

Each filename on A: is substituted in the IF subcommand and then executed. To get the same effect you would have to type:

IF EXIST B:COMMAND.COM ECHO COMMAND.COM is on A: and B:
IF EXIST B:FILE.ONE ECHO FILE.ONE is on A: and B:
IF EXIST B:FILE.TWO ECHO FILE.TWO is on A: and B:

In the case of the example above, the first two would have a positive response and the last would not print anything. Study it carefully before going on!

Another example:

Files on drive A:

COMMAND.COM
FILE.ONE
FILE.TWO

Files on drive B:

COMMAND.COM
FILE.ONE
FILE.LTR

OK, told you to study the example. Let's see if you remember. What is the one line batch file command to find and report out all files starting with an "F" on drive A:?

FOR %%Z IN (A:F*.*) DO ECHO File %%Z is on drive A:

In this case you see that the A: disk is checked for any file starting with the letter "F" and that name is substituted in the variable %%Z. The appropriate message is then printed.

This is not an easy concept. It will require some study and practice to master.

Let's now look at using DOS commands in (Set).

The (Set) can contain DOS commands instead of filenames and these commands will then be executed in sequence (to include running large programs under the control of the FOR..IN..DO loop).

Let's say you want to sequentially:

  • Clear the screen,
  • Show the DOS version number,
  • then Show a disk directory with pause on.

You could do all that in a one line batch file with the following command in it:

FOR %%T IN (CLS Ver Dir/P) DO %%T

When using DOS commands in (Set) you must use the space as a delimiter and cannot have the asterisk (*) or question mark (?) in any command. Use a colon (:) instead of a space when passing parameters to programs (i.e., DBASE:FILE instead of DBASE FILE). [The colon trick does not always work; you have to experiment.]

It is possible to issue the FOR..IN..DO command at the DOS prompt by dropping one of the percentage signs (%) on the variable.

CALL

Sometimes it's handy to be able to run one batch file from another. Until DOS 3.3 you had to resort to tricks to do that; without the tricks when you called a second batch file from a first then control would transfer to the second and you'd never get back to the first.

The command: CALL d:path\FILENAME parameters

can be used in DOS versions 3.3 and later to start a second batch file from a parent and then return to the parent after the second finishes. Note: Do not use pipes or redirection on the second file.

The FILENAME is the name of the second batch file; the parameters are any options that the second file requires to properly run. When the second batch file terminates, control is returned to the first batch file on the line following the CALL command.

You can simulate CALL in DOS versions lower than 3.3 by using:

COMMAND /C d:path\FILENAME parameters

@

In DOS 3.3 and later you can selectively cause lines in a batch file from being displayed. To cause this, place an "@" sign in front of the line in question.

One use of this would be to suppress showing the "ECHO OFF" command which is the starting command of many batch files. Until the ECHO OFF command is actually executed, ECHO is ON and the command shows. To stop even this from showing make the first command: @ECHO OFF.

If you need to use the "@" feature with a command that already starts with @ (e.g., @WIP.EXE) then use a double @ (e.g., @@WIP) in the batch file (this should be rare).

Keep Reading Batch File Examples


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