home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Simtel MSDOS 1992 September
/
Simtel20_Sept92.cdr
/
msdos
/
batutl
/
battutor.arc
/
_FIND.REM
< prev
next >
Wrap
Text File
|
1983-08-29
|
8KB
|
204 lines
Text File _find.rem
FINDFILE.COM and SETDISK.COM
A desirable feature of a "friendly" Batch File is the ability of the Batch File
to "orient" itself. By this we mean the ability to discover on which drive are
located useful files such as system utilities or files which the running Batch
File would like to either run or display.
We can use searching algorithms involving DOS's PATH command and IF EXIST Batch
File subcommand, but these commands have a drawback. When a PATH search or an
IF EXIST test on a directory is done on a drive that is not ready (i.e., door
open), the program is interrupted with the critical error message (the famous
"Abort, Retry, or Ignore" message). We would like to be more unobtrusive in
our searching, allowing the user as much freedom as possible about the use (and
intentional non-use) of his disk drives. If we are going to bother the user
with as few questions, prompts, and constraints as possible and still find what
we need, then we would like a more benign method of searching for files; that
is, a searching utility that will just search the "ready" drives, passing over
any drives that are not ready. In that way, we only need to bother the user
with a polite message if, after looking everywhere that we can, we still cannot
find the file we need.
We introduce two utilities to implement this ability to orient a Batch File,
and then we give a couple of examples of their use. Again, these utilities
have been used previously in this tutorial, so listings of these Batch Files
are also a source of examples.
FINDFILE
The purpose of FINDFILE is to accept a file name on its command line and then
search all existing physical drives for the file, bypassing drives that are not
ready for any reason, until it finds the first drive which contains the given
file name.
The syntax of the command line is as follows:
FINDFILE fname
where "fname" is a filename and extension. Giving a disk drive specification
with fname (i.e., "A:fname") is paradoxical, and so a disk drive spec in front
of fname is ignored. FINDFILE searches for the file and reports its findings
in the errorlevel parameter:
errorlevel = 0 if fname is found on A:
errorlevel = 1 if fname is found on B:
errorlevel = 2 if fname is found on C:
errorlevel = 3 if fname is found on D:
.
.
.
etc
errorlevel = 255 if fname is not found
errorlevel = 255, and
CTRL BREAK is executed if fname is missing from command line
Notice that leaving off the fname is assumed to be a bug during the development
of the Batch File, so a CTRL BREAK is executed to allow the Batch File to be
aborted if desired.
SETDISK
SETDISK has the same syntax as FINDFILE:
SETDISK fname
The operation of SETDISK is the same as FINDFILE with one additional feature.
If SETDISK finds the fname, it not only reports the disk number in errorlevel,
but it also automatically sets the default drive designator to the drive that
contains the file.
Lets give a couple of examples of the use of these utilities.
Pathing
Suppose at some point in our Batch File we want to display a text file called
COMMENTS.REM. We would like to use the DOS command MORE because of its
automatic pauses when the screen is full ( is this beginning to sound
familiar?). We need to find where the MORE.COM file is residing, but the only
constraint that we want to place on the user is to have both the MORE.COM file
and the COMMENTS.REM file available (in the current directory ) on any disk
drives. So, after a friendly prompt asking the user to make the files
available on any drives, we execute the following command lines in the Batch
File:
findfile more.com
if errorlevel 255 goto cantfind
if not errorlevel 4 path = d:\
if not errorlevel 3 path = c:\
if not errorlevel 2 path = b:\
if not errorlevel 1 path = a:\
setdisk comments.rem
if errorlevel 255 goto cantfind
more <comments.rem
The testing after FINDFILE will leave the PATH environment pointing to the
drive containing MORE.COM. DOS will follow that "path" to MORE.COM if it can't
find it on the default drive when the MORE command line is executed. Then
SETDISK will set the default drive to the drive containing the COMMENTS.REM
file, so that the references to COMMENTS.REM in the Batch File commands need
not have a fixed, explicit drive reference.
Where am I?
If we have a very large Batch File function that is broken up into many .BAT
files, text comment files, and utilities ( as we do right here ),then we would
like to do one very important orientation of the Batch File right at the
beginning. We would like to set the default drive to the drive that the Batch
File itself is running on, so that all our references to companion files (which
we assume will be on the same diskette) can be written without drive
specifiers, allowing the Batch File to run in any drive.
We might just execute something like the following...
SETDISK runme.bat
at the beginning of the runme.bat Batch File. This will work if the SETDISK.COM
file is already located on the default drive. But if SETDISK.COM is one of the
utilities on this "load it anywhere" diskette, then we have a problem. We must
either constrain the user to run the Batch File with the default disk
designator pointing to it, as we did with this tutorial, or we must do a
"fixed" path search for the drive with "ourself" on it, and then change the
default drive designator to that drive. However, we cannot pick a general
fixed path for all systems. For instance, if we chose the following path to
try to find SETDISK.COM ...
path a:\; b:\; c:\; d:\
setdisk runme.bat
then DOS's path search would blow up with the critical error message if
SETDISK.COM were located on drive C: and drive A: or B: had its door open
(plus, on a single-drive XT, we would also get the "Insert the diskette for
drive A:/B:" messages ). Clearly, there are problems with these two utilities
when used in a drive configuration that is unknown at the time that the Batch
File is created.
When using these utilities for your own "in house" Batch Files, you can adopt
conventions such as locating all the utilities on a certain disk drive, or,
better yet, copying the utilities to a RAM disk as part of your AUTOEXEC
warm-start sequence, and then always running Batch Files on the RAM disk for
enhanced performance.
If these two utilities are more confusing and upsetting than helpful, then
forget them and just constrain the user ( who is probably yourself -- "we have
seen the enemy, and he is us" ) to use certain drives and to run Batch Files
with the default drive designator always pointing to the Batch File disk.
Let's demonstrate the "benign" search feature. We'll search for the disk with
MORE.COM on it. If convenient, try leaving a drive door open on the drive that
has MORE.COM on it, or a drive before it. Don't open the door of the drive
containing the Batch File, and ...
DON'T OPEN OR CLOSE A DRIVE DOOR WHILE THE DRIVE LIGHT IS ON!
Do it during the "pause" AFTER I say it's OK to open a drive door.
Be patient when the search arrives at a not-ready drive; the light will stay on
for about 15 seconds (I hope to reduce this time in a subsequent version, but
it's tricky ...), but it will eventually time-out and then continue the
search.
Here's the pause ...