home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Education
/
collectionofeducationcarat1997.iso
/
COMPUSCI
/
DOSIMP12.ZIP
/
SIMPLY2.HYP
< prev
next >
Wrap
Text File
|
1992-01-16
|
106KB
|
1,902 lines
What Is a |tBatch|t File?
Oh, my, this is fun. |nBatch|n files are what got me so interested in learn-
ing how to use DOS. They're absolutely fascinating! It is a very simple
sort of Programming, in a way. But so simple that anyone can do it!
Well, a |nbatch|n file is just a plain ASCII text file, that contains nothing
except a |nbatch|n of DOS commands, each command on a separate line, and each
line ending with a carriage return (the <Enter> key). The |nbatch|n file can
have any name you want to give it, but it must have the extension .BAT,
and it's a very good idea to not ever give a |nbatch|n file the same name as
any other command on your system.
A |nbatch|n file is an executable file, so when you just type its name at the
DOS prompt, and hit <Enter>, the |nbatch|n file will run, just like any other
command. DOS will read the first line of the |nbatch|n file, and execute the
command, read the next line and execute it, etc., until DOS finds that no
more lines are left to be executed. That's it!
<page down> for more Batch
|nBatch|n files can contain any commands at all, which includes internal com-
mands that reside inside the COMMAND.COM file, .COM files, .EXE files, or
even other .BAT files (but not CONFIG.SYS commands). Some internal com-
mands are almost never used for anything else besides |nbatch|n files. Those
are CALL, ECHO, FOR, GOTO, IF, PAUSE, REM, and SHIFT. Replaceable para-
meters are another thing that is only used in |nbatch|n files. Each of those
items has its own chapter here, so check them out for more information.
Environment variables can be used from within a |nbatch|n file, in a manner
similar to the use of replaceable parameters. If you have an environment
variable named ONE which was set equal to YES via the command SET ONE=YES
then you could reference that variable from inside a |nbatch|n file by sur-
rounding its name with percent signs, as in %ONE%. Here's an example:
IF %ONE%!==YES! GOTO OK
Now when DOS is executing the |nbatch|n file that contains that line, it will
see the %ONE% and go look in the environment to find the variable named
ONE, look at what ONE is set equal to, and replace ONE with that string.
So when DOS gets done with that, the above line will look like:
IF YES!==YES! GOTO OK
<page down> for more Batch
and so the IF test will come out positive, and the GOTO command will be
executed. Now this doesn't actually change the |nbatch|n file at all, just
the way DOS interprets the line. The next time DOS executes the |nbatch|n
file, it still says IF %ONE%!==YES! GOTO OK, but if the environment var-
iable ONE is set equal to NO this time, then when DOS expands that line,
it will say IF NO!==YES! GOTO OK this time, so that the GOTO command will
get ignored because the IF test fails.
The reason for the exclamation points in my examples, is in case you had
forgotten to set the environment variable. If the ONE variable didn't
exist, then when DOS expands the %ONE% variable, it will find nothing at
all, so the command will be like IF !==YES! GOTO OK. That IF test will
fail, but at least it won't cause DOS to freak out. If we left out the !
symbols, and the line said IF %ONE%==YES GOTO OK, then when DOS expanded
that, it would say IF ==YES GOTO OK, if the ONE variable didn't exist,
and since one side of the == signs is empty, DOS won't like that at all,
and you'll get a "Syntax error" message, and the |nbatch|n file will continue
with the next command which might not be what you intended! So you want
to put some symbol on each side of the == signs to protect from that oc-
<page down> for more Batch
currence. You can use the exclamation point like I did or just about any
symbol you want, as long as it's the same on both sides of the == signs.
Here's an example of a way that you can take advantage of environment
variables in a |nbatch|n file. Suppose there's a certain set of commands
that you would like to execute twice in a row, but not more. You could
do this:
SET AGAIN=
:START
(commands you want repeated go here)
IF !%AGAIN%==!NO GOTO END
SET AGAIN=NO
GOTO START
:END
SET AGAIN=
Now, when you execute that |nbatch|n file, the first line sets the environ-
ment variable AGAIN equal to nothing, to make sure the variable doesn't
already exist. The :START line is just a label for the GOTO command that
comes later, so it has no effect the first time through. Then the main
<page down> for more Batch
|nbatch|n file commands are executed, and then comes the IF statement. Well,
since at this point, AGAIN is not set equal to anything, the GOTO END
command will be ignored, and the next line, SET AGAIN=NO will be execu-
ted. Then the command GOTO START tells DOS to start over again at the
label that says :START. So those main commands get executed again, and
then the IF test is positive this time, because you already executed the
SET AGAIN=NO line, so this time the GOTO END command does get executed,
which sends DOS to the line that says :END, and then the AGAIN variable
is once again removed from the environment, and the |nbatch|n file is done.
Another way that the use of environment variables from within a |nbatch|n
file comes in handy, is with the PATH variable. Suppose that you have a
program that you want to run, for which you need to have that program's
directory on the PATH, but you don't want to leave that directory on the
PATH all the time. Well check out this |nbatch|n file:
SET OLD=%PATH%
SET |sPATH|s=C:\WORD;%OLD%
WORDPROC
SET |sPATH|s=%OLD%
<page down> for more Batch
SET OLD=
Now supposing that your PATH variable started out like C:\DOS;C:\UTIL
then while that |nbatch|n file is being executed, each time you reference
the PATH or OLD variables, they will be expanded to say C:\DOS;C:\UTIL
and here's what the |nbatch|n file will look like to DOS:
SET OLD=C:\DOS;C:\UTIL
SET |sPATH|s=C:\WORD;C:\DOS;C:\UTIL
WORDPROC
SET |sPATH|s=C:\DOS;C:\UTIL
SET OLD=
So, the first line makes a variable called OLD which is just a duplicate
of what your PATH variable says at the moment. The second command sets
a new PATH variable, which is C:\WORD; followed by what used to be in the
PATH variable a minute ago. Then the WORDPROC program gets run, and then
the next line puts the PATH variable back to the way it used to be, by
setting it equal to the OLD variable which you created at the beginning
of the |nbatch|n file. And the last line removes the OLD variable from the
environment since it's not needed any more, and we don't want to waste
environment space by just leaving it there for no reason.
<page down> for more Batch
Well speaking of PATH variables, |nbatch|n files are an excellent way to keep
your PATH short. The only directories that should go on your PATH, are
those which contain commands that you need to run with some other direc-
tory as the current one. For commands that you can run from within their
own directory, those commands should not go on the PATH. Instead, you
should use a |nbatch|n file (or DOSKEY macro) to make that command's drive
and directory current, run the program, and change back to the root dir-
ectory. Like this:
C|1:
CD C:\WORD
WORDPROC
CD \
Now you name that |nbatch|n file WP.BAT, and put it into a directory which
contains nothing but |nbatch|n files (name it BELFRY, perhaps, or CAVE, since
that's where .BATs go), and put that |nbatch|n file directory on the PATH,
then you can access your WORDPROC program from any directory on your sys-
tem, just by typing WP <Enter>, even if C:\WORD is not on your PATH. So,
after you create such a |nbatch|n file for each of your main applications,
now the only directories you need to keep on your PATH are the one where
<page down> for more Batch
you keep your |nbatch|n files, the one where you keep your DOS external com-
mands, and the one where you keep other third-party utilities that only
have one or two files per program, such that you wouldn't want a separate
directory for each of them. That last directory you might want to name
UTIL, short for utilities. So, with only three directories on your PATH,
your whole system will be a lot more efficient.
The problem with |nbatch|n files is that each one takes up an entire cluster
of disk space, just like any other file, even though |nbatch|n files are gen-
erally very small. A 28-byte |nbatch|n file takes up 2048 bytes of space on
a hard disk. What a waste! Well you can use replaceable parameters to
combine all of your little |nbatch|n files into one great big |nbatch|n file in-
stead. See the section about the GOTO command to find out how. Now if
you do that, you don't need to keep an entire directory for your |nbatch|n
files, since you only have one |nbatch|n file. So just put your big |nbatch|n
file into the UTIL directory and remove the |nbatch|n file directory from
your disk and from your PATH.
If you have DOS version 5.0, you can do just about everything you can do
<page down> for more Batch
with |nbatch|n files, with DOSKEY macros instead, except for accessing envir-
onment variables, or using the GOTO or SHIFT commands. DOSKEY macros can
not do those things. And DOSSHELL menu items can do everything except
GOTO and SHIFT. So if you upgrade to DOS version 5 you should convert
all your |nbatch|n files that don't do those things, to one of those options.
You still need to learn about |nbatch|n files anyway though, because DOSKEY
macros and DOSSHELL menu items are put together by the same rules that
|nbatch|n files follow.
And you have to know about |nbatch|n files in order to keep a nice efficient
AUTOEXEC.BAT file. That's a very special |nbatch|n file that DOS executes
every time you reboot your computer. It's the only |nbatch|n file that has
to have a specific name, and that has to be located in the root directory
of the disk you boot from, and that in most cases, you never want to ex-
ecute. Just let DOS execute it all by itself. If you want the AUTOEXEC
.BAT to be executed, |sreboot|sing is the best way to get that accomplished.
See the AUTOEXEC.BAT chapter for the reasons.
You might want to have your AUTOEXEC.BAT contain just one command:
<page down> for more Batch
@C:\DOS\STARTUP
and then you would have a |nbatch|n file named STARTUP.BAT in your C:\DOS
directory, which the AUTOEXEC.BAT would run every time you reboot. The
reasoning behind this is that a lot of software programs that you install
are going to add commands to your AUTOEXEC.BAT file. Well if you have
that really simple one-line AUTOEXEC.BAT file, then it will be really
easy for you to figure out what changes an installation program made to
that file so you can decide which of those changes you want to keep. Put
those changes into your STARTUP.BAT file, and remove them from your one-
line AUTOEXEC.BAT file. This lets you control your own system, instead
of letting some software programmer control your system for you.
Since percent signs are used in |nbatch|n files to refer to environment var-
iables and replaceable parameters, you have to do something special to
make a |nbatch|n file understand a percent sign in any other context. Like
if you have a file named HELLO%.TXT, if you mention that filename in a
|nbatch|n file, DOS will flip out over trying to figure out what replaceable
parameter or environment variable you're trying to reference. Because
that's what percent signs are supposed to mean in a |nbatch|n file. Well all
<page down> for more Batch
you need to do, is write that filename with two percent signs whenever
you mention it in a |nbatch|n file. Just refer to the file as HELLO%%.TXT
whenever you're mentioning it in a |nbatch|n file, and DOS will understand
then, that it's supposed to ignore that particular percent sign as far
as special |nbatch|n file processing is concerned.
DOS version 3.3 introduced a special new use for the @ symbol, which is
good for |nbatch|n files. If you put @ as the first character of any command
line in a |nbatch|n file, then that particular line will not show up on the
screen while the |nbatch|n file is executing. For example, you know how the
ECHO OFF command tells DOS not to show the commands on the screen as they
get executed. But when the ECHO OFF command gets executed, it has not
been executed yet so the ECHO OFF command itself does show on the screen.
Well if you have DOS version 3.3 or later, you can say @|sECHO|s OFF instead
of just ECHO OFF, and then you'll never again see the ECHO OFF command on
your screen. Isn't that nice?
One thing that a lot of people try to accomplish with |nbatch|n files, that
just won't work, is to feed commands or keystrokes to another program.
<page down> for more Batch
Suppose you have a game which, as soon as it's loaded, it asks you wheth-
er you have a color monitor, and you have to tell it Y, and then it asks
you whether you want to use the mouse or the keyboard, and you have to
tell it K, and you're tired of typing those silly keystrokes every single
time you run that game. Well you might be tempted to write a |nbatch|n file
like this:
GAME
Y
K
Well guess what's going to happen when you run that |nbatch|n file? The game
will run, and it will wait for your answers to those questions just like
always, and then when you're finished playing the game, and you exit to
DOS, you'll see the "|sBad command or filename|s" error message twice. Why
did that happen? Well it's because the Y and K lines of the |nbatch|n file
don't get executed until after the GAME command finishes and passes con-
trol back to DOS, so that DOS can read the next command from the |nbatch|n
file. And since there are no such commands in DOS, as Y and K, then you
get that error message.
<page down> for more Batch
One line of a |nbatch|n file does not get executed, in fact does not even get
read by DOS, until the line before it is completely finished. There's no
way around that, at all.
But there are two ways to feed information to a program. If the program
will accept STanDard INput, you can use the piping form of redirection.
But if the program reads its input straight from the keyboard, rather
than using DOS's normal STanDard INput, then you will need a little util-
ity called a Keyboard Stuffer instead. There are quite a few shareware
and public domain keyboard stuffers, such as PC Magazine's KEY-FAKE.COM.
These little utilities will feed just about any keystrokes to just about
any program, and they are available for downloading from just about any
BBS in the country.
You can use the COPY CON command, or the EDLIN line editor, or if you
have DOS version 5.0, you have the EDIT command. You can even use the
ECHO command with output redirection. All of these things will create
|nbatch|n files, or any other files you might want to create. EDLIN and EDIT
can also change |nbatch|n files that already exist. Any text editor, and any
<page down> for more Batch
word processor that can save files in plain ASCII format can also do it.
There are even cases in which you can use certain DOS commands to create
|nbatch|n files, from within other |nbatch|n files, to perform certain functions.
For example, if you execute the PATH command with no parameters, it will
tell you what your PATH environment variable currently says, like this:
|sPATH|s=C:\DOS;C:\UTIL
Well, that's the exact same format that you use to enter a new PATH vari-
able! So if you were to type PATH > OLDPATH.BAT that'd use redirection
to create a new |nbatch|n file named OLDPATH and you could change your PATH
to whatever you wanted, and then if you were to later execute OLDPATH
<Enter>, then that |sPATH|s=C:\DOS;C:\UTIL command would be executed, to put
your PATH back to the way it had been before you started! This sort of
thing can be extremely useful. See the end of the DATE chapter for more.
<page down> for The |nCALL|n Command
The |TCALL|T Command
This is a batch file command, which has almost no use at the DOS prompt.
It only exists in DOS versions 3.3 and later. Before that, it was neces-
sary to use the COMMAN|1D /C command to accomplish the same thing. What it
does is to run another batch file from within a batch file, and still re-
turn to the calling batch file after the called batch file is completed.
Usually if you run one batch file from within another one, when the sec-
ond one finishes it just gives you back a DOS prompt, without ever re-
turning to finish the rest of the batch file from which it was run. The
|nCALL|n command takes care of that problem.
Another thing about the |nCALL|n command is that if ECHO was off in the call-
ing batch file, then ECHO will remain off in the called batch file. So
the second one doesn't have to have ECHO OFF as its first line. But if
you want ECHO to be on in the called batch file, then its first line has
to be ECHO ON, even though that's the default for any batch file and it
doesn't normally have to be specified. But ECHO will still be off in the
<page down> for more CALL
calling batch file, when you get back to it.
You can use replaceable parameters in a called batch file also. Just en-
ter the parameters on the |nCALL|n command line, like this:
|nCALL|n BATCH2 ONE TWO THREE
This command would run the BATCH2.BAT file, with ONE as %1, TWO as %2,
and THREE as %3.
You can also call a batch file that is not in the current directory, or
on the PATH, by specifying its full |spath|sname like this:
|nCALL|n C:\BELFRY\BATCH2
If you use |nCALL|n (or COMMAN|1D /C) to run a second batch file from within a
first one, then as soon as the second batch file is done, the first batch
file will continue right where it left off, at the line right after the
|nCALL|n (or COMMAN|1D /C) command.
Of course you can still use the COMMAN|1D /C technique even if you have DOS
version 3.3 or later, but it's almost always more efficient to use |nCALL|n.
<page down> for more CALL
There is one situation where the |nCALL|n command is useful at the DOS prompt
rather than in a batch file, and that has to do with the FOR command.
<page down> for The |nECHO|n Command
The |TECHO|T Command
This is a batch file command, but it does have some uses on the command
line. Its main use is to display text on the screen, or to tell DOS not
to display text on the screen.
Whenever DOS loads a copy of the COMMAND.COM file into memory, whether it
be the first copy or another copy, the state of echo is on. Whenever DOS
finishes executing a batch file, and returns to the prompt, it turns echo
on. What echo on means is that whatever commands are executed from with-
in a batch file, will show on the screen as they are read from the batch
file. If you don't want the commands to show on the screen as they are
executed, then you want to turn echo off. Then later in the same batch
file if you want the commands to display again, you turn echo back on.
You can also use the |nECHO|n command to display your own text on the screen.
And the |nECHO|n command all by itself with no parameters, will make DOS tell
you whether the |ncurrent|n state of echo is off or on.
Suppose you have a batch file named TEST.BAT which contains these lines:
<page down> for more ECHO
DATE
TIME
VER
DIR
Now, you execute that batch file by typing TEST <Enter>. Here's what'll
happen to your screen (minus the blank lines):
C:\>TEST
C:\>|sDATE|s
|nCurrent|n date is Mon 06-24-1991
Enter new date (mm-dd-yy): _
C:\>|sTIME|s
|nCurrent|n time is 05:30:53.72p
Enter new time: _
C:\>|sVER|s
MS-DOS Version 5.00
C:\>|sDIR|s
Volume in drive C is drive C
Volume Serial Number is 16CB-74E4
Directory of C:\
<page down> for more ECHO
COMMAND COM 47855 05-09-91 5:00a
AUTOEXEC BAT 861 06-18-91 4:58p
CONFIG SYS 287 06-17-91 6:58p
3 file(s) 49003 bytes
69025792 bytes free
C:\>
C:\>
Now if you were to add |nECHO|n OFF as the first line of that batch file, and
leave the rest the same, here's what your screen would do instead:
C:\>TEST
C:\>ECHO OFF
|nCurrent|n date is Mon 06-24-1991
Enter new date (mm-dd-yy): _
|nCurrent|n time is 05:30:53.72p
Enter new time: _
MS-DOS Version 5.00
Volume in drive C is drive C
Volume Serial Number is 16CB-74E4
Directory of C:\
<page down> for more ECHO
COMMAND COM 47855 05-09-91 5:00a
AUTOEXEC BAT 861 06-18-91 4:58p
CONFIG SYS 287 06-17-91 6:58p
3 file(s) 49003 bytes
69025792 bytes free
C:\>
A lot less clutter on the screen this time, huh? First of all, until af-
ter the first command has executed, echo is still on, so you see the |nECHO|n
OFF command. (In DOS version 3.3 or later, you can use the @ symbol to
supress the display of any command even when echo is on so if you changed
that line to @ECHO OFF, then it wouldn't show on the screen either.) But
after that, no more |sprompt|ss get sent to the screen, and only the output
of each command shows, not the commands that cause the output.
Then at the end, notice that there is only one prompt this time. You
see, when echo is on, DOS is displaying the prompt for each line, then
looking to see if there is another command in the batch file to be exec-
uted or not. Well after the DIR command, DOS finds that there aren't any
commands left, so it closes up the batch file interpreter and turns echo
<page down> for more ECHO
on (which doesn't matter this time because echo was on the whole time)
and displays another prompt. But when echo is off, DOS is not displaying
a prompt in between each command of the batch file, so that last prompt
that would be showing while DOS looks to see if there are any commands
left in the batch file, doesn't show on the screen. No |sprompt|ss show on
the screen until the batch interpreter is closed down and echo is turned
back on.
How about an example of using the |nECHO|n command to display text onscreen?
|nECHO|n HELLO.
|nECHO|n HOW ARE YOU?
Now if that batch file were executed, the screen would be like this:
C:\>ECHO HELLO.
HELLO.
C:\>ECHO HOW ARE YOU?
HOW ARE YOU?
C:\>
C:\>
Not exactly what you had intended, right? Well make the first line of
<page down> for more ECHO
that batch file @ECHO OFF, leave the rest the same, and execute it:
HELLO.
HOW ARE YOU?
C:\>
Well you can also use the |nECHO|n command at the DOS prompt instead of in a
batch file, but that's only appropriate if you're using redirection to
send the output of the |nECHO|n command to some other device instead of to
the screen. For example, |nECHO|n HELLO > PRN would make your printer type
the word HELLO.
Another sort of a device that you can redirect output to, is a filename.
You could actually write a batch file to disk by using the |nECHO|n command
with redirection. Note that the > symbol creates a brand new file by the
name you specify, and if that file already existed at the time, it gets
erased. A double >> sign means for DOS to add the text to the already
existing file, without deleting the file first. So to create the above
TEST.BAT file, you could use these commands from the DOS prompt:
|nECHO|n DATE > TEST.BAT
<page down> for more ECHO
|nECHO|n TIME >> TEST.BAT
|nECHO|n VER >> TEST.BAT
|nECHO|n DIR >> TEST.BAT
The |nECHO|n command always sends along a carriage return (the <Enter> key)
at the end of each thing that it sends, so that batch file you just cre-
ated does not say DATE TIME VER DIR. It's got each command on a separate
line just like it's supposed to.
If you try to display instructions containing one of the redirection
characters using the |nECHO|n command, redirection will take place and the
results will not be what you expected. For example, a command like:
|nECHO|n Type TYPE FILENAME.EXT > PRN
|nECHO|n to print out this file.
will send the words "Type TYPE FILENAME.EXT" to the printer. In later
versions of DOS, though, you can get the results you wanted by using
|nECHO|n Type "|sTYPE|s FILENAME.EXT > |sPRN|s"
The quotation marks keep DOS from performing the redirection.
<page down> for more ECHO
If you execute the command |nECHO|n all by itself with no parameters, it will
say either "ECHO is on" or "ECHO is off". So how do you get it to send a
blank line to the screen from within a batch file? Well in recent DOS
versions, it will work if you give |nECHO|n. as a command. That's right,
|nECHO|n-period with no <Space> between. If that doesn't work for your ver-
sion, you can try ECHO<Space><F7> which will look like |nECHO|n ^@. The ^@
is the ASCII 0 character which represents the null character, looks like
a <Space>, but isn't a <Space> and in most DOS versions will make |nECHO|n
display a blank line. If you have DOS version 5 though, and you're run-
ning the DOSKEY program, or if you're running any other version and have
your <F7> key programmed via ANSI.SYS to stand for something else, then
<F7> won't give you ^@. In that case, find out how your text editor goes
about accepting control characters in the text, and use |nECHO|n <Ctrl-H>
which is a <Backspace> character. But if you're using a version of DOS
that's so old that |nECHO|n. doesn't work, especially now that Microsoft is
selling DOS 5.0 to the public instead of only to computer dealers, you
should go get version 5.0 and find out just what you've been missing!
<page down> for The |nFOR|n Command
The |TFOR|T Command
This is mainly a batch file command, but it is also very useful at the
DOS prompt. It works sort of like replaceable parameters. Example:
|nFOR|n %%a IN (*.BAT) DO TYPE %%a
What that command will do, is to expand the wildcard specification *.BAT
into all the filenames that fit it, and then type each one to the screen.
This is useful because the TYPE command does not work on wildcards.
How about the DEL command? It doesn't work on more than one filename at
a time unless the filenames can be named by a wildcard specification. So
you could do this:
|nFOR|n %%a IN (FILE1.TXT TEXT2.FIL LETTER3.DOC) DO DEL %%a
This command will replace the %%a in the DEL command, with each filename
listed in parentheses, in turn, and delete each one.
You can also use the %%a variable to fill in for the commands to be exec-
uted, instead of for the parameters to the command. Like this:
|nFOR|n %%a IN (|sTYPE|s PAUSE |sDEL|s) DO %%a FILE1.TXT
<page down> for more FOR
This command will type the FILE1.TXT file to the screen, then pause for
a keystroke, and then delete the file. If, by seeing the file on the
screen, you realize that you didn't really want to delete it, you could
hit <Ctrl-C> instead of any other keystroke, when PAUSE is waiting for
input, to BREAK out of the batch file without deleting FILE1.TXT. If
you hit anything else besides <Ctrl-C> or <Ctrl-Break>, then FILE1.TXT
will be deleted.
Now what would make that particular command really useful, is if you used
a replaceable parameter instead of a particular filename. Like this:
|nFOR|n %%a IN (|sTYPE|s PAUSE |sDEL|s) DO %%a %1
Now if that command were in a batch file named D.BAT and you used it with
a command line like D FILE1.TXT, then FILE1.TXT would be placed where the
batch file says %1, or if the next time you executed that batch file, you
said D LETTER.DOC, then that time LETTER.DOC would be the file that the
TYPE, PAUSE, and DEL commands would act on.
You're probably wondering why it's not just as good to use a batch file
like this instead of that one complicated command:
<page down> for more FOR
TYPE %1
PAUSE
DEL %1
Well that batch file will do the exact same thing, just less efficiently.
Remember that DOS executes batch files by reading the first line off the
disk, then executing it, then reading the next line off the disk, then
executing it, then reading the next line off the disk, etc. In other
words, it's slow. If you put all three of those commands on one line by
using the |nFOR|n command, then you have a more efficient batch file. I
know, it's only a difference of a couple milliseconds if you're using a
decent hard drive. But what if you have a one-floppy-drive system? And
what if the batch file is on one disk and the file you want to delete is
on another? Then you put the batch file disk into the drive and execute
it like D B:LETTER.DOC (remember that on one-floppy-systems, DOS acts
like you have an A: drive and a B: drive, and asks you to switch disks
when necessary) so the first line of the batch file is read from the disk
and then you are requested to put the drive B: disk into the drive, and
the file is typed to the screen. Then DOS needs you to put the batch
file disk back into the drive so it can read the second line of the batch
<page down> for more FOR
file. Then, when the third line of the batch file comes, you have to
change disks again so that DOS can delete the file. Then you have to put
the batch file disk in one more time so that DOS can look to see if there
were any more commands in the batch file or not. Well if the |nFOR|n command
had been used instead, then DOS would only need the batch file disk twice
instead of three times. (Yeah, I know, big deal, right?)
Another way to use replaceable parameters with the |nFOR|n command is to put
them into the parentheses, like this:
|nFOR|n %%a IN (%1 %2 %3 %4 %5 %6 %7 %8 %9) DO TYPE %%a > PRN
Now you can specify anywhere from one to nine filenames on the command
line, and they will be typed to the printer, all with just one command.
Here's a strange one. Suppose you need a way to make a batch file pause
for a couple moments, without requiring any keyboard input to make it
start going again. How about this:
|nFOR|n %%a IN (1 2 3 4 5 6 7 8 9 0) DO CHKDSK
Since there's no %%a in the ending part of that command, the numbers in-
side the parentheses have no effect on the CHKDSK command, but they just
<page down> for more FOR
cause the CHKDSK command to be performed ten times over. This would put
a decent-length pause into your batch file. It's not a real good idea to
do it though, because that's a good bit of wear-n-tear on your hard drive
for no good reason. But it was just an example of how you can use |nFOR|n to
execute the same command some specified number of times.
Personally, I find the most use for the |nFOR|n command at the DOS prompt
rather than inside batch files, even though all the books usually say
that |nFOR|n is just a batch file command. How about if you want to copy
eight files that can't be covered in one wildcard specification, from one
disk to another? Without the |nFOR|n command you have to give the first COPY
command, wait for that to finish, give another COPY command, wait for
that one, give another command, wait, etc. Using |nFOR|n instead:
|nFOR|n %a IN (FILE1.TXT TEXT2.FIL LETTER3.DOC ABCDE.DOC FOOBAR.DOC
TEXT6.DOC LETTER7.FIL FILE8.TXT) DO COPY %a B:
Now as long as that all fits on a 127-character command line, all the
copying will be done while you sit back and watch.
Notice that time I said %a instead of %%a. At the DOS prompt you only
<page down> for more FOR
want one percent sign (%), while inside a batch file, you need two.
You don't have to use %%a or %a. You could use %B or %q or %Z or what-
ever you want. Any letter, upper or lowercase will do, as long as it's
a letter, and it's the same at the beginning and the end of the same com-
mand. For example, |nFOR|n %%a IN (|s*.*|s) DO DEL %%A won't work because DOS
will be looking for some %%A file to delete, when all it has is some %%a
files. DOS does not think of upper and lowercase letters as the same.
That was a good command, though, if it had the same variable at the be-
ginning and the end. |nFOR|n %%a IN (|s*.*|s) DO DEL %%a will do the same thing
as DEL *.* but it won't ask you "Are you sure? (y/n)" because as far as
the DEL command knows, you're only deleting one file at a time instead of
a whole directory.
Just be careful of what you're doing when you use *.* as the filespec in
the parentheses. *.* means all files in the current directory. The cur-
rent directory, not any other directory. Suppose you were in the root
directory of your hard drive and it contains the COMMAND.COM, CONFIG.SYS,
<page down> for more FOR
and AUTOEXEC.BAT files. Now suppose you wanted to delete all the files
in the current directory of your A: drive and you used the command:
|nFOR|n %d IN (|s*.*|s) DO DEL A:%d
That's not going to have the effect you expected! What it's going to try
to delete is A:COMMAND.COM, A:CONFIG.SYS, and A:AUTOEXEC.BAT, whether
those files exist on drive A: or not. It won't even try to delete any
other files on the A: drive. Because *.* doesn't mean the same thing as
A:*.* at all. The command you wanted there was:
|nFOR|n %d IN (A:*.*) DO DEL %d
Here's another real useful thing you can do with the |nFOR|n command. If you
have two directories that contain some of the same files, and suppose you
want directory A to keep all of its files, but you want to delete all the
files that are in the B directory that are also in A. After you check to
make sure that the files that have matching names really are the exact
same files (with the COMP command, perhaps), then you could type:
|nFOR|n %d IN (|s*.*|s) DO DEL \B\%d
from the A directory, and that would delete anything in B that's also in
the A directory. Of course there will be some "File not found" messages
<page down> for more FOR
during that process, for files that are in A but not in B, but that's ok.
In many ways, the |nFOR|n command acts like a miniature batch file, even when
you're using it from the DOS prompt. For example, if you want to use the
|nFOR|n command to execute a batch file, you have to use the CALL or COMMAN|1D
command, even if you're working at the command line instead of within an-
other batch file. Without CALL or COMMAN|1D, the batch file will be exec-
uted with the first parameter in the set in parentheses, and then you'll
get your DOS prompt back. DOS will think it's done with what you told it
to do because the batch file it was working on has finished. The CALL or
COMMAN|1D /C command will cause the batch file, when it's done, to return
control to the |nFOR|n command instead of to the DOS prompt. Here's an exam-
ple, supposing you have a batch file named |nBATCH|n.BAT:
|nFOR|n %a IN (|s*.*|s) DO CALL |nBATCH|n %a
And of course, |nBATCH|n.BAT must have a %1 replaceable parameter inside it,
or else there's no point in trying to feed it a %a parameter. So, the
CALL command is not completely useless at the DOS prompt after all, the
way most people think it is!
<page down> for The |nGOTO|n Command
The |tGOTO|t Command
This is a batch file command, which has no purpose at the DOS prompt.
But in a batch file, it's a great command. It sends the execution to
some other part of the batch file, instead of continuing on with the com-
mands as they are listed in order.
Suppose that you want to perform a certain set of commands based on the
results of an IF test. Like this, perhaps:
@|sECHO|s OFF
IF EXIST AUTOEXEC.BAT |nGOTO|n YES
(here go the commands that you want performed if the file AUTO-
EXEC.BAT does not exist in the current |sdirectory|s)
|nGOTO|n END
:YES
(here go the commands that you want performed if AUTOEXEC.BAT
does exist in the current |sdirectory|s)
:END
The word that comes after the |nGOTO|n command is called a label, and when
<page down> for more GOTO
the label is later repeated in the batch file, at the point where you
want the |nGOTO|n to jump to, the label must be preceded by a colon (:) as
shown in that example. Also, don't forget the |nGOTO|n END statement after
the "no" commands, or else the "yes" commands will be executed after the
"no" commands are finished. And if you have a |nGOTO|n END statement, then
you sure do have to remember to put the :END label in the batch file
somewhere, or you'll get a nasty "Label not found" error message, and
the batch file will just quit and leave you with a DOS prompt.
Another good use for the |nGOTO|n command is in conjunction with replaceable
parameters. You can use |nGOTO|n %1 at the beginning of your batch file, and
that way you could combine all of your little batch files into one great
big batch file, to save disk space (since every file takes a whole clus-
ter of disk space even if the file is only 17 bytes long). Let's say you
have batch files named 1.BAT, 2.BAT, and 3.BAT and you want to combine
them into one batch file called GO.BAT. Here's how:
@|sECHO|s OFF
IF NOT '%1==' |nGOTO|n %1
ECHO The syntax of this command is %0 followed
<page down> for more GOTO
ECHO by a label, where the available labels are
ECHO 1, 2, and 3. For example, to perform the
ECHO tasks that used to be in 1.BAT, you enter
ECHO %0 1
|nGOTO|n END
:1
(here go the commands that used to be in 1.BAT)
|nGOTO|n END
:2
(here go the commands that used to be in 2.BAT)
|nGOTO|n END
:3
(here go the commands that used to be in 3.BAT)
|nGOTO|n END
:END
(See the sections on ECHO, IF, and Replaceable Parameters for more infor-
mation.)
So now you can delete your 1.BAT, 2.BAT, and 3.BAT files (after you've
<page down> for more GOTO
tested to make sure that your new GO.BAT file works right) and free up
two |scluster|ss of disk space. Because GO.BAT is still small enough to fit
all in one cluster, so it's only taking up as much space as 1.BAT used to
take up by itself.
Notice that in a batch file, %0 always refers to the name of the batch
file that the %0 is inside of. So in that example above, every place
that says "%0" in the batch file will be replaced with "GO" when you
actually run the batch file. But if you change the name of the batch
file to MENU.BAT, then you won't have to change the inside of the batch
file, because now every place that says "%0" will say "MENU" instead.
Remember that only the first 8 characters of a label are noticed by DOS,
so |nGOTO|n REMEMBER1 is the same as |nGOTO|n REMEMBER2, as far as DOS is concer-
ned. Having two different labels that are only different in their ninth
character does no good. Also, every time DOS goes looking for a label
that a |nGOTO|n command pointed to, it starts at the very beginning of the
file, and glances at each line, so if you have a :HERE label near the
beginning of the file and a :HERE label near the end, the one at the be-
<page down> for more GOTO
ginning is the only one DOS will ever go to.
You can also use labels as comments, just like the REM command, because
if there is no |nGOTO|n command that points to a particular label, DOS won't
mind, or even notice it. DOS completely ignores any line that starts
with a colon (:), unless it's looking for a specific label because a |nGOTO|n
command told it to. You can write anything you want in a batch file af-
ter a colon, as long as you have the correct label for every |nGOTO|n command
in the file, it doesn't matter if you also have seventy more labels if
you want to. You can also add comments on the same line with a real la-
bel, because DOS is only going to pay attention to the first word follow-
ing the colon. As long as there is a <Space> between the real label and
the comments, DOS won't care.
<page down> for The |nIF|n Command
The |TIF|T Command
This is a batch file command, which almost never has any use at the DOS
prompt. It causes the following command to be executed or ignored, based
on whether or not the |nIF|n statement is true.
There are several types of |nIF|n statements, and each of them can be used
with a |TNOT|T qualifier. Let's take each type separately.
First, there is "IF |TEXIST|T". This is used to check on whether or not a
certain filename exists in the current or specified directory. Suppose
you are in the C:\ directory and you do have a file named C:\CONFIG.SYS,
the command |nIF|n |nEXIST|n CONFIG.SYS ECHO YES, in a batch file, would cause
the word YES to appear on your screen. The command |nIF|n |nNOT|n |nEXIST|n CONFIG
.SYS ECHO NO would cause nothing at all to happen, since CONFIG.SYS does
exist.
You can also use this method to determine whether a file exists in some
other directory, by saying |nIF|n |nEXIST|n C:\DOS\MODE.COM ECHO YES.
<page down> for more IF
It is also possible to determine whether a directory exists, even though
you can't say something like |nIF|n EXIST C:\DOS ECHO YES because that would
cause DOS to look for a file named DOS in the root directory, rather than
looking for a directory named DOS. Here's how it can be done:
|nIF|n EXIST C:\DOS\NUL ECHO YES
NUL is a sort of an imaginary device that DOS uses, and it does sort of
exist in every directory on your disk. Of course you will not find it in
a directory listing, but it is sort of there as far as DOS is concerned,
so that |nIF|n test will report that the C:\DOS directory does exist, whether
or not there are any files in it.
Next, there is "IF X==X". This is used to check on whether two things're
the same or not. Notice that you always need to use two equals (=) signs
together and that you shouldn't leave any spaces on each side of them.
This comparison is case sensitive, so X and x would not be a match!
The only time this is the least bit useful is when one of the items is a
replaceable parameter or an environment variable. (See the batch file
section for more information about using environment variables.) Now you
<page down> for more IF
also need to use some sort of a dummy character, in case the variable or
parameter was not supplied. For example, if you had forgotten to set the
ONE variable before executing the following batch file command:
|nIF|n %ONE%==YES GOTO NEXT
then DOS would expand that line to say |nIF|n ==YES GOTO NEXT because if ONE
was not set, then it is equal to nothing. This line will give you a
major syntax error. But if you had instead said:
|nIF|n !%ONE%==!YES GOTO NEXT
and you still forgot to SET ONE=YES before running the batch file, then
DOS would expand that line to say |nIF|n !==!YES GOTO NEXT. Well that may
not have been the result you had intended, but at least it is not an
error message. You can use just about any character you want for the
dummy character, not just the exclamation point that I used in that exam-
ple. As long as you put the same character on each side of the == signs,
it will work.
Another use for the dummy character is with replaceable parameters. To
make sure that the user of the batch file remembers to type a parameter
on the command line, you could do this:
<page down> for more IF
|nIF|n !%1==! GOTO FORGOT
Because if there was no parameter on the command line, then DOS will ex-
pand that line to say |nIF|n !==! GOTO FORGOT, and ! does indeed equal !.
Then under the :FORGOT label, you might want to use some ECHO statements
to tell the user how the batch file should have been run. (See the GOTO
section for an example.)
Then there is "IF |TERRORLEVEL|T". A lot of programs return an |nERRORLEVEL|n to
DOS after they complete whatever function they were supposed to perform,
and that |nERRORLEVEL|n can be used in a batch file. For example, the DISK-
|nCOPY|n command returns an |nERRORLEVEL|n of 1, 2, 3, or 4, if the command was
not successful, or an |nERRORLEVEL|n of 0 if it was. Well the |nIF|n |nERRORLEVEL|n
test is positive if the |nERRORLEVEL|n is the same or higher as the one spec-
ified, so a batch file like:
DISKCOPY A: A:
|nIF|n NOT |nERRORLEVEL|n 1 GOTO OK
ECHO Something went wrong!
GOTO END
:OK
<page down> for more IF
ECHO It worked!
:END
will always report "Something went wrong!" if |sDISKCOPY|s's ERRORLEVEL is 1
or higher, or "It worked!" if the ERRORLEVEL is less than 1.
Other DOS commands that return ERRORLEVEL codes, are BACKUP, DISKCOMP,
DISKCOPY, FORMAT, GRAFTABL, KEYB, REPLACE, RESTORE, SETVER, TREE, and
XCOPY. Any ERRORLEVEL higher than zero means that some error occurred.
There are quite a few little public domain utilities that can be used in
a batch file to perform some action based on keyboard input. For exam-
ple, a little program from the 2/90 issue of PC/Computing magazine called
ASK.COM (watch out because I've seen other versions of commands named ASK
.COM that didn't work quite the same way), is used like this to retrieve
a Yes or No from the user during execution of a batch file:
@|sECHO|s OFF
ECHO Do you want to load your screen saver into |smemory|s? (Y/n)
ASK
|nIF|n ERRORLEVEL 1 GOTO YES
<page down> for more IF
GOTO NO
:YES
(Command here to load screen saver)
GOTO END
:NO
(Command here for if screen saver is not loaded)
:END
The ASK.COM program looks to the keyboard to see which key you press. If
it is Y or y, an ERRORLEVEL of 2 is returned. If it is <Enter>, the
ERRORLEVEL is 1. If you press N or n, the ERRORLEVEL is 0. So in the
sample batch file above, if you press Y, y, or <Enter>, an ERRORLEVEL of
1 or higher will be returned, so the GOTO YES command will be executed.
If you press N or n, the ERRORLEVEL will be 0 which is less than 1, so
the GOTO YES command will be ignored and the next line, GOTO NO will be
executed instead.
What if the question were instead in reference to something to which the
answer will more often be No rather than Yes? You would want the default
chosen by the <Enter> key to be No instead. You could do that like this:
<page down> for more IF
@|sECHO|s OFF
ECHO Do you want to run |sCHKDSK|s? (y/N)
ASK
|nIF|n ERRORLEVEL 2 GOTO YES
GOTO NO
This time, since the |nIF|n ERRORLEVEL command specifies 2, the GOTO YES com-
mand will only be executed if the Y or y keys are pressed. Anything else
like N, n, or <Enter>, will return an ERRORLEVEL of 1 or 0 which is lower
than 2, so the GOTO YES command will be ignored and the GOTO NO command
will be executed instead.
You could also use the ASK.COM program for a purpose that required three
separate options. Like this:
@|sECHO|s OFF
ECHO If you want choice A, press Y or y.
ECHO If you want choice B, press [Enter].
ECHO If you want choice C, press N or n.
ASK
|nIF|n ERRORLEVEL 2 GOTO YES
<page down> for more IF
|nIF|n ERRORLEVEL 1 GOTO ENTER
GOTO NO
Now don't forget to put those ERRORLEVEL statements in the correct order!
Since an |nIF|n ERRORLEVEL statement is true if the ERRORLEVEL is the same or
higher than the one specified, then if you did it like this:
|nIF|n ERRORLEVEL 1 GOTO ENTER
|nIF|n ERRORLEVEL 2 GOTO YES
then any time the ERRORLEVEL is 1 or 2, the GOTO ENTER command will be
executed, and so DOS will never see the |nIF|n ERRORLEVEL 2 command.
Now remember that ASK.COM is not a part of DOS, so you can't use any of
those batch files unless you get that utility. But since DOS did such a
lousy job of making use of its own ERRORLEVEL parameter, I had to use
that third-party utility as an example of what ERRORLEVEL could do.
You can even nest one |nIF|n command inside another, as in:
|nIF|n ERRORLEVEL 2 |nIF|n NOT ERRORLEVEL 3 GOTO TWO
Now if the ERRORLEVEL were 1, then the first |nIF|n test would fail, so the
second one would not even be noticed by DOS. But if the ERRORLEVEL were
<page down> for more IF
2, then the first |nIF|n test would pass, and then the second test would also
pass since it includes the NOT qualifier, so the GOTO command would be
executed. And if the ERRORLEVEL were 3 then the first |nIF|n test would pass
but the second one would fail, so the GOTO command would be ignored.
<page down> for The |nPAUSE|n Command
The |TPAUSE|T Command
This command is only useful in a batch file. You can put this command
into any batch file, and when it executes, when DOS gets to the |nPAUSE|n
line, it will write "Press any key to continue . . ." on your screen, and
it will just sit and wait for you to press a key. If you press <Ctrl-C>
or <Ctrl-Break>, then DOS will allow you to BREAK out of the batch file
and go back to the DOS prompt without finishing the rest of the commands
in the batch file. Or, when DOS asks you "Terminate batch job (Y/N)?"
you can say N instead, and DOS will continue on with the next command in
the batch file after the one that DOS was working on, at the time you
pressed <Ctrl-Break> or <Ctrl-C>, which in this case was the |nPAUSE|n com-
mand. Well if, instead of one of those two keystroke combinations, you
press any other key, then the batch file will continue on with what it
was doing.
This is especially useful for three purposes. First, whenever you need
to allow the user a moment to do something like put a different floppy
disk into drive A: or turn the printer on.
<page down> for more PAUSE
Second, whenever you want to be sure to allow the user the opportunity to
BREAK out of the batch file with the <Ctrl-C> keystroke. The <Ctrl-C> or
<Ctrl-Break> keystroke generally allows anyone to break out of a batch
file at any time, more or less, but if you want to be absolutely sure the
user will be able to get out if desired, at a certain point in the batch
file, without a doubt, then put a |nPAUSE|n command into the batch file at
that point.
Third, whenever you're working on creating a batch file, and you're not
sure you have the exactly correct commands in it, you can remove the ECHO
OFF line from the beginning, if you had it there, and insert |nPAUSE|n com-
mands every two or three lines, so that you can execute the batch file
and watch what happens without trying to read the screen so fast as it
scrolls by. You'll have plenty of time to look and see what the batch
file is doing, so you can figure out which parts aren't working right.
You can type whatever you want after the |nPAUSE|n command, on the same line
with it, without affecting anything. If ECHO is on, then the whole com-
mand, including whatever you might have typed after it, will be displayed
<page down> for more PAUSE
on the screen. But if echo is off, only the message "Press any key to
continue . . ." will show up, so you probably want to precede the |nPAUSE|n
command in the batch file with an ECHO command to tell the user what it
is that DOS is pausing to wait for.
You might want to make the computer's speaker beep along with a |nPAUSE|n
command, to alert the user that the computer is waiting for a keystroke,
in case he walked off to do something else while that batch file exec-
utes. To put a beep into a batch file, you need ASCII character 7, which
is called BEL. To enter an ASCII character 7 you can hold down the <Alt>
key and type a 7 on the numeric keypad. If a ^G appears on your screen,
you know it worked. (Some text editors won't properly accept such input
from the <Alt-number> method.) If you're using EDLIN to create a batch
file, there's another way to do it also. Just type ECHO ^VG where the ^
symbol means to hold down the <Ctrl> key while typing the next letter.
The ^V tells EDLIN that the next character you enter should be interpret-
ed as a control character, so ^VG puts the ASCII 7 character into the
file. (The next time you list that file to the screen, the V will be
missing, but that's ok; it's served its purpose and the computer knows
<page down> for more PAUSE
now that the G is really supposed to be a <Ctrl-G> or BEL character.)
Now whenever you type that file to the screen with the TYPE command, or
execute it by typing the name of the batch file itself, your computer
will ring its bell (beep its speaker) instead of showing the ^G on the
screen. So just put the |nPAUSE|n command right after that line, or of
course, if your batch file does not have ECHO off, you can just place
these ASCII 7 or ^G characters on the |nPAUSE|n command line instead of using
a separate ECHO command.
<page down> for The |nREM|n Command
The |TREM|T Command
This command has no use except in the CONFIG.SYS file or in a batch file.
It stands for REMark. All it does is tell DOS to "ignore this line". So
that you can insert little comments into your CONFIG.SYS and batch files,
to remind you of what a certain command is supposed to accomplish, so you
don't wonder six months from now why you wrote what you did.
Also, if you want to temporarily disable a line from a batch file or from
the CONFIG.SYS file, without removing it permanently, you just insert the
word |nREM|n and a <Space> at the beginning of the line, and DOS will ignore
that line when it executes the file. Of course in a batch file, an even
easier way to temporarily disable a line is to just put a colon (:) in
front of it. That way, DOS will think it's just a label for the GOTO
command, so DOS will totally ignore that line as long as there isn't a
GOTO command in the file that uses a label by the same name as the first
word in the line you're disabling.
If you want the REMark to display on the screen as a batch file executes,
<page down> for more REM
you have to have echo on. In fact, if echo is on, |nREM|n is the best way to
display a line on the screen, because the ECHO command will display the
line twice if echo is on.
|nREM|n was not a valid CONFIG.SYS command until DOS version 4, so if you use
it in a CONFIG.SYS file in an earlier version, you'll get an error mess-
age "Unrecognized command", but it won't hurt anything. It will only
look like something is wrong with your CONFIG.SYS file as it executes.
As long as there's only the one error message, and you remember that it's
because of the "REMmed out" statement then you'll be fine. Which reminds
me, I'd better warn you that a lot of people will go around telling you
to "REM out" a line in a CONFIG.SYS or batch file, and expect you to un-
derstand what they're talking about. Well what they're talking about is
just putting the word |nREM|n and a <Space> in front of the line in question,
so that DOS will ignore that line the next time the file gets executed.
It's not totally true though, that DOS will ignore everything on the rest
of a line that starts with |nREM|n. Redirection symbols (>, <, and ||) will
be interpreted on a |nREM|n command the same way as on any other command.
<page down> for The |nSHIFT|n Command
The |TSHIFT|T Command
This is a batch file command which has no use whatsoever at the DOS
prompt. Its only purpose is in conjunction with replaceable parameters.
All it does is shift everything over to the left. Huh?
Well, suppose you are using a whole bunch of replaceable parameters in a
batch file. All that's strictly allowed is %0 to represent the name of
the batch file, and then %1 through %9 for whatever you want to use them
for. What if you need more than nine? That's what |nSHIFT|n is for. Here's
the command line we'll use for an example:
D 1.TXT 2.TXT 3.TXT 4.TXT 5.TXT 6.TXT 7.TXT 8.TXT 9.TXT 0.TXT
Well, the name of the batch file, D, is %0. The first parameter, 1.TXT,
is %1, the second one, 2.TXT, is %2, etc., up until the tenth one, 0.TXT,
which doesn't have any %# because there's no such thing as %10. So, how
are we going to use it? Well, as soon as we are done, in the batch file,
with whatever we needed to do to %1 through %9, we could use the |nSHIFT|n
command which will move everything over one space to the left. The name
of the batch file will no longer have any %#, 1.TXT will become %0, and
<page down> for more SHIFT
2.TXT is now %1, etc., up until 0.TXT becomes %9. Now 0.TXT can have a
%# even though it's the eleventh word on the command line. If there were
another file, say 11.TXT, on the command line, then another |nSHIFT|n command
would make 1.TXT not have any %#, and 2.TXT would be %0, 3.TXT gets %1,
etc., until 0.TXT is %8 and 11.TXT is %9. So it is conceivable to have
an unlimited number of parameters on the command line (as long as the to-
tal length of the command line is 127 characters or less). You just need
to have as many |nSHIFT|n commands in the batch file as there are parameters
past the ninth one.
The |nSHIFT|n command also comes in handy when you want to do the exact same
thing to a whole bunch of parameters, and you could be using a different
number of parameters each time. Let's use that same sample command line
above, and say that D.BAT looks like this:
:START
DEL %1
|nSHIFT|n
IF !%1==! GOTO END
GOTO START
<page down> for more SHIFT
:END
Well, the first line is just a label for the GOTO command. Since it
starts with a colon (:), it will be completely ignored until such time as
DOS sees the GOTO command, so having it as the first line is not a prob-
lem. The next line just deletes the file that currently has the value of
%1. Right now, since there has not been any |nSHIFT|n command yet, it is the
first word after the name of the batch file on the command line. The
next line shifts everything on the command line, one space to the left,
so that what was %1 is now %0, what was %2 is now %1, etc. The next line
checks to see if we're done yet. If, after the shift, there is nothing
left to put into %1, then DOS will expand this line to say IF !==! GOTO
END (because %1 is now "nothing") and since ! does equal !, the batch
file will GOTO END. But if there is still something left on the command
line after the shift, so that %1 does not equal nothing, then the line
will be expanded to say, for example, IF !2.TXT==! GOTO END, and since
!2.TXT does not equal !, the GOTO END command will be ignored, and the
GOTO START command will be executed instead. This just keeps on going
until however many files were included on the command line, have been
deleted, at which time %1 is equal to nothing, and the batch file ends.
<page down> for Input/Output |nRedirection|n
Input/Output |tRedirection|t
Hey, this is a really useful subject! Well, to redirect something means
to take something that was supposed to come from or go to a certain place
and take it from or send it to a different place instead. STanDard INput
(called STDIN for short) usually comes from the keyboard, so input redir-
ection means to make the input come from somewhere else instead. STan-
Dard OUTput (also called STDOUT) usually goes to the monitor, so output
|nredirection|n means to make the output go to somewhere else instead. Pret-
ty simple concept, huh?
For input |nredirection|n, the symbol is <. For example, suppose you want to
delete a whole directory full of files, from within a batch file. If the
batch file gives the command DEL C:\TEMP\*.* then the DEL command's going
to ask "Are you sure? (Y/N)" and sit there waiting for Y or N input from
the keyboard. Well if you know that the answer to that question is def-
initely going to always be Y, then you can create a little file on your
disk that contains nothing but a Y and carriage return (the <Enter> key),
and name that file Y.TXT, and make the STDIN for that question come from
<page down> for more Redirection
that file instead of from the keyboard, and then the batch file can con-
tinue on its merry way without your having to press a Y or N key, like
this:
DEL C:\TEMP\*.* < Y.TXT
Of course the DEL part has to come first, because that's the name of the
command, so in order to get Y.TXT pointing toward the command, use the <
symbol. Now when the DEL command tells DOS that it wants some STDIN from
the keyboard, DOS is going to give the input from the Y.TXT file instead,
because the DEL command won't know the difference as long as it gets some
input. Just make sure that the Y.TXT file is either in the current dir-
ectory, or else specify the path to the directory on the command line, as
in DEL C:\TEMP\*.* < C:\BELFRY\Y.TXT, or else DOS won't be able to find
the Y.TXT file and that will lock up the computer totally.
Another time that input |nredirection|n is really useful, is with the MORE
command. This command takes STDIN and sends it to STDOUT 23 lines at a
time. That doesn't do much good at all unless STDIN is redirected from
somewhere else besides the keyboard. You can read a long text file, one
<page down> for more Redirection
page at a time, with this command:
MORE < FILENAME.EXT
Notice that the < symbol causes the file to be pointing toward the MORE
command. If you were to accidentally type the > symbol instead the MORE
command would send STDOUT to the file, instead of taking STDIN from the
file, and that would effectively erase every byte of data from that file.
Make sure the symbol points from the file toward the command!
Now make sure you understand that once you redirect STDIN from somewhere
toward a command, that command is not going to accept any input from the
keyboard, because it's taking all of its input from the place you redir-
ected it from. If a command takes some input and then does something and
then asks for some more input, that second piece of input has got to be
in the same place that the first piece was in, or else the command will
sit there all day waiting for the second piece of input to come from that
same place. The command won't pay the slightest bit of attention to the
keyboard, because you told it to take its input from somewhere else. So
make sure that if you write a little file like Y.TXT for a purpose that
requires more than one piece of input, that you put all the input that
<page down> for more Redirection
will be needed for the entire command, into that file.
For example, the FORMAT command asks you first to place the disk into the
drive and press <Enter>, and then it starts formatting. So suppose you
were to make a file that contained just a carriage return, and you used
the command FORMAT A: < CR.TXT to redirect that file into the FORMAT com-
mand. Well it will start out working just fine, but when the formatting
is done FORMAT is going to ask you "Format another? (Y/N)" and it's going
to sit there forever waiting for the Y or N to come from that CR.TXT file
and the only thing you can do about it is reboot the computer. Run the
command a few times first, to make sure you know exactly what input the
command is going to want, before you create a file to redirect into it!
(The DOS version 5 FORMAT command is not a good candidate for input re-
direction, because under different circumstances it will ask for differ-
ent input. For example, if a disk doesn't have enough free space on it
for the MIRROR.FIL file, then FORMAT will ask if you want to format it
anyway, even though it won't be |sUNFORMAT|stable, so your little text file
won't be able to deal with that question if it comes up!) It won't mat-
ter if the text file being redirected into a command has too much data in
<page down> for more Redirection
it, as long as all the data the command is going to need is there and in
the right order. You can put fifty extra characters into the file if you
want, and nobody will care, as long as they come after the parts that the
command is going to want.
The MORE command requests the data that it is to display, from the STDIN
device, so if the STDIN has been redirected from somewhere other than the
the keyboard, then that's where MORE will take its input from. But for
the keystroke that tells MORE to display the next screenful, MORE doesn't
request that particular piece of input from STDIN, it looks right at the
keyboard instead, for that piece of input. So that's why you can still
"press any key to continue" even though STDIN has been redirected. But
for any command that takes all of its input from STDIN, then in that case
the keyboard will be totally ignored when input is redirected, until that
particular command completes and exits back to DOS.
Another use for input |nredirection|n is when you've temporarily disabled CON
(that's the name for the CONsole device which means monitor-and-|skeyboard|s)
with the CTTY command, and you need to take just one little piece of in-
<page down> for more Redirection
put from the keyboard. For example, PAUSE < CON so that you will be able
to use the keyboard to "Press a key when ready . . ." because PAUSE, un-
like MORE, looks to STDIN for that keystroke, rather than the keyboard.
Output |nredirection|n is even more useful! You can take all the stuff that
a command usually sends to STDOUT, and send it to a file, or to a print-
er, or to your modem, or even to a place called NUL which means nowhere.
Now this will not effect STDERR, which means the place where a command
sends ERRor messages to. Those will still go to the monitor, even when
STDOUT is redirected elsewhere. But little messages like "1 File(s) cop-
ied" can be sent to NUL so you don't have to look at them on the screen.
The output of the MEM command can be sent to a printer so you can take it
over to your friend's house, and show him how much free memory you have
now that you installed DOS version 5. The entire output of the CHKDSK
command can be sent to a disk file so that you can look at it later. But
you have to watch it with that one, though, because that does mean the
entire output. If CHKDSK runs into any errors on your disk, it will send
the message about "Convert lost chains to files? (Y/N)" to the disk file
where you won't be able to see it. You won't even realize that CHKDSK is
<page down> for more Redirection
sitting there waiting for you to answer the question. You can say Y or N
or you can say <Ctrl-C> to BREAK out of the CHKDSK command, but it will
take you a minute to realize that you need to do such a thing. Of course
you'd better use the <Ctrl-C> method though, because you don't know what
question CHKDSK is waiting for an answer to. Well here are the commands
you would use to perform those tricks:
COPY FILENAME.EXT D: > NUL
MEM /C > PRN
CHKDSK C:*.* /V > FILENAME.EXT
See, the little arrow points in the direction in which you want the flow
to occur. We're sending output away from the command and toward the file
or toward the device|1s, so we're using the > symbol.
Make sure you understand that if the recipient of the > symbol is a file-
name, and there already exists a file by that name, whatever that file
contains is going to be irrevocably lost the second you hit the <Enter>
key to execute that command. Because whenever DOS sees an output redir-
ection symbol, the very first thing that happens is that DOS opens the
<page down> for more Redirection
file that's referred to in the command, and dumps the contents of that
file into the "bit-bucket" (trash can). Whenever the > symbol points
toward a filename, the file is created if it doesn't exist already, or if
it does exist, it is erased. Wiped out. Immediately. Before DOS even
looks at the rest of what you typed on that command line. Also, if there
doesn't end up being any STDOUT directed toward that file, the file will
still exist because DOS created it as soon as you hit the <Enter> key.
For example, if you typed DEL FILENAME.EXT > TEST, then the file named
TEST will be created even though there is no output from a command like
DEL FILENAME.EXT. TEST will just be a zero-length file, but it will have
a directory entry even though it won't take up any disk space.
If a file already exists and you want to add some more data to it, you
can use a double >> sign, like this:
MEM /C >> MEMCHKS.TXT
Whenever DOS sees a double >> sign, that means to create the file if it
doesn't already exist, or add more data to the end of the file if it does
already exist, which is called appending the data to the file.
<page down> for more Redirection
Now all these |nredirection|n examples so far have involved one command and
one device or filename. What if you want to redirect one command to an-
other? That's what the third type of |nredirection|n is for. It's called
|tpiping|t and it uses the pipe symbol which looks like ||. That's the char-
acter on top of the \ key, so if you hold down a <Shift> key and type \
you'll get || and that's a pipe. |nPiping|n takes the output from the first
command on the command line, and sends it as input to the second command
on the command line. For example,
CHKDSK C:\*.* /V || MORE
That takes the output of the CHKDSK command that would normally be sent
to your screen at about a million miles per minute, and sends it instead
as input to the MORE command. The MORE command takes its input and div-
ides it into 23-line sections, and sends each section to the screen with
a "-- More --" symbol at the bottom, which means you should press a key
when you're ready to see the next screenful of output.
Remember, you can't use pipes with filenames or device|1s, only commands.
|nPiping|n sends STDOUT from one command as STDIN to another command. And
you can't put a command to the right of a > or < symbol. The only thing
<page down> for more Redirection
that goes to the right of those symbols is a device or filename. If you
typed CHKDSK C:\*.* /V > MORE by accident, that would create a file named
MORE (with no extension) in the current directory and put |sCHKDSK|s's output
there instead of sending it to the MORE command.
Here's another useful example. Remember how you can use a command like
DEL C:\TEMP\*.* < Y.TXT to make the DEL command work without waiting for
you to say whether you're "sure" or not? Well that requires that you
have a little file named Y.TXT which contains a Y and a carriage return,
sitting on your disk taking up an entire cluster of disk space. This
command will serve the same purpose, without that Y.TXT file:
ECHO Y || DEL C:\TEMP\*.*
The output of this ECHO command is a Y and carriage return, so with the
pipe, that gets sent to the DEL command whenever DEL gets ready to ask
for its input. Now you can get rid of your Y.TXT file that's taking up
all that disk space!
The only problem with piping is that DOS needs to borrow some disk space
to create some temporary files, whenever you use piping. If your current
<page down> for more Redirection
disk doesn't have enough room, or if you're on a network and you don't
have write access to the current drive, or if you have a write-protect on
the current disk, then DOS won't be able to perform the piping operation.
Also, if you should use the <Ctrl-C> keystroke to break out of the com-
mand, or if you have a power outage in the middle of the command, then
those little temp files will get left on your disk, for you to wonder
later where they came from.
If you have DOS version 5, you can set an environment variable named |TTEMP|T
which will tell DOS where you want those temp files created, instead of
in older versions of DOS, the temp files were created in the current dir-
ectory of the current drive. No more! If you have a RAMdisk with E: for
a drive letter, put this command into your AUTOEXEC.BAT file:
SET TEMP=E:\
Now whenever DOS does any piping, it will put those temp files onto the
RAMdisk. That's great for two reasons. First, a RAMdisk is so much fas-
ter than any other type of disk, so piping operations will be faster than
they would be if |nTEMP|n was not set or if it was set to a slow hard disk.
Second, if you have a power failure in the middle of the command, you
<page down> for more Redirection
won't have to go and erase those little temp files because when the power
went out, everything on the RAMdisk, including the little temp files, was
erased automatically. (See also DOSSHELL for another use for TEMP.)
Even if you don't have a RAMdisk, if you have DOS version 5 you should
use a SET |sTEMP|s=C:\SOMEWHERE command in your AUTOEXEC.BAT file, so that
your temp files all end up in the same place, instead of being scattered
all over the place depending on what drive and directory you happened to
be in at the time you performed a piping operation. Of course, if the
operation is completed properly, DOS will delete the temp files all by
itself.
You can also combine more than one |nredirection|n character in the same com-
mand. For example:
FIND "Hello" FILE1 || SORT /+3 || MORE
CHKDSK C:\*.* /V || FIND "WHAT" > PRN
FORMAT A: < FMT.TXT > DISKSIZE.TXT
But except for the pipe symbol, you can't use more than one of the same
symbol in the same command. For example,
<page down> for more Redirection
DIR > FILE1 > FILE2
will not send the output of the DIR command to both of those files!
There is a type of command called a "|tfilter|t", which takes STDIN from the
keyboard and performs some function on it, and then sends STDOUT to the
screen. Filters don't really do anything useful unless they're used with
some |nredirection|n. FIND, MORE, and SORT are the filters that come with
DOS, although the FIND |nfilter|n is easier to use as a regular command in-
stead of as a normal |nfilter|n. That means, use it without the < symbol.
You can also use |nredirection|n to create an empty file or a non-empty file.
Remember that as soon as DOS sees the > symbol, if there's a filename on
the other side of that symbol then the specified file gets created, be-
fore DOS even looks at the rest of the command. So if you do not have a
file named FILE1 and you execute this command:
TYPE FILE1 > FILE1
then DOS creates a file named FILE1 and so far it's empty, but it does
exist. So then DOS types out the contents of the FILE1 file (nothing),
and sends that output (nothing) to the FILE1 file. So FILE1 still con-
<page down> for more Redirection
tains nothing. You just created a zero-length file which takes up a
directory entry, but no disk space gets used because the file is empty.
You can use the ECHO command to create a non-empty file like this:
ECHO HELLO > FILE2
ECHO HOW ARE YOU >> FILE2
DOS creates a file named FILE2 and the ECHO command puts the word HELLO
and a carriage return into that file. The next command opens the FILE2
file again, and sends the words HOW ARE YOU and another carriage return,
to the end of the file.
If you're absolutely positive that the last line in your CONFIG.SYS file
ended with a carriage return, then you can add a new line to the end of
it without even editing it. Like this:
ECHO |sDEVIC|1E|s=C:\DOS\ANSI.SYS >> C:\CONFIG.SYS
But if the last line in the CONFIG.SYS file were |sSTACKS|s=0,0 and that com-
mand didn't have any carriage return at the end of it, then the result of
that ECHO command would be this:
|sSTACKS|s=0,0|sDEVIC|1E|s=C:\DOS\ANSI.SYS
<page down> for more Redirection
all on one line like that, and DOS wouldn't appreciate that command very
well the next time you reboot the computer and DOS tries to execute the
commands in your CONFIG.SYS file. That's a good reason to make sure that
each command in your CONFIG.SYS file, or any batch file, ends with a car-
riage return (the <Enter> key). What that means is don't ever type <F6>
or <Ctrl-Z> before you hit the <Enter> key at the end of a line, when you
are using COPY CON FILENAME or EDLIN or anything else, to create or mod-
ify a file. Always put the F6 or <Ctrl-Z> on a blank line by itself, at
the end of your file.
You can use input |nredirection|n to make use of script files for commands
such as EDLIN, GWBASIC, and DEBUG. If you upgraded from DOS version 3.2
to 3.3, and wanted to change the first line of each batch file from ECHO
OFF to @|sECHO|s OFF, you could create a little text file named |nEDLIN|n.TXT
like this:
1
@|sECHO|s OFF
E
and then use this command:
<page down> for more Redirection
FOR %a IN (*.BAT) DO EDLIN %a < |nEDLIN|n.TXT
And automatically, right before your very eyes, all your batch files will
be updated to include the @ symbol which didn't appear in DOS until ver-
sion 3.3. Here's how it works. The FOR command causes DOS to repeat the
command that's written after DO, for each file that matches the specifi-
cation in parentheses. So for each .BAT file in the current directory,
DOS will perform the command EDLIN WHATEVER.BAT < |nEDLIN|n.TXT. That will
load the EDLIN line editor and the WHATEVER.BAT file, and then instead of
taking STDIN from the keyboard, EDLIN will take its input from the file
you created named |nEDLIN|n.TXT, which contains all the commands EDLIN needs
to change line one of the file to @|sECHO|s OFF, and then save the changed
file to disk, and exit back to DOS. Then DOS will continue on with the
FOR command, with the next .BAT file in the current directory, until they
have all been edited. From now on, whenever you run your batch files,
you won't see that ugly old "|sECHO|s OFF" command flash onto the screen, be-
cause what the @ symbol does is it tells DOS not to display the command.
That's just about the best thing about EDLIN, even though it's a really
primitive text editor, is that it will take redirected input from a text
file script like that. There aren't more than two or three other text
<page down> for more Redirection
editors in the entire world that can do that.
Now if you want the script file to cause EDLIN to insert any lines, you
have to remember that EDLIN requires a <Ctrl-C> keystroke to go from in-
sert mode back to the command mode. So the <Ctrl-C> must be part of the
script file. It's not easy to get a <Ctrl-C> into a file, but it can be
done. With EDLIN itself. You use the <Ctrl-V> keystroke sequence in ED-
LIN to tell EDLIN that the next keystroke you enter should be interpret-
ed as a control character. So <Ctrl-V> followed by a capital C will put
the <Ctrl-C> into the file. It will look like this:
1I
ECHO OFF
^VC
E
That script, if redirected as input into an EDLIN command, would insert
the command ECHO OFF before the first line of the file you're |sEDLIN|sing.
(Remember that ^C or ^V means the same thing as <Ctrl-C> or <Ctrl-V>.)
The next time you look at or edit that script file, the V will be miss-
ing, but that's ok. It was only there in order to tell EDLIN to put the
<page down> for more Redirection
^C into the file, and it served its purpose. You can't just type <Ctrl-
C> to get it in the file, because if you type <Ctrl-C> in EDLIN, it swit-
ches from insert mode to command mode. EDLIN can't tell you were trying
to put the ^C into the text rather than trying to switch to command mode.
So that's why you needed the <Ctrl-V> trick.
Don't forget that if you have DOS version 5 and have /P as part of your
DIRCMD environment variable, then if you should use the command:
DIR > FILE1
to put your directory listing into a text file, DOS will be performing
the command DIR /P because of your DIRCMD setting, and it will be sit-
ting there waiting for you to "Press any key . . ." only you won't real-
ize it because the "Press any key . . ." message has been redirected into
the FILE1 file along with the rest of STDOUT, and you'll think you've
locked up the computer when all you need to do is press any key. Yep,
the first time I did that, I stared at the screen for a good five minutes
wondering why on earth my computer had locked up, before I realized what
the problem was.
<page down> for more Redirection
You can't redirect the input or output of a batch file, although you can
use |nredirection|n on each line in a batch file. For example, to make all
output of a batch file go to the NUL device instead of to your screen,
you might be tempted to try this:
|nBATCH|n > NUL
Nope, it won't work! |nBATCH|n.BAT will be executed as usual, and all the
STDOUT will go to the screen. If you want to suppress the output of the
batch file, you need to put a > NUL at the end of each line in the batch
file. |nRedirection|n on the command line that executes the batch file won't
work, even though |nredirection|n will work just fine inside the batch file.
All DOS commands send their output to either STDOUT or STDERR. STDERR is
always the monitor, and STDOUT is the monitor unless |nredirection|n has been
specified. But lots of programs that don't come with DOS send their out-
put straight to video memory, and for those programs, trying to redirect
output from the screen to somewhere else just won't work! And lots of
programs take their input straight from the keyboard rather than from
STDIN, so for those programs, input |nredirection|n won't work. Sorry!
<page down> for more Redirection
And remember that when you're using piping, the command to the right of
the pipe symbol is still a plain old regular DOS command, so just like
always, its executable file must be either in the current directory, or
on the PATH, in order for DOS to find and execute it. If you ever get
"|sBad command or filename|s" from a command like TYPE CONFIG.SYS || MORE,
that's because your |nMORE|n.COM file is not anywhere that DOS can find it.
<page down> for The |nFIND|n Command
The |TFIND|T Command
This is a highly useful command. It will search any file for any string
of text. Of course it's only likely to work on pure ASCII files, because
a file created by a word processor has control codes embedded in it, to
tell it where the margins go and where the words should be in italics,
and things like that. So the word "little" with control codes embedded
in it might look like "littlΣ" so if you use the |nFIND|n command to look for
"little", it won't be found. But for ASCII files, the |nFIND|n command works
wonderfully.
Well, here's how you use it. Suppose you want to search for the word
"little" in the files named FILE1, FILE2, and FILE3:
|nFIND|n "little" FILE1 FILE2 FILE3
will do it. But it won't find the word "little" if it's capitalized at
the beginning of a sentence. For that you need this command:
|nFIND|n "Little" FILE1 FILE2 FILE3
But in DOS version 5.0, there's a new switch to the |nFIND|n command. The /I
switch tells DOS to ignore the case of the letters so that |nFIND|n "little"
<page down> for more FIND
will find "little" and "Little" and "LITTLE" and "liTtlE".
|nFIND|n /I "little" FILE1 FILE2 FILE3
would do that if you have DOS version 5.0 or later.
If you're using the |nFIND|n command as a filter (explained later in this
section) to find a filename in your directory listings, be sure to type
the filename in capital letters (or else use the /I switch, if you have
DOS version 5.0) because filenames are always stored in the directory
listings in all-caps, so |nFIND|n won't find the file HITHERE if you tell it
to search for "ither", but |nFIND|n "ITHER" would work.
The output of the find command would be like this:
---------- FILE1
Mary had a little lamb,
---------- FILE2
---------- FILE3
Little Red Riding Hood took a basket of fruit to her sick .....
When Little Red Riding Hood got to her grandmother's house,....
It shows the name of each file as it begins searching it, then it shows
<page down> for more FIND
each line that contains the word being searched for. If the word is not
found in a particular file, it doesn't say anything, it just goes on to
search the next file.
If you're searching more than one file at a time, you have to type out
each filename separately. Wildcards can't be used with the |nFIND|n command.
Of course you could get around that by using the FOR command, as in:
FOR %a IN (FILE*) DO |nFIND|n /I "little" %a
Remember that the |nFIND|n command will search for exactly what you tell it
to search for, no more, and no less. If you tell it to search for "and"
it will find "and" "hand" "land" and "grandmother". If you tell it to
search for " and " that would make it leave out "hand" and "land" and all
that, but it will also leave out "and," because there is a comma after
"and" instead of a <Space>. If you're searching for the word "renamed"
and the word does exist in the file but it's hyphenated at the end of a
line, so that it exists in the file in the form "re-" on one line and
"named" on the next line, then |nFIND|n won't find it at all. The same thing
if you're searching for a string that is more than one word long, such as
<page down> for more FIND
"my birthday". If "my" is at the end of one line and "birthday" is at
the beginning of the next line, then |nFIND|n won't find "my birthday" be-
cause there's a carriage return instead of a <Space> between those words.
If the string you want to search for contains any quotation marks, you
have to type two quotation marks for each one in the string. For exam-
ple, to find the string <He said, "Why don't we go to the mall?"> you
have to use this command:
|nFIND|n "He said, ""Why don't we go to the mall?"""
Each quotation mark in the string you're looking for is replaced by two
quotation marks, and then the entire string is also enclosed in one more
set of quotation marks as usual.
If the file you're searching has a <Ctrl-Z> (End-of-File marker) in it,
the |nFIND|n command won't search any of the text past that point.
The |nFIND|n command has other switches that change its output a little bit,
and these switches are for all DOS versions, not just 5.0.
<page down> for more FIND
The /V switch tells DOS to find all the lines in the file that do not
contain the string you're searching for. Just the opposite of normal.
Suppose you want to use |nFIND|n as a filter (described below) to find all
the files in the current directory that were not updated today.
DIR || |nFIND|n /V "6-27-91"
would do it.
The /C switch tells DOS to just count the number of lines that contain
the search string. It won't display the lines, it will only count them.
For example, if you want to know how many lines are in the entire file,
|nFIND|n /C /V "%$#@!" FILENAME.EXT
would do it, because the /V tells |nFIND|n to look for lines that do not con-
tain the string %$#@! and that's going to be all of them, right? Then
the /C switch will count how many lines matched the search, and that's
the total number of lines in the file.
The /N switch tells |nFIND|n to display line numbers with each line. So the
output in this case will be the line that contains the search string, and
the line number for that line, for each line that contains the string.
<page down> for more FIND
It might look like this:
[2]Little Red Riding Hood took a basket of fruit to her .......
[16]When Little Red Riding Hood got to her grandmother's ......
Well that's all there is to the |nFIND|n command in its normal command form,
but that's definitely not the only way to use it. It can also be used as
a filter. (Go read the section on redirection and then come back here.)
A filter is a little command that takes STanDard INput from the keyboard,
performs some function on that input, and then sends STanDard OUTput to
the monitor. The three |sfilter|ss that come with DOS are |nFIND|n, MORE, and
SORT.
When you don't supply a filename on the |nFIND|n command line, then you're
using it as a filter. Well it doesn't do much good to take input from
the keyboard and send output to the screen. That's why I told you to go
read about redirection first. If you redirect the input from somewhere
else, and/or redirect output to somewhere else, then a filter is very
useful.
<page down> for more FIND
|nFIND|n "string" < FILE1 > FILE2
will create a file named FILE2 that contains only those lines from FILE1
that contain "string".
TYPE FILE1 || |nFIND|n "string" || |nFIND|n /V "smaller string" > PRN
will print a list of all the lines in FILE1 that contain "string" but do
not contain "smaller string".
DIR || |nFIND|n "AB"
will show you all the lines in your directory that contain the string AB
in either the filename or the extension.
DIR || |nFIND|n /V "<DIR>"
will show you all the lines in your directory that are not subdirectory
listings.
DIR || |nFIND|n /V "e"
will show you a directory listing without the lines that say "|sVolume|s in
drive C is..." and "... bytes free".
(Of course, if you have DOS version 5.0, you can get those last two dis-
plays by using the new switches to the DIR command instead of using
|nFIND|n.)
<page down> for The |nMORE|n Command
The |TMORE|T Command
This command takes its input from the keyboard and sends its output to
the monitor, one screenful (23 lines) at a time. What good does that do?
Not a whole lot. But wait, this is a very useful command after all. Be-
cause you can use it with input redirection, to make it take its input
from a file instead. Have you ever used the TYPE command on a large file
and watched it scroll by on your screen about ten thousand times faster
than you could read it? TYPE FILENAME || |nMORE|n or |nMORE|n < FILENAME would
make the file go to your screen just one screenful at a time, and at the
end of each page it would say "-- More --" and that tells you to hit any
key to make it continue. When you hit a key, it will show you one more
screenful, etc.
Be very careful to make sure you understand the concept of redirection
before you go using the second method above, with the < symbol. Because
if you accidentally use the > symbol instead, every byte of data in that
FILENAME file will be erased. Permanently. It won't be recoverable with
the UNDELETE command or anything else. The first method above, with the
<page down> for more MORE
|| symbol, is much safer although it does have its drawbacks.
The drawbacks of the || method are that it takes more keystrokes, because
you have to use the TYPE command, and that any time you use the || symbol
you create two temporary files on the current disk. If the current drive
is a floppy, that temp file is going to slow you down. If the disk in
the current drive is |swrite-protect|sed, you won't be able to use the || sym-
bol at all. And if you use <Ctrl-C> to BREAK out of the operation after
you hit the <Enter> key, the temp files will be left laying around on the
disk. (Of course if you have version 5.0 or later, then the temp files
used by the operation of the || symbol will be created in whatever drive
and directory is pointed to by the TEMP environment variable, and it ap-
pears that breaking out with <Ctrl-C> no longer leaves the files sitting
there, so none of this is a problem.) But despite these drawbacks of the
|| method, it's still worth it to use until you're absolutely sure you un-
derstand the difference between > and <, for the safety of the file you
want to display.
You can also use this command to send the output of some other command
<page down> for more MORE
through it. For example, CHKDSK *.* /V || |nMORE|n, so that you can see the
CHKDSK report before it all scrolls off the screen. Just about any time
your screen tries to show you something faster that you can read it, the
|nMORE|n command will let you slow it down. (Except for while your AUTOEXEC
.BAT and CONFIG.SYS files are executing. For that, all you can do is use
your <Pause> key, which isn't all that good of an idea during |sboot|sup, or
put PAUSE as the first command in AUTOEXEC.BAT and put another PAUSE com-
mand every two or three lines, save this edited AUTOEXEC.BAT file, reboot
and you'll be able to see all those messages that normally fly by. Now
edit the file again to remove those PAUSE commands, and you're back to
normal.)
If any of this is confusing you, check out the section on redirection to
learn about the <, ||, and > symbols and what they do.
<page down> for The |nSORT|n Command
The |TSORT|T Command
This command sorts ASCII files into ASCII order. That's pretty close to
alphabetical order, except that lowercase letters come after of all the
uppercase ones (...XYZabc...). But they corrected that problem starting
with DOS version 3, so now |nSORT|n really does sort in alphabetical order.
Actually, |nSORT|n is a filter, so it takes standard input and sends standard
output. If you just use |nSORT|n with no Input/Output redirection, your cur-
sor will just drop down a line and wait for you to enter some lines of
text for it to sort. You type the stuff and then hit <F6> or <Ctrl-Z> to
tell DOS you're done, hit <Enter>, and |nSORT|n will sort the text you just
typed, by the first character in each line, and send the output to the
screen. If the first character is the same in two different lines, then
|nSORT|n will sort those two lines by their second character. If those two
characters are the same too, then the third character will be used as the
tie-breaker, etc.
But you can use the |nSORT|n filter with input redirection, output redirec-
<page down> for more SORT
tion, piping, or any combination of those, to make it do just about any-
thing you want.
Here's the syntax for this command:
|nSORT|n /R /+# < D:\DIR\FILE1.EXT > D:\DIR\FILE2.EXT
where /R makes it sort in reverse order, /+# makes it sort by the # char-
acter instead of by the first character of each line, and FILE1.EXT is
the file you want it to sort. Just make sure that you use < and not >,
to make the file point toward the command. Because if you accidentally
use > instead, for that part of the command, then the file you wanted to
sort gets erased instead. Now if you want the output of the |nSORT|n command
to go, instead of to the screen, somewhere else like to the printer or to
a different file, then you use the > symbol with PRN for the printer, or
with a filename as in the example above. Make sure that you don't use
the same filename for the > symbol as you used for the < symbol, because
once again, that would erase the contents of that file before it gets a
chance to get sorted. And if you want to keep the FILE2 file instead of
the FILE1 file, then you can delete the FILE1 file after the command is
done, but first look at it to make sure that the |nSORT|n was successful and
<page down> for more SORT
didn't lose any of the data in the file. Then use the RENAME command to
give the target file the same name as what the source file had before you
deleted it. That is, if you want it to have the same old name.
You can also use |nSORT|n as the target of a piping operation. That means
you can sort the output of another command before displaying it on the
screen. For example, DIR || |nSORT|n /+14 would cause a DIR command to send
its output to the |nSORT|n command instead of to the screen, and |nSORT|n would
sort the directory listing by the data in the fourteenth column which is
the file size, and then that sorted output would go to your screen.
The trouble with that is that the lines of the DIR command's output that
say things like "|sVolume|s in drive C is WHATEVER" and "However many bytes
free" would be included in that sorted output. Yuk. To get rid of that
problem, use the FIND filter to get rid of those lines. Every line that
has to do with files and directories has a - character in it, in the date
column. So DIR || FIND "-" || |nSORT|n /+14 || MORE would take a directory
listing, remove the lines that don't contain -, sort the remaining lines
according to column 14, and display the output 23 lines at a time so you
<page down> for more SORT
can see it before it scrolls off the screen. Notice that it would work
just as well to say DIR || |nSORT|n /+14 || FIND "-" || MORE but it would take
longer, because there would be more lines for |nSORT|n to sort. Using the
FIND command first to filter out the lines you're not going to want any-
way, means there are fewer lines for |nSORT|n to deal with. Since |nSORT|n is
pretty slow, as far as computer programs go, you want to use FIND first
if that will serve the purpose, whenever you're using both |sfilter|ss in the
same command.
The first column in a directory listing is, of course, the filename. The
extension starts in column 10, the size in column 14, the date in column
24, and the time in column 34. Of course, if you have DOS version 5, you
don't need the |nSORT|n command to sort your DIR listings, but it's still a
useful command, for sure!
A few caveats: |nSORT|n can't handle files larger than 63 kilo|sbytes|s in size.
Each line in the file must have a carriage return (the <Enter> key) at
the end of it.
<page down> for more SORT
If you're using output redirection to create a new file containing the
sorted version of the input file, and if there is a ^Z End-of-File char-
acter at the end of the file, it will be moved to the beginning since it
has a lower ASCII code number than any letters, and with a ^Z as the
first character of the new sorted file, you won't be able to TYPE the
file to the screen, or PRINT it, or do hardly anything with it! (That's
one reason why I said to make sure the |nSORT|n was successful before you de-
lete the original file, if the sorted one is the only one you plan to
keep.) Make sure your file does not have a ^Z in it before you use the
|nSORT|n command on it. You can use the COPY command with the /A and /B
switches to remove a ^Z from a file. For example COPY /A FILEB4 FILEAFTR
/B, then RENAME FILEAFTR FILEB4 to give it back its old name. Of course
if FILEB4 has a ^Z in it before the end of the file, all the data that
came after it will be lost. Don't do this unless the first ^Z in the
file comes at the very end of the file. Or else do it on a spare copy of
the file first and check the results before you do it on the real copy.
For beginning of File 3, see Memory
|tAUTOEXEC.BAT|t|fSIMPLY1|f
|TSETVER|T|fSIMPLY1|f
|tUNDELETE|t|fSIMPLY1|f
|tUNFORMAT|t|fSIMPLY1|f
|tXCOPY|t|fSIMPLY1|f
|tBytes|t|fSIMPLY3|f
|tMemory|t|fSIMPLY3|f
|TMEM|T|fSIMPLY3|f
|TPRINT|T|fSIMPLY3|f
|tRAMdisk|t|fSIMPLY3|f
|TREPLACE|T|fSIMPLY3|f
|TRESTORE|T|fSIMPLY3|f
|TTREE|T|fSIMPLY3|f
|tANSI.SYS|t|fSIMPLY4|f
|TBREAK|T|fSIMPLY4|f
|tCONFIG.SYS|t|fSIMPLY4|f
|TDEVIC|1E|T|fSIMPLY4|f
|tEnvironment|t|fSIMPLY4|f
|tfloppy|t|fSIMPLY4|f
|TFORMAT|T|fSIMPLY4|f
|tmacros|t|fSIMPLY4|f
|tPATH|t|fSIMPLY4|f
|tPROMPT|t|fSIMPLY4|f
|TSET|T|fSIMPLY4|f
|TSTACKS|T|fSIMPLY4|f
|tvolume|t|fSIMPLY4|f
|tBad command or filename|t|fSIMPLY5|f
|TC|1:|T|fSIMPLY5|f
|TCD|T|fSIMPLY5|f
|tCOMMAND.COM|t|fSIMPLY5|f
|TCOMMAN|1D|T|fSIMPLY5|f
|TCON|T|fSIMPLY5|f
|TCOPY|T|fSIMPLY5|f
|TCTTY|T|fSIMPLY5|f
|tcurrent|t|fSIMPLY5|f
|TDATE|T|fSIMPLY5|f
|tdefault|t|fSIMPLY5|f
|TDEL|T|fSIMPLY5|f
|tDevice|1s|t|fSIMPLY5|f
|TDIRCMD|T|fSIMPLY5|f
|tDirectory|t|fSIMPLY5|f
|TDIR|T|fSIMPLY5|f
|tExecutable|t|fSIMPLY5|f
|texternal|t|fSIMPLY5|f
|TGRAFTABL|T|fSIMPLY5|f
|tinternal|t|fSIMPLY5|f
|TKEYB|T|fSIMPLY5|f
|TNUL|T|fSIMPLY5|f
|TPRN|T|fSIMPLY5|f
|TRENAME|T|fSIMPLY5|f
|troot|t|fSIMPLY5|f
|TTIME|T|fSIMPLY5|f
|TTYPE|T|fSIMPLY5|f
|TVER|T|fSIMPLY5|f
|t*.*|t|fSIMPLY6|f
|tASCII|t|fSIMPLY6|f
|TBBS|T|fSIMPLY6|f
|tBoot|t|fSIMPLY6|f
|tcluster|t|fSIMPLY6|f
|tDisks|t|fSIMPLY6|f
|tdownloading|t|fSIMPLY6|f
|tKeyboard|t|fSIMPLY6|f
|tParameters|t|fSIMPLY6|f
|tPower|t|fSIMPLY6|f
|tpublic domain|t|fSIMPLY6|f
|tReboot|t|fSIMPLY6|f
|tReplaceable|t|fSIMPLY6|f
|tShareware|t|fSIMPLY6|f
|tWildcards|t|fSIMPLY6|f
|tWrite-protect|t|fSIMPLY6|f
|TBACKUP|T|fSIMPLY7|f
|TCHKDSK|T|fSIMPLY7|f
|TCOMP|T|fSIMPLY7|f
|TDEBUG|T|fSIMPLY7|f
|TDISKCOMP|T|fSIMPLY7|f
|TDISKCOPY|T|fSIMPLY7|f
|TDOSKEY|T|fSIMPLY7|f
|TDOSSHELL|T|fSIMPLY7|f
|TEDIT|T|fSIMPLY7|f
|TEDLIN|T|fSIMPLY7|f
|TGWBASIC|T|fSIMPLY7|f