|| Tips Index || DOS : Navigation | Memory | Hard Disk Configuration | Auto Execute | Batch | Misc
Batch Files
The simplest batch files are text files containing lines of commands. The commands are the same ones you would type at a DOS prompt to perform any operation.
Batch files can save you keystrokes by saving multiple commands in a single file.
By putting the commands in a batch file and saving it with a familiar name, you only have to type that name to do every command in the batch file.
Creating Batch Files
Use the DOS editor edit.com or Windows Notepad.exe to create the files. If you do not want the commands displayed when they execute, make the first line of the file: @Echo Off
Batch Files Language
Batch files have a programming language that allows them to control systems, act as program menus, time events, etc. Taking the time to learn a few commands will give you more control over your system.
Here are a few general commands:
Note: Items in [] are optional.
- Echo [on/off] [text]
- Turns screen display on or off until the command is given again. Any text following this command is displayed.
You can use an '@' sign to start a command line and prevent it from displaying any information.
- Echo.
- Displays a blank line on the screen. Use this to space the information on the screen.
- Cls
- Clears the display screen.
- Pause
- Stops, displays 'Press any key to continue...
Press [Ctrl+C] to end batch file... Wait until a key is pressed to continue processing the batch file.
- :name
- A Label - use to indicate a position in the file. Use the GOTO name to jump to this point in the file.
- Goto name
- Jumps to the indicated position in the file.
- If Exist [filename] [action]
- Tests to see if [filename] exists, if true the [action] is performed. If false, the [action] is skipped. The [action] can be any valid DOS command. To check for any filename, use wildcards or nul which exists in all directories.
- Call batch file
- Use to run another batch file then return to this one. This batch file maintains control of the processing. Menuing batch files use this command to keep control of the system. See the sample below.
- Choice [/c:choices] [/T:c,nn] [query]
- Displays the query text followed with a question mark.
Use this command to control your batch file. A startup menu is simple with this command. See the sample below.
Waits for nn seconds for a key to be pressed, if none is, it chooses c (must be a choices option).
The only keys recognized are the choices following the command. By default they are [y]es/[n]o, but they could be any letters or numbers you want.
Use If errorlevel # to determine the input. Each choices is returned as a number; first letter returns an errorlevel value of 1, the second value is 2, etc.
If there are 3 choices, test for errorlevel 3 first (the last option in choices), then 2, then 1.
I have a sample menuing batch file program to help you understand the use of these commands. To look at the file and play with it, click here.
|