Batch File Examples
Microcomputer/DOS Tutorial


Computer Studying

Navigation
{NavForms}

Virus Tutorial Map
Tutorial Map

Below are some simple batch file examples to whet your appetite.

AUTOEXEC.BAT

AUTOEXEC.BAT is a special batch file name that, if found in the root directory of the boot disk, will automatically run before control of the computer is turned over to you.

You might want to always load particular files on starting the computer and the commands to do this would be in AUTOEXEC.BAT.

Typically, the AUTOEXEC.BAT file is used to set the system up to your particular needs. This includes setting the PATH to define where DOS will look for commands, defining various variables in the DOS environment and setting the PROMPT to look like you want it to.

If you want to terminate the AUTOEXEC.BAT file (or any other batch file) before it is complete, issue the break command [Control-Break].

@Echo OFF
Path C:\DOS;C:\;C:\BAT;C:\UTILITY;
Prompt $p$g
Set TEMP=C:\Temp
C:\Utility\NumLock -
CD\
CLS
Type C:\Bat\Menu.TXT

This file sets the PATH, defines a prompt and a temporary directory, runs a utility program and then clears the screen and types a menu.

Batch Sample #1

Here is a sample batch file you might use to periodically back up and clear a word processing data disk. It assumes that word processing backup files are named with extension .BAK, you don't want them, and they are on the disk in drive B:. The batch file is called BAK.BAT and is also on the word processing data disk in drive B:. Start it by typing B:BAK.

@ECHO OFF
CLS
ECHO * * * Word processing cleanup * * *
ECHO Put backup disk in drive A:
PAUSE
ERASE B:*.BAK
COPY B:*.* A:
DEL A:BAK.BAT
ECHO Backup is complete. Label your disk.

The first line turns screen echo off. The next line clears the screen. A label is then displayed with instructions on where to place the backup disk. A keypress needed to continue, then all .BAK files are erased and all files on drive B: are copied to A:. The batch file is then deleted from A: and a termination message shown.

Your situation might be different and you may not want to delete .BAK files first, but this should give you an idea of what you can do.

Batch Sample #2

Suppose you have a phone list in a file named FONE on your disk. There is a DOS utility called FIND which allows you to search files for specific text strings. The general format for using FIND is:

FIND "text" filename.

Instead of typing a complicated command each time, create a batch file called LOOKUP.BAT as follows:

@ECHO OFF
FIND "%1" FONE

This file is called up by typing LOOKUP NAME at the system prompt. Assuming that you have typed the name in all caps, you might see a response like:

C> LOOKUP JONES
JONES Tom (805) 555-1212 Tom owes me $50

Clever use of DOS will save you money!

Batch Sample #3

Call this file MENU.BAT. It demonstrates branching. See below for creation of GETKEY which stops the computer, waits for a keystroke and returns its ASCII value as a program error level. (The ASCII value of 1 is 49, 2 is 50 and 3 is 51.)

An IF ERRORLEVEL test is true if the error level tested is less than or equal to the actual error level. Therefore, such tests have to be in reverse order or the first will always be true.

@ECHO OFF
:START
CLS
ECHO --------------------
ECHO 1 - Wordprocessing
ECHO 2 - Spreadsheet
ECHO 3 - Exit menu to DOS
ECHO --------------------
:QUERY
ECHO 1, 2, or 3?
GETKEY
IF ERRORLEVEL 52 GOTO QUERY
IF ERRORLEVEL 51 GOTO L3
IF ERRORLEVEL 50 GOTO L2
IF ERRORLEVEL 49 GOTO L1
GOTO QUERY
:L1
Commands necessary to call up word processor
GOTO START
:L2
Commands necessary to call up spreadsheet
GOTO START
:L3
CLS

GETKEY.COM

GETKEY.COM is a small file that you need to create to allow the example above to work. If you are not comfortable with the procedure outlined below DO NOT DO IT. The program DEBUG can be dangerous when used improperly.

DEBUG comes with most versions of DOS. You type everything highlighted:

C:\> DEBUG
-E 100 B4 00 CD 16 B4 4C CD 21
-N GETKEY.COM
-R CX
CX 0000
:8
-W
Writing 0008 bytes
-Q

The DEBUG command starts the DEBUG program. The prompt for that program is a hyphen. The "E" command enters data and the "N" command names a file. The "R" command finds out what's in register CX and then enters a new value (the number of commands in this case). The "W" command tells DEBUG to write the file and "Q" quits the DEBUG program and drops you back to the DOS prompt.

You should now have the file GETKEY.COM on the default drive. Try it in a batch file like the one above.

Keep Reading Batch Endnotes


{NavBar}
Home | Products | Definitions | Newsletter | Virus Tutorial | Contact


FILExt is proudly hosted by DewaHost.com - a fast and reliable host for everyone!
DewaHost offers premium Web hosting starting from $8.95/month and a high speed file hosting service FileBurst!

Please use the contact form for questions or comments about this web site.
Copyright © 2001 Computer Knowledge, All Rights Reserved
Pray the Rosary for peace.