Introduction
If there’s a set of DOS commands you use often, or someone who doesn’t know DOS needs to run them, you can group them together in a batch file. Typing one command then runs them all, in order. Almost any DOS command can go in a batch file.
Batch file names
Batch files always end in .BAT. You can use any name except one already used by DOS or another program.MYFILE.BAT is fine; FORMAT.BAT is not, because DOS already has a FORMAT command.
Running a batch file
Type its name without the extension. To run MYFILE.BAT, type MYFILE.
A simple batch file
REM FILENAME = TEST.BAT
REM This file clears the screen and lists
REM your current directory a screen at a time
CLS
DIR /PSpecial batch commands
Nearly all ordinary DOS commands work in a batch file. A few extra commands only make sense inside one:REM, ECHO and PAUSE.
REM
Adds a comment to make the file easier to understand. DOS ignores it.
REM Now get the next file.ECHO
Writes text to the screen, or turns off the display of each command as it runs. Without it, every command appears on screen as the batch file runs, which isn’t always what you want.
ECHO OFFhides the commands (but not PAUSE prompts or DOS messages).ECHO ONshows them again.- Put
@in front of any line to hide just that line, so@ECHO OFFhides itself too.
@DIR /WPAUSE
Stops the batch file until the user presses a key, for example to change a floppy disk.
ECHO Please put the disk in drive A:
PAUSEDOS then shows Press any key to continue . . .
Advanced batch files
Parameters
Anything typed after the batch file’s name is passed in as a parameter. Inside the file, %1 is the first parameter, %2 the second, and so on.
Labels and GOTO
A label is a line starting with a colon, like :FINISH. GOTO FINISH jumps to it.
IF
Runs a command only when a condition is true. This example uses IF, a parameter, labels and GOTO:
@ECHO OFF
REM Shows how parameters and IF statements are used
IF "%1" == "YES" GOTO YES
ECHO You have NOT typed YES
GOTO FINISH
:YES
ECHO You have typed YES!
:FINISHSave it as TEST.BAT, then try TEST and TEST YES. What happens if you type yes in lower case?
FOR … DO
Repeats a command once for each item in a list:
FOR %%a IN (PARAM1 PARAM2 PARAM3) DO command%%a becomes PARAM1 on the first pass, PARAM2 on the second, and so on. You can also use %%b, %%c and so on. To delete every file named TEMP.*, NEW.* and NEXT.*:
FOR %%a IN (TEMP NEW NEXT) DO DEL %%a.*Screen control with ANSI.SYS
With the ANSI.SYS driver loaded you can change screen colours and move the cursor from a batch file. Add one of these lines to CONFIG.SYS:
DEVICE=C:\DOS\ANSI.SYS
DEVICE=C:\WINDOWS\COMMAND\ANSI.SYSEscape sequences
Every ANSI sequence starts with the Escape character followed by [. You can’t type Escape at the DOS prompt, so ECHO it from a batch file. In the DOS EDIT program, hold Ctrl and press P, then press Escape. Below, the Escape character is shown as ␛.
| Code | Effect |
|---|---|
| 0 | Default (white text, black background) |
| 1 | Bright text |
| 5 | Flashing text |
| 7 | Reverse text |
| 8 | Concealed text (useful for passwords) |
| 30 | Black text |
| 31 | Red text |
| 32 | Green text |
| 33 | Yellow text |
| 34 | Blue text |
| 35 | Magenta text |
| 36 | Cyan text |
| 37 | White text |
| 40 | Black background |
| 41 | Red background |
| 42 | Green background |
| 43 | Yellow background |
| 44 | Blue background |
| 45 | Magenta background |
| 46 | Cyan background |
| 47 | White background |
ECHO ␛[31;42m Red text on a green background
ECHO ␛[0m Back to normal
ECHO ␛[5;33m Flashing yellow textThis batch file, SCREEN.BAT, takes up to two codes for the text and background colours:
@ECHO OFF
REM Sets the screen colours. %1 = text colour, %2 = background colour
IF "%1" == "" GOTO MESS1
ECHO ␛[%1m
IF "%2" == "" GOTO FINISH
ECHO ␛[%2m
GOTO FINISH
:MESS1
REM No parameters: show a flashing bright yellow warning
ECHO ␛[5;33;1m
CLS
ECHO NO PARAMETERS SUPPLIED!
ECHO ␛[0m
:FINISHSCREEN 31 43 gives red text on a yellow background. Try other numbers, and see what happens with none.
Try these
- Write
NAME.BATto show your name in flashing bright red. - Write
BACK.BATto make all text bright yellow on a red background.
Moving the cursor
Cursor commands end in A, B, C, D or H:
ECHO ␛[5Amarvin Up 5 lines, then print "marvin"
ECHO ␛[7Bslarty Down 7 lines, then print "slarty"
ECHO ␛[10C Right 10 columns
ECHO ␛[10D Left 10 columns
ECHO ␛[13;40Hxrmx Row 13, column 40, then print "xrmx"Reassigning keys
Key assignments end in p. After this, pressing a types b:
ECHO ␛["a";"b"pATOB.BAT does both upper and lower case. Make a BTOA.BAT to swap them back, or you’ll need to restart to get your A key back!
@ECHO OFF
REM Make the A key into a B key
ECHO ␛["A";"B"p
ECHO ␛["a";"b"pProgramming the function keys
The function keys have extended codes, alone and with Shift, Ctrl or Alt:
| Key | Alone | Shift + | Ctrl + | Alt + |
|---|---|---|---|---|
| F1 | 0;59 | 0;84 | 0;94 | 0;104 |
| F2 | 0;60 | 0;85 | 0;95 | 0;105 |
| F3 | 0;61 | 0;86 | 0;96 | 0;106 |
| F4 | 0;62 | 0;87 | 0;97 | 0;107 |
| F5 | 0;63 | 0;88 | 0;98 | 0;108 |
| F6 | 0;64 | 0;89 | 0;99 | 0;109 |
| F7 | 0;65 | 0;90 | 0;100 | 0;110 |
| F8 | 0;66 | 0;91 | 0;101 | 0;111 |
| F9 | 0;67 | 0;92 | 0;102 | 0;112 |
| F10 | 0;68 | 0;93 | 0;103 | 0;113 |
| F11 | 0;133 | 0;135 | 0;137 | 0;139 |
| F12 | 0;134 | 0;136 | 0;138 | 0;140 |
To make F10 type DIR /P and press Enter (the 13 at the end):
ECHO ␛[0;68;"DIR /P";13pThis menu, MENU1.BAT, sets up F1 and F10 and shows what they do:
@ECHO OFF
REM F1 lists the current directory, F10 shows the DOS version
ECHO ␛[0;59;"DIR /W /P";13p
ECHO ␛[0;68;"VER";13p
REM Draw a menu in bright yellow
CLS
ECHO ␛[1;33m
ECHO ␛[12;20HF1 List current directory
ECHO ␛[14;20HF10 Show DOS version number
ECHO ␛[0m