|
Below are some simple batch file examples to whet your appetite. AUTOEXEC.BATAUTOEXEC.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].
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 #1Here 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.
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 #2Suppose 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:
Instead of typing a complicated command each time, create a batch file called LOOKUP.BAT as follows:
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:
Clever use of DOS will save you money! Batch Sample #3Call 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.
GETKEY.COMGETKEY.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:
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. |
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.