Vous êtes sur la page 1sur 19

----------------------------------------------------------------

BATCH FILES FOR EFFICIENCY - GETTING REAL WORK DONE


----------------------------------------------------------------
Batch files are one of the hidden treasures within your
computer. Let's face it, learning and using DOS commands is a
test of patience, memory and stamina. Batch files are the secret
weapon which can boost your computer into the fast lane of work
productivity.
Batch files are small software programs which you can prepare in
the space of only five or ten minutes which automate a variety
of tasks and customize the computer to your work style. The
bottom line is that batch files can preserve your sanity in the
face of arcane, easily-forgotten DOS commands to provide you
with a menu system for your hard drive, transfer and backup
files, provide security, start other software programs, activate
your printer and much more.
Amazingly, batch files require that you become familiar with
only eight commands in addition to the normal DOS commands.
Working with batch files means you are programming in the most
literal sense - let's take a tour of the small miracles called
batch files . . .
A batch file is little more than a list of DOS commands plus
eight special batch commands. These commands are stored on your
floppy or hard disk in an ordinary text file such as produced by
your word processor or text editor. If you can type a letter to
a friend, you can prepare a batch file!
Several time-saving batch file examples which provide real work
power to your everyday computer problems are presented at the
end of this tutorial. We will also examine the eight basic batch
file commands as well as the AUTOEXEC.BAT file which starts your
computer each morning. In addition we will present some simple
tricks for managing your printer with batch files.
One way to think about a batch file is that it takes the
place of your keyboard and issues commands one after another
until it reaches a conclusion. Batch files operate line by line
and are read directly from the disk which makes them a little
slow, but nevertheless useful and flexible. You can eliminate
repetitious keyboard tasks by using batch files. Let's look at a
short batch file . . .
Each line of a batch file contains one instruction or operation
per line which the computer is to perform. Below is the listing
of a simple batch file example. Don't worry about understanding
it yet, simply note that each instruction is a DOS command on a
separate line. The list in the left column is the actual batch
file, while the explanation in the right column is NOT part of
the batch file, only a helpful column of comments.
EXAMPLE BATCH FILE EXPLANATION
| |
date date displayed
time time displayed
ver DOS version displayed
dir a:/p directory of a: floppy
displayed with a pause
The primary use of batch files is to automate sequences or
instructions which you use frequently. A batch file always has
the extension BAT. A batch file might for instance be named
MENU.BAT, CAR.BAT, INSTALL.BAT or MONEY.BAT.
Each line in a batch file is a separate command and is performed
in sequence as if you had typed in the command from your
keyboard at the DOS prompt. In addition to the usual DOS
commands, batch files can also contain additional special
commands to provide truly sophisticated program structures which
include decision branching and even repetitions of commands.
In addition, batch files may have special parameters or inputs
passed to them at the time you run the batch file from the DOS
command line or prompt.
A batch file is run or started by typing the file name without
the extension. This of course also applies to files ending with
file extensions EXE or COM as well as BAT.
Example: A>hello (Then pressing enter or return key)
This starts the file hello.bat hello.com or hello.exe
Example: C>whoops (Then pressing enter or return key)
This starts the file whoops.exe whoops.bat or whoops.com
There are several ways to abort or terminate any batch file in
progress. 1) Issue the break command which uses the two key
combination CONTROL-BREAK (hold down the control or CTRL key
then press the break key) or you can 2) Tap CTRL-SCROLL LOCK
keys or 3) Tap CTRL-C keys.
There are many ways to prepare a batch file, all of which use
simple methods of text editing or word processing: 1) Use the
DOS COPY CON (copy console) command. 2) Use the older EDLIN line
editor available within DOS. 3) Use the newer DOS EDIT text
editor available in DOS version 5.0 4) Use any word processor
(e.g., Microsoft Word, Wordperfect, PC-Write) whose output has
been set to ASCII or pure text output - many word processors use
a "save as" file option to select pure ASCII output. See your
word processor reference book index under ASCII file saving.
Let's prepare a batch file:
First make sure you have a formatted disk in your disk drive and
DOS is displaying a DOS prompt such as A> or C>. We need a disk
in order to save our batch file.
We will be using the command COPY CON (copy data from the
CONsole) command. We could also use any ASCII (plain english)
text word processor (e.g., Wordperfect) or even EDLIN on your
DOS disk. Note that you can use either upper or lower case to
prepare batch files (capitals or small letters.) Using COPY CON
is like using a small typewriter to prepare your batch file.
Type the following list carefully at the DOS prompt:
copy con blink.bat (press enter - cursor skips to new line)
echo Hello there (press enter)
ver (press enter)
date (press enter)
dir/p (press enter)
^Z (press F6 OR your can press control key
AND Z key, then press enter)
When done, you'll have prepared a batch file of DOS commands
named blink.bat. Run the batch file by typing this at the DOS
prompt:
blink (then press enter key)
WARNING! Be careful when preparing batch files since you will
automatically overwrite and destroy any PREXISTING batch files
of the same name! Better to make a backup copy of the existing
batch file (or rename it temporarily with the REN command) and
then proceed. A classic beginner mistake is to tinker with the
crucial AUTOEXEC.BAT file without saving a backup copy first!
More about AUTOEXEC.BAT later in this tutorial.
We could also have named the batch file above hello.bat or
info.bat rather than blink.bat by changing the first line we
typed, but for simplicity we'll stick with blink.bat which does
the following chores: Print "hello there" on the screen, then
type the DOS version in use then display date and finally
produce a directory listing with pause after each screenful. At
this point the batch file ends and returns you to DOS. In the
first line we use COPY CON as our small word processor to begin
construction of the batch file named blink.bat. In the last line
the ^Z means end of batch file preparation - exit back to DOS
and save the file on disk.
Another example batch file for you to try, let's call it F.BAT
This is a reminder that F.BAT refers to formatting a disk:
echo off
copy con f.bat
cls
pause
format b:
echo all done
Notice that here I have omitted the COPY CON command to start
file preparation and the F6 to end file preparation. Use the
COPY CON method described above if you wish or whatever word
processing software is available to construct the file.
This batch file (activated by typing f then enter) will clear
the screen then prepare to format a blank disk in b: drive.
NOTE: you MUST have FORMAT.COM, the DOS formatting utility, on
the same disk as the batch file, f.bat - remember that format is
an EXTERNAL command and f.bat will try to find FORMAT.COM.
After the batch file has formatted the disk it prints "all done"
on the screen. So instead of LOTS of keystrokes to format a
disk, you just tap "F" then hit enter and the batch file runs.
See how we are saving keystrokes - that's one of the purposes of
a batch file! We will discuss the new ECHO command a little
later in this tutorial.
A reminder: Ctrl-Break or Ctrl-C key combination will halt
any batch file operation if you wish.
The next batch file might be used to backup word processing data
files from your hard drive onto a floppy disk. Let's make an
initial assumption that your word processing documents are
stored on your hard drive in the subdirectory C:\DOC. You could
name this backup batch file B.BAT and when you need to backup
simply type B (then press enter) at the DOS prompt. We've
omitted the copy con command at the top of the file and the ^Z
at the end of the file since you already know how to start and
end a batch file from previous examples. Note the new commands
we are using: REM, ECHO and PAUSE which we will discuss shortly.
ECHO OFF
ECHO This batch file backs up DOCUMENT files to disk B:
ECHO READY TO BACKUP.
PAUSE
COPY C:\DOC\*.* B:
ECHO All done!
The line which does most of the work is COPY C:\DOC\*.* B:
which translates as "copy all files from C:\DOC subdirectory
and transfer them to B: drive."
One batch file can start or call another, but the original batch
file cannot usually be returned to - you must continue on within
the second batch file. For example, you could have one batch
file start another batch file.
If a batch file contains a typing or syntax error in any of its
commands, the computer will stop execution at that point and
return you to DOS which remembers which disk contains the batch
file and the drive it was in. If you remove the original disk,
DOS will ask you to replace it so it can finish executing the
batch file. Batch files execute one step at a time from the disk
and NOT from RAM memory. This disk-based nature of batch files
make them a little slow, but they get the work done in
reasonably short order for most people.
Several books and power user tricks should also be mentioned
regarding batch files before we move on . . .
A superlative book on batch files you might wish to investigate
is MOS-DOS Batch File programming by Ronny Richardson, 1988,
Wincrest Books.
You should also investigate the SEBFU (Scanlon Enterprises Batch
File Utilities) software package which is a series of small
batch file utilities which offer an improvement over the
standalone DOS batch file programming language. SEBFU allows the
user to produce subtle, powerful batch files and includes an
excellent tutorial about using batch files for productivity. If
you wish to try SEBFU, the shareware version, contact Scanlon
Enterprises, 38354 17th ST E #C, Palmdale, CA 93550 Telephone
(805) 272-4827. Include five dollars for shipping and handling.
Special batch file COMPILER utilities exist which speed
execution of batch files and make them run from RAM memory
rather than disk. Most computer clubs and BBS system carry these
batch file compilers. One popular batch compiler is named
BAT2EXEC and was produced by PC Magazine several years ago.
Batch files will FLY once they have been compiled and run
from memory rather than disk. Another batch file speedup trick
uses a "ramdisk" as follows.
Remember the DOS VDISK command in our second DOS tutorial? Many
DOS experts put commonly used batch files in a virtual or RAM
disk in memory where a batch file runs quickly. This is one
trick which can turbocharge batch file operations.
----------------------------------------------------------------
AUTOEXEC.BAT FILE BASICS - THE WAKEUP CALL TO YOUR COMPUTER
----------------------------------------------------------------
The AUTOEXEC.BAT file starts your computer exactly the way you
want. It allows you to customize the machine to your liking as
the computer comes to life. You can cause the AUTOEXEC.BAT file
to print a startup menu of choices, load one particular program,
execute another batch file or other useful tasks. The
AUTOEXEC.BAT file is the first file DOS runs after loading
itself and configuring the computer. The AUTOEXEC.BAT file must
be on the same disk as DOS when the computer starts.
The AUTOEXEC.BAT file is a special batch file which MUST be
placed in the main or root directory of a disk to function
properly.
An AUTOEXEC.BAT file can always be modified, enlarged, edited,
or deleted later as you wish. Sometimes it is useful to have
several AUTOEXEC.BAT files. Each on a different startup disk to
operate different programs! An AUTOEXEC.BAT file, like all batch
files, can be modified with any word processor, DOS EDIT or
EDLIN text editor.
Before tinkering with your AUTOEXEC.BAT file, make sure you do
not accidentally over-write or destroy your current AUTOEXEC.BAT
file. If necessary, rename your current AUTOEXEC.BAT file (using
the rename or REN command) and make a new file while saving the
old one "just in case." Never edit files on your original DOS disk,
work on a copy! This wise advice applies to ANY computer file.
Save a backup copy; never work on the original.
Examine the next batch file:
copy con AUTOEXEC.BAT (press enter)
123 (press enter)
^Z (press enter)
This means (first line) create a file named AUTOEXEC.BAT as
typed from the keyboard or con (console). Then (second line)
start program named 123. The (final line) end of batch file
preparation - stash it on the disk. Since the first and last
lines prepare the batch file, this program really has only one
line whose purpose is to start a specific program (123.EXE) each
time the computer is turned on.
When finished you'll see a file named AUTOEXEC.BAT on your
directory listing screen which contains automatic startup
instructions. If this file were placed on your main DOS disk it
would try to start a program such as 123.EXE if such a program
existed there. And since it is AUTOEXEC.BAT this would be the
first file run each morning when you turn on your computer.
You can also start the AUTOEXEC.BAT by typing autoexec and then
pressing enter. To take a "peek" at the contents of an
AUTOEXEC.BAT file (or any bat file) simply use the type command.
Remember to use Ctrl-S key combination to pause the screen if
the display flashes by too quickly.
Example: C>type AUTOEXEC.BAT (display file contents)
Example: A>type b:AUTOEXEC.BAT (display file on the B: drive)
Example: C>type AUTOEXEC.BAT>PRN (display file contents on
printer)
Here is another AUTOEXEC.BAT file, this time from a hard drive
computer. It provides a higher degree of control and direction
that a computer user might need for hard drive customization.
path \dos;\reflex;\wp;\util;\doc;\nor;\bat
prompt $P$G
cpu n
verify on
blank
mode bw80,r
dispclk
type menu.txt
Let's examine this more complicated AUTOEXEC.BAT file in greater
detail:
The first line after establishes a path command to help DOS
search every subdirectory on the hard disk -you don't have to
switch around to different areas of the disk, DOS will search
for you since it knows the various subdirectory "paths" to take.
The second line alters the cursor prompt to always display your
current location and subdirectory. Instead of seeing C> you view
a more informative C:\DOCS> for example.
The third line is a reference to the speed the computer will
operate at and is a unique command to a particular brand of
machine (cpu n means start the central processing unit chip at
normal speed.) Cpu is really CPU.COM, an external file which
sets the computer's processing speed. Your DOS disk may or may
not contain the file CPU.COM. This highlights the ability of the
AUTOEXEC.BAT file to start or load other programs and is
very useful!
The fourth line turns on the verify function for file copying.
The next line instructs the DOS mode function to switch to black
and white display, 80 columns wide and shift one column to the
right for alignment. We are setting the hardware the way we
wish. We could also configure the modem or printer with the mode
command.
Next we ask DOS to tell us the time and date. Run the program
DISPCLK.COM, an external program stored on disk.
The final line instructs DOS to type to the screen a text file
containing a simple menu for the monitor to display. Menu.txt
probably gives us choices of programs and thus calls other batch
files.
----------------------------------------------------------------
BATCH FILE COMMANDS AND USE
----------------------------------------------------------------
In addition to the normal DOS commands, batch files have eight
special subcommands. At the end of this section we will provide
some interesting batch files which you can use or edit on your
computer.
The special batch commands are:
--- REM --- The rem command sends a message to the screen or
simply documents or notes a part of a batch file's operation.
You should use REM extensively to document long detailed batch
files so you can revise things and locate portions of the
program if you decide to change the batch file later. Remarks
can be up to 123 characters long. REM does not cause any
operation, it merely documents what you want to say or do.
Example: C>REM this is the location of menu operations
In DOS 2.0 the REM command could be replaced with a period or
dot, but this is not true in DOS 3.0 and above.
Example: C>. this is the location of menu operations
--- PAUSE --- Stops batch file execution on a temporary basis
until you press a key. Thus you can pause a batch file and do
some operation (perhaps changing a floppy disk) and then
continue when you strike a key. Very useful.
Example: B>PAUSE
Example: B>PAUSE This is an optional message, pardner!
In the first example, no message is displayed.
--- ECHO --- Turns display listing of commands on/off. It can
also send a message to the screen. It is frequently turned off
to remove excessive screen messages. Normally, with ECHO on,
screen messages are sent to the screen which can be distracting.
To suppress them use the first example. To restart the messages
use the second example. To add a message with the ECHO command
see example three. REM or remark command can also send a message
to the screen but NOT with ECHO turned off!
Example: A>ECHO OFF
Example: A>ECHO ON
Example: A>ECHO It's raining cats, dogs and computers
Example: A>@ECHO (don't display this particular line)
--- PARAMETERS AND MARKERS ---- This is NOT a batch file
command like ECHO or PAUSE.
Instead parameters are additional pieces of information or
"modifiers" which follow DOS commands.
Example: C>format b:/s
In the above, format is the command while b: and /s are the
parameters. Parameters modify the basic operation of a DOS
command but are not required by the command to operate. A batch
file can also accept parameters such as a word, filename,
symbol, drive letter or any useful character or group of
characters!
Markers placed inside the batch file listing signify which
parameter goes where. Markers are made from a percent sign (%)
and a single digit between 0 and 9 for a total of ten markers
available (remember, zero is a number too.) Here are the ten
markers:
%0 %1 %2 %3 %4 %5 %6 %7 %8 %9
Let's use an example. Pretend that DOLITTLE.BAT is on your
floppy. Within its listing of commands there might be this
single line:
ECHO %0 %1 %2 (ECHO shows messages on the monitor)
If at the DOS prompt you typed:
B>DOLITTLE fancy pants (then press enter)
Your screen would show the following:
ECHO DOLITTLE fancy pants.
In this case, %0 has taken on the value at the start of the DOS
command which is the first word "DOLITTLE". Meanwhile %1 has
become "fancy" and %2 is now pants.
Looking at this another way:
DOLITTLE fancy pants
| | |
ECHO %0 %1 %2
Let's try a more useful example. Pretend you had a large file of
word processing files containing bills you have to pay from time to
time.
You need to look up bills or amounts in the file accounts.txt
which is in plain ASCII (english) text from your word processor.
The DOS FIND utility can search large files for specific words,
strings or characters. The general format for the FIND command
is: FIND "text" filename. FIND is located in the file FIND.COM
on your DOS disk and must be present with the batch file to be
used.
A simple batch file possibly named GET.BAT could do this:
ECHO OFF
ECHO searching for data . . . .
FIND "%1" %2
ECHO Finished, boss
Start the batch file get.bat with search data like this:
C>get grocery accounts.txt (first word starts get.bat, second
word is the item to search for, third item is the file to
search.)
As a result, you will get a report of the line where the word
"grocery" is found within the file accounts.txt. This could also
be used to search a telephone list or list of employee names and
addresses. A powerful idea for a short batch file!
--- GOTO --- Jumps to a labeled set of commands within the
batch file. The general format for the command is GOTO LABEL
where LABEL is a line in the batch file which must start with a
colon (:) followed by a name up to eight characters long.
A simple, but useless batch file illustrates the GOTO command by
looping around in circles doing the same task endlessly.
Example listing for batch file:
ECHO OFF
:kitty
ECHO watch this fill your screen over and over, folks
GOTO kitty
Note! On some versions of DOS it is necessary to include one blank
line at the end of this file. In the above example, just press
Enter/Return key one extra time after the line "GOTO kitty"
and then save the batch file.
The above batch file will continue to print the remark line over
and over since it always returns to the start. Tap Ctrl-Break to
stop this silliness. The true usefulness of the GOTO command is
best understood by allowing the GOTO within a batch file to
transfer control elsewhere within its listing rather than to the
line immediately next in sequence. You can thus cause varying
useful results depending on a conditions present. Choices and
different outcomes are a trademark of savvy batch file use.
--- IF --- Allows conditional operation of a command. This is a
fancy way of saying you can cause a batch file to make decisions
based on a logical condition or input then do something. The
usual syntax of the IF command is IF CONDITION COMMAND. Let's
take this apart and examine the concept.
In the situation IF CONDITION COMMAND:
COMMAND is any normal DOS or batch file command and CONDITION is
one of three possible tests that yield true or false.
Example: IF %1==w GOTO dog (we'll explain this in a bit)
Example: IF %3 == 80 MODE BW80 (we'll explain this in a bit)
The three possible tests are:
1. The ERRORLEVEL condition (i.e., a specific number is found).
2. The STRING COMPARISON. (i.e., two strings are equivalent or
not.)
3. The FILE EXISTENCE condition. (i.e., if a file exists or not.)
In true full-featured programming languages many other logical
tests might be allowed, but for batch files these are the
only three tests. Let's examine the three more closely. Then
illustrate with an example.
1. ERRORLEVEL is a number which tells DOS whether the last
program run was successful. If so the errorlevel is zero (0)
anything else above zero means unsuccessful.
2. STRING COMPARISON, the second conditional test, is always
indicated in a batch file by double equals signs (==). A test is
designated by the condition IF string1 == string2. This is
frequently used with parameters or markers such as: IF %3 == 80
MODE BW80.
3. In the final and third conditional test, FILE EXISTENCE, the
usual format is IF EXIST d:filename.ext. which checks for a
certain file on a certain drive. You can thus check for a
certain disk or file before continuing the batch file process.
Pathnames are not allowed (d:\slip\and\slide).
Let's try a batch file example to illustrate the use of STRING
COMPARISONS to make a choice in how the batch file does its
work. In a way, this is a menu program. Pretend you have two
software applications. One is a word processor named WORD.EXE
whose command to start is WORD and the other is a spreadsheet
named LOTUS.EXE whose command is LOTUS to start.
If we prepared a simple batch file called go.bat whose listing is
below, we could start one or the other program by using either
the command:
A>go w (to start the word processor)
OR THIS:
A>go s (to start the spreadsheet).
Notice how the "w" or "s" is picked up by the batch file and
sends the program either one direction or the other in the
example below. The remarks lines which begin with REM in the
batch file give you a clue about the operation of the program
but are not themselves commands. The end result of this batch
file is a saving of keystrokes for frequently used software (the
word processor and spreadsheet) and could be expanded to start
many other software packages.
REM This batch file selects one of two choices based on input
REM The next line turns off screen echo to avoid screen clutter
ECHO OFF
REM Begin test for one of two choices
REM Next two lines use percent signs as markers for "w" or "s" keys
IF %1==w GOTO dog
IF %1==s GOTO cat
REM Next line forces goto end if no match is made for w or s
GOTO end
:dog
REM Next command starts word processor, WORD.EXE
WORD
GOTO end
:cat
REM Next command starts spreadsheet, LOTUS.EXE
LOTUS
GOTO end
:end
REM Next line switches to root directory and ends the batch file
CD\
ECHO Batch file done, bye bye!
--- SHIFT --- Re-assigns the relationship of parameters to
markers. It changes their values. And it does it in a very odd
way . . .
Remember that there are only ten markers available to a batch
file to hold the parameter values as we mentioned above. Here
they are:
%0 %1 %2 %3 %4 %5 %6 %7 %8 %9
However you can raise the limit of 10 parameters in a batch file
using the single word SHIFT. When this command is encountered in
a batch file, all the parameter and marker pairings are shifted
one unit to the left. Whatever was assigned to %0 is lost.
A diagram to visualize. Before a SHIFT command is issued the
parameters and markers might be:
%0 %1 %2
| | |
dog cat computer
After the SHIFT command we would see:
%0 %1 %2
| | |
cat computer
Notice that dog is lost, %1 becomes computer and %2 is left
vacant unless it takes a new parameter from %3 (if %3 had a
parameter). The effects of the SHIFT command are wide ranging
throughout the batch file and provide great flexibility and a
range of parameters greater than ten values.
--- FOR..IN..DO --- Allows iteration (repetition) of actions
or commands. The command is similar to a FOR...NEXT...STEP loop
programmers use. This command lets you repeat an action several
times.
The command is rather subtle and could be thought of as a three
part command. The syntax is:
FOR %%Variable IN (Set) DO Command
Let's look more closely at the three parts:
FOR %%Variable IN (Set) DO Command
============== ======= ==========
| | |
part 1 part 2 part 3
Translating into English this means: FOR a certain batch file
variable withIN a SET of filenames or commands DO a certain
action.
The %%VARIABLE is a one-letter variable which must have a double
%% prior to the letter to distinguish it from single % markers
we have seen earlier.
The SET portion of the command is always in parenthesis as
(SET). The SET represents filenames or DOS commands you want the
%% variable to assume while the command is executing. A space is
used between entries. Pathnames are never allowed but wildcards
such as *.* are acceptable. If the SET contains DOS command then
only the %%VARIABLE is used.
The COMMAND is a DOS command or batch subcommand. One or several
of these commands will contain the %%Variable in it.
Let's try an example. Pretend by you want a batch file to
present the DOS version then clear the screen and finally issue
the directory. We could do this in three lines by:
VER
CLS
DIR/P
However, with the command FOR..IN..DO we can do this in one
line:
FOR %%T IN (Ver cls Dir/P) DO %%T
Notice how each DOS command is separated by a space. ? and * are
NOT allowed within any command within the SET. Use a colon :
instead of a space within the set when passing parameters to
programs. You can issue the FOR..IN..DO batch file subcommand at
the DOS prompt by dropping one of the percentage signs % on
the variable. Let's move on to some practical and fairly
interesting examples . . .
----------------------------------------------------------------
BATCH FILE PROJECTS FOR YOU TO TRY!
----------------------------------------------------------------
The following batch files do real work and can teach you some
fascinating principles.
To save typing each example, here's a shortcut: simply load this
file, BATCH.TUT, from this disk or hard drive into your word
processor. Next, delete the tutorial and batch files you don't
need, keeping only the batch file lines you require. Move all
lines of the batch file to the far left margin then save the
batch file back to disk in plain ASCII text with a permanent
batch file name such as CANDY.BAT, GO.BAT or PRINTER.BAT which
you can easily remember. Feel free to change or add things to
these batch files with your word processor since that's the
point - batch files are flexible!
Note the liberal use of the remarks or REM lines in some batch
files to explain how things work. If you like, delete all REM
lines to save typing time, space and speed program execution!
---- Fast freespace batch file ----
This is a short but useful batch file. It reports the amount of
freespace on a disk by using the FIND command in DOS to search
out the line containing the word "free" in the DOS DIR command.
Program name: TELLFREE.BAT
ECHO OFF
CLS
ECHO CHECKING FOR FREE SPACE ON DISK
DIR | FIND "free"
You could modify the last line to DIR A: | FIND "free" to locate
the free space on the A: drive. The piping symbol | , discussed
in a previous DOS tutorial illustrates how one command (DIR) can
"pipe" its output into the FIND command. You MUST have the DOS
file FIND.EXE on the same disk so that the DIR command can use
it. The word "free" is case sensitive and must MOT be typed in
capital letters since the FIND command is case sensitive.
Obviously, to use this small program, at the DOS prompt just
type TELLFREE then press the return or enter key. You could also
name this batch file T.BAT if you wanted to only type a
single letter and save additional keystrokes.
---- Fast deletion batch file ----
This is a short batch file with some powerful wrinkles for
speedy file deletions. It could be modified in many ways.
Program name: DB.BAT
ECHO OFF
CLS
ECHO Ready to delete ALL files on B: drive
ECHO Press control-break keys to abort or
ECHO any other key to continue
PAUSE
REM Following line does the deleting
ECHO Y | DEL B:*.*
ECHO DONE!
This is an odd batch file using some powerful DOS secrets. Line
four reminds us we are about to delete all files on the B: drive
and gives us the chance to abort using the control-break key
combination which works to abort all batch files. The pause
command on line six halts operations and waits for our keypress.
Line eight shows the real power of DOS in a one line command
which uses ECHO to pass the keystroke Y (meaning yes) via the
pipe operation of DOS represented by the vertical bar |. This
effectively means that the deletion of all files on B: drive
represented by *.* will take place WITHOUT pausing for the
traditional Yes/No request. The name of the batch file, DB.BAT
reminds us that its use is to delete all files on B: drive or
DB, for short!
---- Fast formatting batch file ----
This batch file will speed your disk formatting. Its name
F.BAT means format disks, and it gives you some choices and
illustrates other batch file tricks.
There are three ways to use it: for formatting A: drive, B:
drive, or both A: and B: drives. Note the minimum number of
keystrokes required and how the batch file determines your
choice by using parameters. This batch file also requires a
small text file called yes.txt which shows how a file can be
used to redirect input to a DOS command in place of the
keyboard. It is explained below. Be sure to prepare YES.TXT or
F.BAT will not work!
To start this batch file you MUST chose one of the following.
The space between letter characters is important.
To format only A: drive, at DOS prompt enter F A
To format only B: drive, at DOS prompt enter F B
To format both A and B drives, at DOS prompt enter F AB
In the above commands, the A, B or AB will be inserted into the
batch file in the location of the symbol %1 as noted earlier in
this tutorial.
Program name: F.BAT
ECHO OFF
CLS
ECHO FORMATTING DISKS NOW!
REM This batch file selects one of three choices based on input
REM Begin test for one of three choices
IF %1==A GOTO DOG
IF %1==B GOTO CAT
IF %1==AB GOTO MOUSE
REM Next line forces goto end if no match is made
GOTO end
:DOG
FORMAT A:<yes.txt
GOTO end
:CAT
FORMAT B:<yes.txt
GOTO end
:MOUSE
FORMAT A:<yes.txt
FORMAT B:<yes.txt
GOTO end
:end
Lines 18 and 19 provide formatting of B: immediately after
formatting of A: is finished. A short but very powerful batch
file.
Note, how the batch file cleverly uses redirection with the
< symbol discussed in our earlier DOS tutorial to send the
"stored keystrokes" in the file yes.txt to the format command so
you do NOT need to type Yes/No each time the computer formats a
disk which is usual when using the format command. Redirection
is a very powerful DOS operation. You will also need the file
YES.TXT as discussed below.
Program name: YES.TXT
This is a very simple file. Start your word processor, DOS Edlin
or even use the COPY CONsole command earlier. This tiny file
contain a "y" character and two carriage returns and serves as
an input file to take the place of the keyboard as explained
above. It provides the format command with "simulated" keyboard
response of Y (Yes) so the batch file above can continue.
Using Copy Console command (which is one method to prepare
YES.TXT):
COPY CON YES.TXT (press enter)
y (press enter)
(press enter, skip to new line)
(press enter, skip to new line)
^Z (press F6 to end and write file to disk)
---- Make a menu batch file project ----
The next project is actually four simple batch files which work
together to provide a "poor man's" menu program for a hard
drive. Note the automatic switching between subdirectories
provided by the files 1.bat, 2.bat and 3.bat. Obviously you
could expand this to include menu choices for more options. Type
in each batch file program and save on your hard drive. Place
all files in the root directory of your hard drive, usually C:\
When ready to start, simply type M, then press enter key. Notice
how M.BAT runs one of the other three files, 1.BAT, 2.BAT or
3.BAT. Notice also how when those batch files are done, they
automatically run M.BAT to return from where they began and
again display the menu screen.
Program name: M.BAT
REM Third line turns off echo so commands are not repeated to
REM the screen twice, avoids screen clutter
ECHO OFF
REM Next line clears the screen
CLS
REM Next line suggests option 1 which will run 1.bat
ECHO PRESS 1 FOR WORD PROCESSOR
REM Next line suggests option 2 which will run 2.bat
ECHO PRESS 2 FOR SPREADSHEET
REM Next line suggests option 3 which will run 3.bat
ECHO PRESS 3 FOR DATABASE
ECHO PRESS ENTER KEY AFTER SELECTION IS MADE
Program name: 1.BAT
ECHO OFF
REM Fourth line switches to a subdirectory containing your
REM word processor using the cd or change directory command
CD\WP
REM Seventh line starts your word processor, named word.exe
REM Substitute the startup command for your word processor
WORD
REM Eleventh line changes out of word processing subdirectory
REM and goes back to the root directory when word processor
REM is finished
CD\
REM Final line restarts the menu program m.bat
M
Program name: 2.BAT
ECHO OFF
REM Fourth line switches to a subdirectory containing your
REM spreadsheet using the cd or change directory command
CD\SPREAD
REM Seventh line starts your spreadsheet, named lotus.exe
REM Substitute the start command for your spreadsheet
LOTUS
REM Eleventh line changes out of spreadsheet subdirectory
REM and goes back to the root directory when spreadsheet
REM is finished
CD\
REM Final line restarts the menu program m.bat
M
Program name: 3.BAT
ECHO OFF
REM Fourth line switches to a subdirectory containing your
REM database using the cd or change directory command
CD\DATABASE
REM Seventh line starts your database, named db.exe
REM Substitute the start command for your database
DB
REM Eleventh line changes out of database subdirectory
REM and goes back to the root directory when database
REM IS FINISHED
CD\
REM Final line restarts the menu program m.bat
M
---- Printer Control Batch file ----
Some of the best kept DOS secrets are fascinating. If you have a
standard dot matrix printer connected to your computer, you can
use simple batch files to change the typeface and other features
your printer provides. The reference booklet which accompanied
your printer discusses printer control codes. For example, on
Epson compatible printers you will note in your printer book
that "emphasized printing" has the DECIMAL control code 27 69.
Let's write a batch file to take charge of our printer!
Program name: E.BAT
ECHO OFF
CLS
ECHO SETTING PRINTER TO EMPHASIZED MODE
ECHO (ALT 155)(ALT 69) >PRN
ECHO DONE
The fourth line contains a secret trick. The code we need to
send is 27 69 according to our printer book. When preparing this
batch file with EDLIN, COPY CONsole or your word processor, you
must send the printer control code 27 then 69 to the printer.
In line four after typing the word "ECHO" then a blank space,
hold down the ALT key then press 155 on the FAR RIGHT NUMERIC
KEYBOARD. When done entering the number, release the ALT key.
On most computers, the cents symbol will appear which the
printer will accept as the "escape code 27." DO NOT type the
left and right parenthesis marks which appear on line four: ( ),
they are only for clarity.
Next press ALT 69 which produces the E symbol. You could also
just type capital E. Next type >PRN which sends this code
to your printer. Note that there is NO blank space between (ALT
155) and (ALT 69).
The code 155 is substituted for 27 but the 69 is unchanged. Why
155 rather than 27 for the escape code? An explanation: Printer
control codes begin with code 128, thus escape character 27 is
generated by using 27+128 = 155. An necessary trick for this
batch file. We could send SEVERAL codes by adding more lines to
the batch file to set letter quality, pica font, line spacing
and tabs, then type the letter to the printer (ECHO LETTER.TXT>PRN)
and finally reset the printer. Your printer book discusses these
decimal control codes.
If we wanted to send a formfeed to the printer (eject paper,)
the printer reference book suggests control code 12, therefore
12+128 = 140. So the fourth line in the batch file would read
ECHO (ALT 140) >PRN. The bell sound (decimnal code 7) is
generated by 7+128 = 135. The batch file would change to read
ECHO (ALT 135) >PRN.
Some printer features are controlled by SINGLE control codes
while other features are controlled by MULTIPLE escape sequence
codes which always begin with 27 followed by additional numbers.
Escape code 27 is always translated to ALT 155 when DOS and
batch files transmit the printer control information. Within
software programs the codes may use a different format: \027E
for example.
Many other printing features can be turned on using short batch
files. For example, double strike printing uses the decimal code
27 71. In the batch file you could use ECHO (ALT 155)(ALT 71) >PRN.
You can also turn on a COMBINATION of several features in one
longer batch file to control several features. Just add more
lines and codes to the batch file!
To reset the printer when a printing job is finished: code 27
64. Search out these printer codes in your printer manual and
let a batch file do the hard work!
One of the best batch file tutorials to date was published in
two parts in the November and December 1991 editions of PC
Computing Magazine. Contact your library for back issues or
contact PC Computing at Back Issues Dept, PC Computing Magazine,
Ziff Davis, POB 53131, Boulder, CO 80322. Back issues currently
cost $6.00 each.
The bibliography/suggested reading list with this disk provides
additional reading suggestions to advance your batch file
knowledge.
Tutorial finished. Be sure to order your FOUR BONUS DISKS which
expand this software package with vital tools, updates and
additional tutorial material for laptop users! Send $20.00 to
Seattle Scientific Photography, Department LAP, PO Box 1506,
Mercer Island, WA 98040. Bonus disks shipped promptly! Some
portions of this software package use sections from the larger
PC-Learn tutorial system which you will also receive with your
order. Modifications, custom program versions, site and LAN
licenses of this package for business or corporate use are
possible, contact the author. This software is shareware - an
honor system which means TRY BEFORE YOU BUY. Press escape key to
return to menu.

Vous aimerez peut-être aussi