The magazine of the Melbourne PC User Group

Beginning with BAT and PATH
Stephen Davey

A guide to some of the simpler, useful DOS tools for those new, and perhaps not so new, to computers.

Many 'Computer Experts', software manuals and magazines (including, at times, this one) often assume that all new users already have an advanced understanding of computers and the associated jargon. The gap between the novice and the expert begins to seem too vast even to attempt to bridge, and the DOS manuals are of little help. While they may give a good description of what each command does, often there is no discussion as to why the command is used. This article addresses the beginner. Experts should proceed to the next article. The only assumptions I have made in this article are that the reader is at least familiar with some word processing) program and the basic DOS commands (or at least has access to a DOS manual).

DOS - The Disk Operating System

DOS, generally either PC-DOS (Personal Computer Disk Operating System) or MS-DOS (Microsoft DOS), is the basic system that lets you or a program tell the computer what to do. Once installed in the machine's memory, DOS takes 'instructions' from programs or commands entered at the keyboard. The most common of these commands are the DOS commands such as DIR (lists Directory files), DEL (deletes files), and COPY (copies files). Other programs or commands that DOS will act upon are in fact files containing special sets of instructions. These command files have a name and a three-letter extension of either COM, EXE, or .BAT. (eg FORMAT.COM, COMMAND.COM, WORD.COM, ALJTOEXEC.BAT), however the extension does not have to be typed with the command name. For instance, to use the WORD.COM program you would only have to type 'WORD' at the prompt. While the creation of .COM and .EXE files is probably beyond the normal beginner, the creation of .BAT files is both simple and extremely useful.

Batch Files

Batch files (which carry the extension .BAT, and are often therefore called .BAT files) are simply ASCII text files containing DOS commands. The simplest batch files might only contain one or two lines. Some might contain hundreds. These files can easily be created and edited with any word processing program that allows you to create a text or unformatted file. Almost all the major programs are suitable for this purpose. When a batch file is 'run' (simply by typing the file name at the DOS prompt), the system reads each line of the file and carries out the command on that line before proceeding to the next. Where you have a complex or lengthy chain of DOS commands, inserting them in a batch file saves you having to re-type all the commands each time you use them. While it might take a little extra time to set up these files, the time saved in the long run can be considerable.

The AUTOEXEC.BAT File

When your computer is switched on (or booted up), after DOS has been loaded, it looks for a file named AUTOEXEC.BAT. If it finds this file it automatically carries out all the commands it contains before doing anything else. Depending on who set your system up, you may or may not have such a file already. A simple AUTOEXEC.BAT file might just contain two lines, for example:

DATE
TIME


In this example both lines are simple DOS commands that could also have been entered from the keyboard. The first line simply instructs the system to ask you to insert the correct date, and the second line asks you to set the correct time. If your system is used only for word processing, the AUTOEXEC.BAT could contain a third line that would automatically start the word processing program after you had entered the date and time. The real power of batch files is that they speed up the entry of complex path and directory commands. Before we take a more detailed look at batch files, we should make sure we understand the system of file storage in directories and the paths to those directories.

Directories

All of the files used by, or created by the computer are stored on a magnetic disk. This disk can be a small 3.5 inch or 5.25 inch floppy disk, or a hard disk. Whatever type of disk you have, the concept of directories is more or less the same, but while floppy disks can be used without directories, it would be very difficult to use a hard disk efficiently without them. A directory is simply an area on the disk that is used to store a certain group of related files so that you can easily find them. Within a directory you can have other sub-directories to store other files. Imagine if the numbers in your telephone directory just contained thousands of numbers listed in no particular order. Finding the number you want would be a matter of either amazing good luck or laborious searching. Fortunately the book is arranged so that all the very important numbers (police, ambulance, fire brigade etc) are listed at the front, then all related numbers arranged in 'directories', ie Government departments, subscribers' numbers, and Yellow Pages. Each of the three main areas is, of course, broken down into smaller, alphabetically-arranged sub-directories. Your computer disk could be arranged in a similar manner. The most important files, such as COMMAND.COM and AUTOEXEC.BAT, could be stored 'up the front' in what is called the ROOT directory; all your DOS program files in a directory called DOS; and your word-processing files in a directory called, say, WORD-P. This directory might have a sub-directory called BUSINESS allocated to hold all your business letters, and another called PERSONAL to hold all your personal letters.

Making Directories

To make a directory, use the DOS command MKDIR (or MD). For instance, to create a directory called WORD-P on a disk in drive A, first log on to drive A by typing:

A

  then

MKDIR A:\WORD-P

  To make the Document sub-directory simply type:

