Tuesday, 15 October 2013

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\FILE NAME 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 FILE NAME 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\FILE NAME 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).
Below are some simple batch file examples to whet your appetite.

Saturday, 22 June 2013

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

Sunday, 3 March 2013

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:

When that sub command 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:



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

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.

Tuesday, 29 January 2013

ECHO

You have seen one facet of ECHO already: it can send a messageto 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; butNOT 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 whata complicated set of
commands really does.
The format is:
REMMessage
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 anecessary 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)