What Are Batch Files?
Batch files are user-written "mini-programs" in which each line is a DOS command. Such files are processed line-by-line, executing commands that have been batched together. Most of the time batch files are created to avoid having to type in a series of commonly-used commands. A simple example might be the following:
CLS
C:
CD\WINDOWS
DIR *.DOC
This batch file will clear the screen, go to the c: drive, change directories to Windows, and list all the files .DOC extensions. If you carry out these four commands on a regular basis, and you’re tired of typing them in each time, a batch file is your solution.
Batches, however, can be quite complicated, executing a long chain of commands. One command in the chain might run a whole new batch file. It is even possible to create smart "batches" that test for certain conditions. For example, the above batch file might be written in such a way that it checks first for the existence of files with .DOC extensions before listing them.
Batch File Names
Batch files are always given a filename with a .BAT extension. From that point on, the filename is considered a DOS command. When you type it, the series or batch of commands is immediately executed. For example, the above file might be given the name WINLIST.BAT. That way, whenever the user wants to see the .DOC documents in Windows, he or she just needs to type the command WINLIST and hit <ENTER>. It’s not necessary to type in the .BAT extension. It’s a very good idea to use file names that easily indicate the purpose of the "batch" and are easy to remember. Also, keep in mind these DOS conventions when creating batch files:
- The filename must be eight characters or less in length.
- Don’t use capitalization, spaces, or punctuation marks in the filename.
- Remember to add a .BAT extension.
- Keep in mind that the full filename, such as WINLIST.BAT, has two parts, a main name and an extension, separated by a period. This will be important later when you are using the DIR command to list the batch files or the TYPE command to view them.
The AUTOEXEC.BAT file
The AUTOEXEC.BAT is a special-purpose batch file that runs each time you start your computer. It is the most important batch file on your computer and must be treated with extreme care. If you accidentally delete you will crash your computer.
Displaying Batch Files
It is possible to display the contents of a batch file without actually executing it. To display the contents of a batch, use the TYPE command. Example: TYPE AUTOEXEC.BAT <Enter>
Batch files may also be loaded into a program called the DOS editor or into your favorite word processor for viewing, editing and printing. When you save a batch file with a word processor. though, you must remember to save the file as unformatted "DOS text" or "ASCII text."
Storing Batch Files
In order to find your batch files quickly and to access them no matter what drive or directory you are currently using, group them together in a directory. Later, we will discuss how to include this directory in what is called the "command path". The "path" is simply a single statement in the AUTOEXEC.BAT file which tells your computer where to look for commands.
When you type in a command your computer starts at the beginning of the path and works it’s way to the end till it comes to the requested command. By the way, the AUTOEXEC.BAT must be in the root directory, which is always the beginning of the path, in order to automatically execute at startup. See "Writing an AUTOEXEC.BAT" later in this handout.
Creating Batch Files
Batch files may be written a couple of ways:
The EDIT command starts up a program called the "DOS editor." You can think of it as a very simple word processor that allows you to create files which are stripped of formatting. When you save a batch file with the DOS editor, it is saved as "DOS text" or "ASCII text", meaning that there are no word processing formatting codes attached to it. It’s bare bones text.
Better yet, use a word processor to create and edit your batch file. Wordperfect for Windows and Word for Windows are especially popular ones, because they have easy options to save the file as "DOS text" or "ASCII text". Wordperfect for DOS (Version 5.2) allows the same function when you save with CTRL + F5.
Common Batch File Commands
Some typical DOS commands found in batch files are the following:
CLS — Clears the screen; resets cursor at upper left of screen.
DATE — Sets the system date.
TIME — Sets the system time.
C:— Goes to the C: drive.
A:— Goes to the A: drive.
CD— Changes directories.
DIR — Lists files in the current directory.
FORMAT A: — Initializes a blank diskette in the A drive.
PATH — Lists directories to be searched for command files.
PROMPT $P$G — Shows the current drive, directory and subdirectory (which is known as the current "path").
COPY — copies a file to a specified location.
REN — renames a file.
DEL — deletes a file.
TYPE — prints the contents of a file to the screen.
EDIT — starts up the DOS editor, allowing you to edit a specified file.
PRINT — prints a specified file.
Batch File Subcommands–ECHO, REM, PAUSE
Besides basic DOS commands, batch files can contain various subcommands typically used only in batch files. These subcommands provide the ability to print messages on the screen, permitting the user to type in conditions, allow "command loops" which execute a series of commands a certain number of times, or even branch off to execute other subsections of the batch file–all of which make the batch more efficient.
ECHO
The ECHO subcommand controls the display of batch file commands as they are executed. Normally, ECHO is "on". That means that as a batch file runs, the individual commands are displayed, along with the computer’s response to those commands. For example, if you run the batch file WINLIST.BAT created earlier, the commands C:, CD:\WINDOWS, and DIR *.DOC would have appeared on screen at the DOS prompt, followed by the output of that command. Naturally, C: and C:\WINDOWS won’t provide any output. But, DIR *.DOC will show both the command and the output. Now, even though the default setting has ECHO turned on, you can turn it off by entering the command ECHO OFF. From that point on, the commands will not show up on the screen. Don’t worry, the output will still show. This, of course, will keep your screen looking neat and uncluttered. The following are examples of how the ECHO command is used:
ECHO Shows the current status of ECHO, whether it is turned on or off.
ECHO OFF Turns off the display of commands. This command is usually put at the beginning of a batch file to suppress the commands in order to show output only.
ECHO ONTurns on the display of commands.
@ECHO The @ is used to prevent the words immediately following it from showing up on screen. So, if you wanted to turn off the ECHO, but you didn’t even want the words ECHO OFF to appear on the screen, you would create the batch file command @ECHO OFF.
ECHO [text]Displays the text following the ECHO command on the screen. This is a way to send messages to the user.
ECHO. ECHO followed by a period inserts a blank line.
ECHO >PRN Routes commands that would normally appear on the screen to the printer.
Here’s an example of how the ECHO command might be used in the WINLIST.BAT file created above:
CLS
ECHO OFF
ECHO This is a list of files in Windows with .doc extensions.
ECHO.
C:
CD\WINDOWS
DIR *.DOC
PAUSE
The PAUSE command temporarily halts the batch file and prompts the user to hit a key to continue. In fact, PAUSE causes the line "Strike a key when ready…" to be printed on screen. But you can add another message to tell a user what to do next. Example: PAUSE CHECK PRINTER
REM
A REM subcommand indicates a remark. Remarks are non-executing lines of the batch file, intended for information only. Type the command REM, followed by a string of up to 123 characters. If ECHO is turned off, REM statements will not show up on the screen when the batch file is executed. Most of the time, users add REM statements as notes to themselves. For example, we can add a REM to our earlier batch: REM This batch file shows the documents in the Windows directory.
CLS
ECHO OFF
ECHO This is a list of files in Windows with .doc extensions.
ECHO.
C:
CD\WINDOWS
DIR *.DOC
Replaceable Parameters
Up to this point, the batch files you have written have performed an operation on drives, directories, file names and extensions specified within the batch commands. The result might be somewhat limited, though.
What Are Parameters?
Fortunately, DOS allows something known as a replaceable parameter, a.k.a. a "dummy parameter" or "variable." That is, if you substitute a % for a drive, directory, file name or extension in your batch file, DOS will understand the % to be a blank space. When you execute the batch, you will need to type in the batch file command immediately followed by the drive, directory, file name or extension you want to fill in the blank.
This setup allows your batch files much greater flexibility. For example, the batch called WINLIST.BAT will only show you files ending with a .DOC extension. To make this batch file more efficient, you could substitute the .DOC with a % character followed by a number (1-9). This will allow you to type in a different extension when you type in the batch file command:
REM This batch file shows specified items in the Windows directory.
CLS
ECHO OFF
ECHO This is a list of files in Windows, extensions based on your parameter.
ECHO.
C:
CD\WINDOWS
DIR *.%1Now, when you want to use the batch file command, you must type the following:
WINLIST DOC – to show files with .DOC extensions.
WINLIST XLS – to show files with .XLS extensions.Note: There is a space between the command and the extension here, not a period.
Multiple Parameters
As you can see, the dummy parameter is a % followed by a number (1-9). You might want to add more than one parameter to a batch file, though. If so, start by using %1, and increase by increments of one. For example, the above batch could be revised to show two lists of documents based on two separate variables:
REM This batch file shows two lists of specified items in the Windows directory.
CLS
ECHO OFF
ECHO Here are two lists of files in Windows specified by your parameter.
ECHO.
C:
CD\WINDOWS
DIR *.%1
DIR *.%2When you enter the batch file command, you would add the two extensions to the command line, separated by a space. For example: WINLIST DOC XLS This would show you two separate lists: A list of .DOC files immediately followed by an inventory of .XLS files.
Invoking Other Batch Files
One interesting feature of batch files is that a command embedded inside one batch can run a whole new batch file with a different name. This is commonly used in creating "menu" batch files. For example, you might have one batch file called MENU.BAT used to print a menu on the screen with options A, B, and C. Each of these letters might, in turn, be separate batch files with the file names A.BAT, B.BAT, C.BAT. That way a user can select option A, B, or C, and by typing in a single letter he or she can start up a different batch. For example, the A.BAT batch might start up Windows, the B.BAT might run Lotus, and the C.BAT might start Paintshop Pro.
CALL
In a similar way the CALL subcommand can be used to link separate batch files. To use the command, add the line CALL immediately followed by the new batch. The current batch file will branch off to execute the other batch, and return later to finish any subsequent commands. For example, let’s say you wanted to break the WINLIST.BAT into two separate batch files, a main one and a secondary one called DOCS.BAT. Let’s suppose you wanted to do this because the DOCS.BAT file is a generic one that lists the documents with .DOC extensions in whichever is the current directory. In that case, your primary batch file, WINLIST.BAT, would look like this:
REM This batch file goes into WINDOWS and calls DOCS.BAT
CLS
ECHO OFF
ECHO.
C:
CD\WINDOWS
CALL C:\DOCS.BAT
ECHO There are no more documents.The CD\WINDOWS command will take you into the Windows directory, and then the batch file called DOCS.BAT will start up. It might look like this:
ECHO Here are the .DOC documents in whichever is the current directory.
DIR *.DOCNote that the syntax for the CALL command is CALL [Drive:] [Path] [Filename]. It is not necessary to include the extension .BAT after a filename used with CALL. Here the main batch file will branch off to execute the batch called DOCS.BAT, but afterward the last line of WINLIST.BAT will be executed, printing the message on screen "There are no more documents."
IF Conditional Statement
The IF subcommand allows condition testing within a batch file. In effect, it allows the batch file to "make decisions" as it runs and to execute appropriate command accordingly. The syntax is IF [Condition] [Command]. The condition must be something that can be evaluated as either true or false. When the condition is true, the command is executed. When it’s false, the command is skipped and the next line of the batch file is executed. Let’s look at a simple example using WINLIST.BAT. Say you wanted to test this condition: If the user asks to see documents with a .DOC extension, then list them. If not, don’t bother. Write it this way:
REM This batch file tests to see if the user wants .DOC documents first, BEFORE calling DOCS.BAT
CLS
ECHO OFF
ECHO.
C:
CD\WINDOWS
IF %1 = DOC CALL C:\DOCS.BAT
ECHO There are no more documents.
Redirection of Input/Output
Normally, DOS allows your computer prints output to the screen. However, you can choose to redirect output to a the printer or to a file. To do so, use the > character. Usually, this character is are used together with the commands TYPE and DIR.
To the Printer
Directing output to the printer means you are sending it out through a port such as LPT1 and LPT2 (for parallel printers, like a dot matrix) or COM1 and or COM2 (for serial printers like a Laser Jet). As an alternative, send the output to the PRN, which stands for "printer." For example:
TYPE LETTER.DOC > PRN – prints LETTER.DOC on the printer.
DIR *.DOC > LPT1 – prints a list of .DOC documents on the printer hooked up to port LPT1.
To a File
Directing output to a file is the same as saving it to a file. Sometimes, for example, you may want to save a list of documents to review or print later. For example:
DIR *.DOC > MYDOCS.LST – saves the list of .DOC documents to a file called MYDOCS.LST.
To append output to an existing file, add the >> sign.
DIR *.DOC >>FILES.TXT - attaches the list to an existing list of documents in the file called FILES.TXT
Writing an AUTOEXEC.BAT
An autoexec.bat is a special-purpose batch file that runs each time you boot your computer. The name stands for "automatically executing." Every time you start your computer, DOS will search for an AUTOEXEC.BAT in the root directory. It is important to store your AUTOEXEC.BAT in this directory.
What Should I Include In My AUTOEXEC.BAT File?
Since it runs each time your boot the computer, an AUTOEXEC.BAT is useful for setting system parameters and startup tasks. The following are common commands:
PATH — sets a search path for commands or other batch files. In effect, it tells the computer where to look for commands. It will start looking in the first directory and work its way to the end of the path. Each directory in the path is separated by a semicolon. See the example below.
PROMPT $P$G— sets the prompt to show you
which drive and directory is currently "active".
Creating An AUTOEXEC.BAT
In order to create a practice AUTOEXEC.BAT, use the A: drive. DON’T MESS WITH YOUR ORIGINAL. If you put one on the A: drive, you will be able to turn off your computer and restart it with the disk in drive A: DOS will look for AUTOEXEC.BAT there first. Use the EDIT command to run the DOS Editor and type this simple example:
ECHO OFF
CLS
PROMPT $P$G
PATH=C:\;C:\DOS;C:\WINDOWS
MENU
Creating a Menu
The last command in this practice AUTOEXEC.BAT automatically invokes another batch file called MENU.BAT. You would, of course, create this second batch with the DOS Editor. It might look like this:
CLS
ECHO OFF
ECHO Main Menu
ECHO.
ECHO 1 – Windows
ECHO.
ECHO 2 – Word for Windows
ECHO.
ECHO 3 – Wordperfect for WindowsThis batch file doesn’t really do anything. It just displays a menu on the screen and allows the user to select an option: 1, 2, or 3. Now you need to create separate batch files for each of the menu items. Each batch file should be named with a number corresponding to one of the menu choices. For example, 1.BAT is
as follows:
C:
CD\WINDOWS
WINThat will start up the program Windows when the user enters the number 1. Note: The command WIN, which doesn’t require the extension .EXE, is actually located in the C:\WINDOWS directory. However, if you have included C:\WINDOWS in the path statement of your AUTOEXEC.BAT file, you don’t need to include it here in the 1.BAT batch file.
Hard Disk Considerations
Size
The size of a hard disk is now measured in megabytes and gigabytes. One thousand megabytes = 1 gigabyte. Hard disks usually come no smaller than 20 megabytes and can be as large as a gigabyte or more. Of course, the most important consideration when purchasing a hard drive is the size requirement of your software. The documentation will tell you, for example, "This product needs 33 megabytes of free disk space." If you want to check the capacity of your hard disk, and the space available, use the CHKDSK command.
Speed
Speed is always an important factor to consider when you are purchasing a hard disk. The faster a hard disk, the more it costs. Here are some important questions to ask the salesperson:
Speed Factor Explanation
Average Access Time The average amount of time,usually measured in
milliseconds, it takes for the computer to
locate information on the hard disk.
Transfer Rate How fast the computer can read from and write to
the hard disk.
Disk Cache Refers to the speed at which the computer can
temporarily use part of your hard disk for
extra processing memory.
Hard Disk Care
Whenever you are moving a computer or hard disk, take great care not to jolt the hardware. Disk heads inside the device may rub against the actual disk and cause damage to the disk–sometimes irreparable–if the disk is moved suddenly. Never move a hard disk while the computer is turned on.
Do not turn the hard disk or CPU containing a hard disk on its side without consulting your User’s Manual.
Place the drive on a sturdy and level surface.
Do not use the hard disk at temperatures below 50 degrees Fahrenheit and above 104 degrees Fahrenheit.
Do not use the hard disk if the relative humidity is not between 20 and 80 percent.
Keep food, drinks, cigarette smoke, and cleaning agents with vapors away from the drive.
Formatting a New Hard Disk
Plug and Play Disks
Some hard disks come already formatted with DOS installed. All you need to do is plug them in. If you turn on your computer without a diskette in drive A: or B: and the computer boots up successfully, then your disk has already been set up and formatted.
Unformatted Hard Disks
If your disk does not come formatted, you will have to format it yourself. Usually, the computer will come with the software necessary to format your disk. Usually the disk is labeled Install or Setup, and you would need to put that disk in drive A: or B: and type in the appropriate command (Install or Setup).
When NOT To Format The Hard Disk
Do not format the hard disk if the computer boots successfully and you see a DOS prompt or some other program come up. Remember, formatting erases all data from the disk.
When To Format The Hard Disk
If you just bought the hard disk and it has not been formatted, you should format it.
Formatting As A Startup Disk
To format the hard disk and transfer the DOS system files so you can start the computer from your hard disk, type: format c:/s
Partitioning the Hard Disk
Before you can format a hard disk, you must partition it. Partition means to divide the disk into two or more separate sections. It’s usually done so that the computer can run operating systems other than DOS. UNIX or XENIX,for example.
However, let’s say that you are just running DOS on your computer, and no other operating system. Then you still might consider partitioning your hard disk to divide it up into separate sections for better organization of your files. These sections are called "logical drives." The first partition is called the "active partition" is always located on the C: drive and contains three very important files needed to run DOS. These are IO.SYS, MSDOS.SYS and COMMAND.COM. The second partition is the one you would divide up into logical drives, giving each a separate letter designation, D – Z. That allows you a total of 23 extra logical drives. Each one of these is like a separate file cabinet that contains it’s own group of directories. How you organize your hard disk is up to you.
You can set up partitions on your hard disk before or after you format it–or during the installation of DOS. In any of these three cases, you use the FDISK command. If you have not formatted the disk, start the system with your DOS diskette in drive A:. At the DOS prompt, type FDISK. Obviously, if you have formatted the disk, you can just type the command at the DOS prompt. A menu comes up allowing you the following options:
Create DOS Partition or Logical DOS Drive
Set Active Partition
Delete Partition or Logical DOS Drive
Display Partition Information
To partition the hard disk during DOS installation run SETUP on you DOS installation diskette. Partitioning is one of the options offered there. Warning: Re-partitioning erases existing partitions. Use extreme caution!
Copying DOS Files to the Hard Disk
When you install DOS, the files are usually copied into the directory labeled DOS. If not, you can set up this directory and save your DOS files there by following these steps:
Put your DOS diskette in drive A:
Create a DOS subdirectory by typing: MD C:\DOS.
Copy the DOS program files from A: to your new subdirectory by typing COPY A:*.* C:\DOS.
Delete the file COMMAND.COM from the DOS directory, since a copy is placed in the root directory during formatting, by typing DEL C:\DOS\COMMAND.COM.
Include the DOS directory in the path statement of your AUTOEXEC.BAT batch file.
Paths
As was mentioned earlier in this handout, the path is a list of drives and directories which tells DOS where to look for commands. If you are in the root directory C:\ and the command WIN is in the Windows directory, DOS will not be able to find the command unless you give it the proper path. Most people put a permanent path in their AUTOEXEC.BAT batch file, listing all their primary drives and directories so that DOS will never have trouble finding a command. However, you can always enter a path at the DOS prompt if the drive or directory you want DOS to search is not specified in the AUTOEXEC.BAT
Displaying The Current Path
To display the current path, type: PATH
Setting The Path
To set the path, type in the full path, beginning with the root directory (C:\) and working your way through each primary directory, separating each with a semicolon.
PATH=C:\;\DOS;\WINDOWS
Canceling The Path
To cancel the current path so that DOS will search only the current directory, type: PATH;
Configuring the System
Another file which allows you more efficient use of your hard disk is the CONFIG.SYS file. It lets you override various system defaults set by DOS. A sample CONFIG.SYS file might look like this:
buffers=22
files=15
device=mouse.sys
buffers= sets the amount of Random Access Memory that DOS reserves for data transferred from the hard disk.
files= sets the number of files that DOS allows to be open at one time.
device= loads what is called a "device driver" from the hard disk.
Some applications require certain values for buffers or files. This information is given in the documentation under Setup or Install. Other applications automatically update your existing CONFIG.SYS with other values when they are installed. Before installing a new application, make a copy of your existing CONFIG.SYS with a name such as CONFIG.BAK. That way, if anything goes wrong, you can restore the original.
A device driver is a file that is needed to run a device such as a mouse, joystick or sound card. When you install such a device, the driver is usually automatically copied to your CONFIG.SYS. If it is not, you might have to add the appropriate line to the CONFIG.SYS. Check the documentation for the appropriate driver.
Backing Up the Hard Disk
When Should I Back Up?
Once the hard disk is set up, you should do acomplete backup. After that, the frequency of backups will depend on how much use your hard disk receives. But at least once a week, you should do a partial backup as illustrated below.
Number The Diskettes
Since the restore command (see below) will prompt you for disks by their numbers, be sure to number your floppy disks as you go. Files must be restored in the order in which they were backed up.
Complete Backups
To do a complete backup of the entire hard disk, including the organization of directories and subdirectories as well as files, type backup c:\*.* a:/s To do a complete backup which will also give you a printed list of the backup files, type backup c:\*.* a:/s >prn
Partial Backups
To do a partial backup after you have done a complete backup, add to the complete backup command the following parameters (you may use more than one at a time):
/a — adds backup files to the current files on the floppy diskette rather than writing over the existing files on the disk. Unless you specify this option, copies of any old files on a backup floppy diskette are erased before the new files are written on it.
Example: C:\>backup c:\*.* a:/s/a
/d mm-dd-yy — backs up all files created or altered on or after the specified date. To use this parameter, you must have been consistent about entering the correct date every time you started the machine.
Example: C:\>backup c:\*.* a:/s/d 4-16-89
/m — backs up all files that have been created or modified since the last backup.
Example: C:\>backup c:\*.* a:/s/m
Daily Backups
You may want to create a batch file to run at the end of each day to back up any files created or modified. The following is an example of such a file, which backs up modified files in any subdirectories, adding them to the existing files on the backup diskette and giving you a printed list of the backup files. Let’s say you created it using the DOS Editor and called it BACKUP.BAT.
backup c:\*.* a:/s/m/a >prn
Restoring Files from Backups
Share on FacebookYou must use the DOS restore command to transfer files created with backups from a floppy to a hard disk. The restore command converts the backed-up files for normal use. Do not attempt to use the DOS copy command to transfer files from backup diskettes. Restore will prompt you to insert backup diskettes in numbered order. Files will be replaced in subdirectories if you specified /s during the backup procedure. To restore all files from floppies back onto a newly formatted hard disk, type
restore a: c:\*.* /s