A:\WORD-P\DOCUMENT

A Typical Disk Directory

A typical arrangement of a disk (remembering that A: could equally be B: or C:, depending on which drive you are using) might be:

A:\ (The root directory containing the main DOS command programs, AUTOEXEC.BAT and CONFIG.SYS files. Note that you do not have to 'make' this directory - it is always there once a disk has been formatted.)

A:\DOS (This directory would contain all the other DOS command files such as FORMAT.COM, PRINT.EXE, BACKUP.COM etc.)

A:\WORD-P (This directory could contain all the program files required to run your favourite word processing program.)

A:\WORD-P\DOCUMENT (This sub-directory of the WORD-P directory would be used to hold all your text files created by the word processor. Note that you cannot make this directory until you have made the directory it originates from, ie A:\WORD-P).

A:\WORD-P\DOCUMENT\LETTERS (This sub-directory of the sub-directory DOCUMENT could be used to hold only those text files that are letters.

You could make as many directories and sub-directories as you have groups of files that need separating, such as word processing, database or graphics etc.

PATH to our files

Having set up a system of directories, you now need to be able to let the computer know how to access their contents. If you have a double-disk-drive system, we identify one drive as A: and the other as B: and to copy a file from one to the other we simply type:

COPY A:FILENAME (here inserting the name of the file required) B:

If we have directories set up on the disks, we need to be more specific as to where the file comes from or goes to. To do this we must include a PATH in the command. If for instance we want to copy a file from the WORD-P\DOCUMENT sub-directory on drive A and put it in a directory called WORK on B, then we would need to type:

COPY A:\WORD-P\DOCUMENT\FILENAME B:\WORK

Here \WORD-P\DOCUMENT is the PATH to the file, or in other words, where to find the file called FILENAME. The \WORK is the PATH to the destination, or where you want the file to go.

Changing to a Directory

When we want to swap from one disk to the other we simply type A:, B: or C: etc. To swap to different areas (directories) of the same disk we must use the Change Directory command CHDIR or CD. For example to swap to the WORD-P directory on disk C you would first have to log on to the C drive by typing C: then type CD \WORD-P. When you type in the name of a command such as a batch file or program name, the computer assumes that the command file is located in the directory you are in at the moment, unless you have preceded the command with a path. For instance, if you type the command WORD, the system will look in the current directory for the file WORD.COM, WORD.EXE or WORD.BAT. If, however, you type C:\WORD-P\WORD it will go to the WORD-P directory of disk C: to look for the file, even if you are logged on to drive B.

The PATH Command

You can avoid having to type in this command path each time by telling the computer where to search for .COM, .EXE and .BAT files. For instance the command

PATH=A:\; B:\; C:\; C:\DOS

would make the computer search for your command file first in the root directory of disk A (A:\), then in the root directory of B, then the root directory of C and finally the DOS directory of C (C:\DOS). So no matter which directory you are in, when you type in a command, if the system fails to find the file on that directory, it then searches in the order outlined in your path command. This PATH command would be best inserted in your AUTOEXEC.BAT file, so that whenever your system is switched on it knows where to look for your important files. Our sample AUTOEXEC.BAT file would now look like this:

DATE 
TIME 
PATH= A:\; B:\; C:\; C:\DOS
WORD


(Note that we need the path command before any other program commands so the system can find the program. Here the system would look for the WORD file in the disk directories listed in the path command.) It is interesting to note that we could have three files such as WORD.COM, WORD.EXE or WORD.BAT, all of which are meaningful to the system. However the system looks first for .COM or .EXE and then for.BAT files. Care should be taken therefore that your batch file names are not the same as any .COM files you may have, otherwise you will have to type in the name and extension to get it to run. The PATH command cannot be used to set up a searching path for data files (there are, however, a few public domain utilities around that will.) Although the creation of many different directories can group all your similar files together so that you can easily find them, as you can see it can also make some of your DOS commands a little long-winded. This is where the batch files become indispensable.

Setting up a batch file

Let us assume that you are using the word processing program MS-WORD, the program files of which are kept in the WORD-P directory on disk C, and you want to keep your text files in the sub-directory DOCUMENT. Each time you wanted to start the program, you would have to type the following:

C: (to log on to the correct disk drive) 
CD\WORD-P\DOCUMENT (to change to the document directory so that your text files will be stored there)
C:\WORD-P\WORD (the name and path to the main program.)

Typing these three lines each time you want to start the program could be avoided by putting the same three lines in an ASCII or text file, called, for example, WP.BAT. The WP.BAT file would look like this:

C: 
CD\WORD-P\DOCUMENT 
C:\WORD-P\WORD


Now with this file in your root directory, all you need to do is type 'WP' (then press ENTER) and the system will do the rest! Note that if you have included the correct PATH command in your AUTOEXEC.BAT file, you can type the command WP and get the right result, no matter what directory you are in.

Special Batch File Commands

There are some special commands that are extremely useful for batch files. Here we will briefly look at just a few. (Other more complex commands will be dealt with in future articles.)

CLS: This command clears the screen of any previous display, leaving just the DOS prompt.

REM: Sometimes you might want to include remarks in your batch files to help identify what the file does. The command REM will do nothing more than display any following text on the screen.

ECHO: Normally each line of a batch file is displayed on the screen as it is read by the system. Sometimes, you may not want this to happen. If the command ECHO OFF is included, then the following lines will not be displayed. Any REM lines for instance will not be displayed. The ECHO ON command switches the display back on. With ECHO OFF, a line of text can be displayed on the screen by just using the ECHO command by itself, eg:

ECHO OFF 
REM This bat file starts 
REM the word-processing program 
ECHO Insert disk in Drive A 


In this example, the two REM lines would not be displayed, but the line `Insert disk in Drive A' would be. PAUSE: Sometimes you might want to stop the batch file continuing while some action is carried out. The PAUSE  command stops the batch file and displays the message 'Strike a key when ready', eg: 

ECHO Insert disk in Drive A 
PAUSE 

Would produce on the screen the following two lines:

Insert disk in Drive A 
Strike a key when ready

After a keystroke, the batch file would continue with the next command.

Putting It All Together

How can all of this help you? Let us suppose that the system you, and perhaps others use, has three main applications: Word processing, Database and Graphics. You could set up a very simple menu system using batch files to simplify starting these programs. First, you would need to make a batch file for each of the three applications. We could, for instance, make a W.BAT, D.BAT and G.BAT file, each of which would contain the path and start-up instructions for each application, similar to our earlier WP.BAT example. Second, we would need a batch file, perhaps called MENU.BAT. This file would look something like this:

CLS 
ECHO OFF 
ECHO
*******************************************
ECHO MENU FOR THIS SYSTEM 
ECHO . 
ECHO WORD PROCESSING -- W 
ECHO DATA BASE -- D 
ECHO GRAPHICS -- G 
ECHO
*******************************************
ECHO (Press the letter of the required item 
ECHO then press ENTER) 


When MENU was typed at the prompt, the following would appear on the screen:

**************************************
MENU FOR THIS SYSTEM
.
WORD PROCESSING -- W 
DATA BASE -- D 
GRAPHICS -- G
.
**************************************
(Press the letter of the required item then press ENTER)


Because there are no other commands in our MENU.BAT file after the last ECHO command, the DOS prompt reappears ready for the operator to press W, D, or G. These are, of course, just the names of the associated batCH files. We could have called our batch files 1.BAT, 2.BAT and 3.BAT and replaced the W, D, and G choice in the menu with 1, 2 and 3 etc. Note that to give a blank line we have inserted a full stop at the start of the line, as the ECHO command will not work on a blank line. There are other ways around this which we will go into in future articles. Finally, if we add the word MENU (or whatever name you have called your MENU.BAT file) as the last line of all our batch files, when we exit from each application the system will automatically run this file to redisplay the menu again, ready for the next choice. If we also add MENU as the last line of our AUTOEXEC.BAT file, then when the system is switched on the menu will automatically be displayed. Our example AUTOEXEC.BAT file would now look like this:

DATE 
TIME 
PATH= A:\; B:\; C:\; C:\DOS
MENU


Our example WP.BAT file (Called W.BAT in the menu) would now look like this:

C: 
CD\WORD-P\DOCUMENT
C:\WORD-P\WORD
MENU


There are many much more complex, interesting and useful techniques and features of batch files that we will go into in future articles.

1) Without a word processing program the files can be created using the DOS command: COPY CON FILENAME.BAT. When you have finished entering all your lines, press F6 then Enter to close the file.

2) ASCII - American Standard Code (for) Information Interchange. A standard 8-bit code used for exchanging data within a computer or between computers. For batch file purposes this simply means a text file without any special control codes that are sometimes used by various word processing programs.

3) When using a WP program to create a batch file, make sure that you save the file in ASCII, UNFORMATTED, TEXT or NON-DOCUMENT mode. For instance, if you are using MS-WORD, save the file as UNFORMATTED.

Reprinted from the November 1988 issue of PC Update, the magazine of Melbourne PC User Group, Australia

    

[About Melbourne PC User Group]