Skip to content

Batch Files

A beginner’s guide to MS-DOS batch files, from the original XRMX programming resources.

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 /P

Special 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 OFF hides the commands (but not PAUSE prompts or DOS messages).
  • ECHO ON shows them again.
  • Put @ in front of any line to hide just that line, so @ECHO OFF hides itself too.
@DIR /W

PAUSE

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:
PAUSE

DOS 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!
:FINISH

Save 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.SYS

Escape 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 ␛.

CodeEffect
0Default (white text, black background)
1Bright text
5Flashing text
7Reverse text
8Concealed text (useful for passwords)
30Black text
31Red text
32Green text
33Yellow text
34Blue text
35Magenta text
36Cyan text
37White text
40Black background
41Red background
42Green background
43Yellow background
44Blue background
45Magenta background
46Cyan background
47White background
ECHO ␛[31;42m    Red text on a green background
ECHO ␛[0m       Back to normal
ECHO ␛[5;33m    Flashing yellow text

This 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
:FINISH

SCREEN 31 43 gives red text on a yellow background. Try other numbers, and see what happens with none.

Try these

  • Write NAME.BAT to show your name in flashing bright red.
  • Write BACK.BAT to 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"p

ATOB.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"p

Programming the function keys

The function keys have extended codes, alone and with Shift, Ctrl or Alt:

KeyAloneShift +Ctrl +Alt +
F10;590;840;940;104
F20;600;850;950;105
F30;610;860;960;106
F40;620;870;970;107
F50;630;880;980;108
F60;640;890;990;109
F70;650;900;1000;110
F80;660;910;1010;111
F90;670;920;1020;112
F100;680;930;1030;113
F110;1330;1350;1370;139
F120;1340;1360;1380;140

To make F10 type DIR /P and press Enter (the 13 at the end):

ECHO ␛[0;68;"DIR /P";13p

This 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