home *** CD-ROM | disk | FTP | other *** search
- What Is a |tBatch File|t?
-
- Oh, my, this is fun. Batch 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 file|n is just a plain ASCII text file, that contains nothing
- except a batch of DOS commands, each command on a separate line, and each
- line ending with a carriage return (the <Enter> key). The |nbatch file|n 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 file|n the same name as
- any other command on your system.
-
- A |nbatch file|n is an executable file, so when you just type its name at the
- DOS prompt, and hit <Enter>, the |nbatch file|n will run, just like any other
- command. DOS will read the first line of the |nbatch file|n, 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 File continued
- Batch 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 batch files. Those
- are CALL, ECHO, FOR, GOTO, IF, PAUSE, REM, and SHIFT. Replaceable para-
- meters is another thing that is only used in batch 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 file|n, in a manner
- similar to the use of |nreplaceable|n parameters. If you have an |nenvironment|n
- variable named ONE which was set equal to YES via the command |nSET|n ONE=YES
- then you could reference that variable from inside a |nbatch file|n by sur-
- rounding its name with percent signs, as in %ONE%. Here's an example:
- |nIF|n %ONE%!==YES! |nGOTO|n OK
- Now when DOS is executing the |nbatch file|n that contains that line, it will
- see the %ONE% and go look in the |nenvironment|n 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:
- <page down> for more
- Batch File continued
- |nIF|n YES!==YES! |nGOTO|n OK
- and so the IF test will come out positive, and the GOTO command will be
- executed. Now this doesn't actually change the |nbatch file|n at all, just
- the way DOS interprets the line. The next time DOS executes the batch
- file, it still says |nIF|n %ONE%!==YES! |nGOTO|n OK, but if the environment var-
- iable ONE is set equal to NO this time, then when DOS expands that line,
- it will say |nIF|n NO!==YES! |nGOTO|n OK this time, so that the |nGOTO|n command will
- get ignored because the |nIF|n test fails.
-
- The reason for the exclamation point in my examples, is in case you had
- forgotten to set the |nenvironment|n 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 |nIF|n !==YES! |nGOTO|n OK. That |nIF|n test will
- fail, but at least it won't cause DOS to freak out. If we left out the !
- symbols, and the line said |nIF|n %ONE%==YES |nGOTO|n OK, then when DOS |nexpanded|n
- that, it would say |nIF|n ==YES |nGOTO|n 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 file|n will continue
- <page down> for more
- Batch File continued
- 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-
- 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 file|n. 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:
- |nSET|n AGAIN=
- :START
- (commands you want repeated go here)
- |nIF|n !AGAIN==!NO |nGOTO|n END
- |nSET|n AGAIN=NO
- |nGOTO|n START
- :END
- |nSET|n AGAIN=
- Now, when you execute that |nbatch file|n, the first line sets the environ-
- <page down> for more
- Batch File continued
- 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
- |nbatch file|n commands are executed, and then comes the IF statement. Well,
- since at this point, AGAIN is not set equal to anything, the |nGOTO|n END
- command will be ignored, and the next line, |nSET|n AGAIN=NO will be execu-
- ted. Then the command |nGOTO|n START tells DOS to start over again at the
- label that says :START. So those main commands get executed again, and
- then the |nIF|n test is positive this time, because you already executed the
- |nSET|n AGAIN=NO line, so this time the |nGOTO|n 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 file|n is done.
-
- Another way that the use of |nenvironment|n variables from within a batch
- 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 |nPATH|n, but you don't want to leave that |ndirectory|n on the
- |nPATH|n all the time. Well check out this |nbatch file|n:
- <page down> for more
- Batch File continued
- |nSET|n OLD=%PATH%
- |nSET|n PATH=C:\WORD;%OLD%
- WORDPROC
- |nSET|n PATH=%OLD%
- |nSET|n OLD=
- Now supposing that your PATH variable started out like C:\DOS;C:\UTIL
- then while that |nbatch file|n is being executed, each time you reference
- the |nPATH|n or OLD variables, they will be |nexpanded|n to say C:\DOS;C:\UTIL
- and here's what the |nbatch file|n will look like to DOS:
- |nSET|n OLD=C:\DOS;C:\UTIL
- |nSET|n PATH=C:\WORD;C:\DOS;C:\UTIL
- WORDPROC
- |nSET|n PATH=C:\DOS;C:\UTIL
- |nSET|n OLD=
- So, the first line makes a variable called OLD which is just a duplicate
- of what your |nPATH|n variable says at the moment. The second command sets
- a new |nPATH|n variable, which is C:\WORD; followed by what used to be in the
- |nPATH|n variable a minute ago. Then the WORDPROC program gets run, and then
- <page down> for more
- Batch File continued
- 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 file|n. 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
- |nenvironment|n space by just leaving it there for no reason.
-
- Well speaking of |nPATH|n variables, batch files are an excellent way to keep
- your |nPATH|n short. The only directories that should go on your |nPATH|n, 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 |nPATH|n. Instead, you
- should use a |nbatch file|n (or DOSKEY macro) to make that command's directo-
- ry |ncurrent|n, run the program, and change back to the root |ndirectory|n. Like
- this:
- CD C:\WORD
- WORDPROC
- CD \
- Now you name that |nbatch file|n WP.BAT, and put it into a |ndirectory|n which
- <page down> for more
- Batch File continued
- contains nothing but batch files (name it BELFRY, perhaps, or CAVE, since
- that's where .BATs go), and put that |nbatch file|n directory on the PATH,
- then you can access your WORDPROC program from any |ndirectory|n on your sys-
- tem, just by typing WP <Enter>, even if C:\WORD is not on your |nPATH|n. So,
- after you create such a |nbatch file|n for each of your main applications,
- now the only directories you need to keep on your |nPATH|n are the one where
- you keep your batch 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
- |ndirectory|n for each of them. That last |ndirectory|n you might want to name
- UTIL. So, with only three directories on your |nPATH|n, your whole system
- will be a lot more efficient.
-
- The problem with batch files is that each one takes up an entire cluster
- of disk space, just like any other file, even though batch files are gen-
- erally very small. A 28-byte |nbatch file|n 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 batch files into one great big |nbatch file|n in-
- <page down> for more
- Batch File continued
- 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 batch
- files, since you only have one |nbatch file|n. So just put your big batch
- file into the UTIL |ndirectory|n and remove the |nbatch file|n |ndirectory|n from
- your disk and from your PATH.
-
- If you have DOS version 5.0, you can do just about everything you can do
- with batch files, with DOSKEY |nmacros|n instead, except for accessing envir-
- onment variables, or using the |nGOTO|n or SHIFT commands. |nDOSKEY|n |nmacros|n can
- not do those things. And DOSSHELL menu items can do everything except
- |nGOTO|n and |nSHIFT|n. So if you upgrade to DOS version 5 you should convert
- all your batch files that don't do those things, to one of those options.
- You still need to learn about batch files anyway though, because |nDOSKEY|n
- |nmacros|n and |nDOSSHELL|n menu items are put together by the same rules that
- batch files follow.
-
- And you have to know about batch files in order to keep a nice efficient
- AUTOEXEC.BAT file. That's a very special |nbatch file|n that DOS executes
- <page down> for more
- Batch File continued
- every time you reboot your computer. It's the only |nbatch file|n 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, rebooting is the best way to get that accomplished.
- See the AUTOEXEC.BAT chapter for the reasons.
-
- You might want to have your |nAUTOEXEC.BAT|n contain just two commands:
- |nECHO|n OFF
- C:\DOS\STARTUP
- and then you would have a |nbatch file|n named STARTUP.BAT in your C:\DOS
- |ndirectory|n, which the |nAUTOEXEC.BAT|n would run every time you |nreboot|n. The
- reasoning behind this is that a lot of software programs that you install
- are going to add commands to your |nAUTOEXEC.BAT|n file. Well if you have
- that really simple two-line |nAUTOEXEC.BAT|n 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 two-
- <page down> for more
- Batch File continued
- line |nAUTOEXEC.BAT|n 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 batch files to refer to environment var-
- iables and replaceable parameters, you have to do something special to
- make a |nbatch file|n 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 file|n, DOS will flip out over trying to figure out what |nreplaceable|n
- parameter or |nenvironment|n variable you're trying to reference. Because
- that's what percent signs are supposed to mean in a |nbatch file|n. Well all
- you need to do, is write that filename with two percent signs whenever
- you mention it in a |nbatch file|n. Just refer to the file as HELLO%%.TXT
- whenever you're mentioning it in a |nbatch file|n, and DOS will understand
- then, that it's supposed to ignore that particular percent sign as far
- as special |nbatch file|n processing is concerned.
-
- DOS version 3.3 introduced a special new use for the @ symbol, which is
- good for batch files. If you put @ as the first character of any command
- <page down> for more
- Batch File continued
- line in a |nbatch file|n, then that particular line will not show up on the
- screen while the |nbatch file|n is executing. For example, you know how the
- |nECHO|n OFF command tells DOS not to show the commands on the screen as they
- get executed. But when the |nECHO|n OFF command gets executed, it has not
- been executed yet so the |nECHO|n OFF command itself does show on the screen.
- Well if you have DOS version 3.3 or later, you can say @ECHO OFF instead
- of just |nECHO|n OFF, and then you'll never again see the |nECHO|n OFF command on
- your screen. Isn't that nice?
-
- One thing that a lot of people try to accomplish with batch files, that
- just won't work, is to feed commands or characters to another program.
- 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 file|n
- like this:
-
- <page down> for more
- Batch File continued
- GAME
- Y
- K
- Well guess what's going to happen when you run that |nbatch file|n? 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 file|n
- 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 batch
- file. And since there are no such commands in DOS, as Y and K, then you
- get that error message.
-
- One line of a |nbatch file|n 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
- <page down> for more
- Batch File continued
- 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 |nKeyboard|n Stuffer instead. There are quite a few shareware
- and public domain |nkeyboard|n 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 |nredirection|n. All of these things will create
- batch files. |nEDLIN|n and |nEDIT|n can also change batch files that already
- exist. Any text editor, and any 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
- batch files, from within other batch files, to perform certain functions.
- <page down> for more
- Batch File continued
- For example, if you execute the PATH command with no parameters, it will
- tell you what your |nPATH|n environment variable currently says, like this:
- PATH=C:\DOS;C:\UTIL
- Well, that's the exact same format that you use to enter a new |nPATH|n vari-
- able! So if you were to type |nPATH|n > OLDPATH.BAT that'd use redirection
- to create a new |nbatch file|n named OLDPATH and you could change your |nPATH|n
- to whatever you wanted, and then if you were to later execute OLDPATH
- <Enter>, then that PATH=C:\DOS;C:\UTIL command would be executed, to put
- your |nPATH|n back to the way it had been before you started! This sort of
- thing can be infinitely useful. See the end of the chapter about the
- TIME command for a really interesting example of that.
-
- So check out all the sections that are cross-referenced from this chapter
- and have a great time with batch files!
-
-
-
-
-
- PLEASE IGNORE THIS PAGE!
- |tAUTOEXEC.BAT|t|fSIMPLY1|f
- |tBBS|t|fSIMPLY1|f
- |tDirectory|t|fSIMPLY1|f
- |tEnvironment|t|fSIMPLY1|f
- |tShareware|t|fSIMPLY1|f
- |tbytes|t|fSIMPLY1|f
- |tcurrent|t|fSIMPLY1|f
- |tdownloading|t|fSIMPLY1|f
- |tpublic domain|t|fSIMPLY1|f
- |troot|t|fSIMPLY1|f
- |tASCII|t|fSIMPLY2|f
- |tEDLIN|t|fSIMPLY2|f
- |tExecutable|t|fSIMPLY2|f
- |tParameters|t|fSIMPLY2|f
- |tReplaceable|t|fSIMPLY2|f
- |TCALL|T|fSIMPLY3|f
- |TCON|T|fSIMPLY3|f
- |TCOPY|T|fSIMPLY3|f
- |TFOR|T|fSIMPLY3|f
- |TSHIFT|T|fSIMPLY3|f
- |tBad command or filename|t|fSIMPLY3|f
- |tPATH|t|fSIMPLY3|f
- |TDOSKEY|T|fSIMPLY4|f
- |TECHO|T|fSIMPLY4|f
- |TEDIT|T|fSIMPLY4|f
- |TGOTO|T|fSIMPLY4|f
- |TDOSSHELL|T|fSIMPLY5|f
- |TPAUSE|T|fSIMPLY5|f
- |TREM|T|fSIMPLY5|f
- |tBoot|t|fSIMPLY5|f
- |tCOMMAND.COM|t|fSIMPLY5|f
- |tReboot|t|fSIMPLY5|f
- |tCONFIG.SYS|t|fSIMPLY6|f
- |tKeyboard|t|fSIMPLY6|f
- |tRedirection|t|fSIMPLY6|f
- |tPROMPT|t|fSIMPLY6|f
- |TIF|T|fSIMPLY5|f
- |TTIME|T|fSIMPLY3|f
-