Saturday, 2 February 2013

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.

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.

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

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