If there is a set of DOS commands that you use frequently, or someone else who doesn't know DOS needs to use them, the commands can be groped together in a BATCH file. The commands will then be executed in the order in which they appear in the file by typing a single command. Almost any DOS command can be used in a BATCH file.
Batch files always have the file extension .BAT. You can choose any filename except one which is already used by DOS or another piece of software. MYFILE.BAT is a perfectly good filename. FORMAT.BAT is not (because there is a DOS program called format).
Running BATCH Files
To run a BATCH file you simply type its name - without the extension. So if you have a Batch file called MYFILE.BAT you would simply type MYFILE to run it.
Sample BATCH file
Here is a simple BATCH file listing:
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 can be included in a BATCH file. In addition, there are some special commands which can only be used in a BATCH file. Some of these are :
This is used to put comments in the batch file to make it easier to understand, e.g.:
REM Now get the next file.
This is used either to write text to the screen during the running of the batch program or to suppress screen output.
If you don't have an ECHO command in your batch program all the commands in it will appear in the screen as the program runs. This is not always convenient
ECHO OFF - Turns off all the screen output except PAUSE and DOS messages.
ECHO ON - Restores printing of the screen output
If you want to suppress the ECHO OFF message do this :
Any selected DOS command can be hidden from the screen with an @
@DIR /W PAUSE
This halts the running of the batch program to enable the user to take some action - changing a floppy disk for example. It also prompts the user to make this action e.g.:
PAUSE Please put the disk in DRIVE A:
This will print the message
ADVANCED BATCH FILESThe IF Command
The IF command is used to control the way the batch file runs depending on the circumstances.
When you type a parameter after a batchfile mane the parameter can be used within the batch file. Within the file itself %1 is used to specify parameter 1, %2 for parameter 2 etc.
Labels can be used in batchfiles. They always start with a ':' in the left hand column. You can jump to a label with the GOTO command.
Here is a very simple example showing the use of an IF statement a parameter and labels. It also shows the use of GOTO:
@ECHO OFF REM BATCH Program to show how parameters & if statements REM are used IF "%1" == "YES" GOTO YES ECHO You have NOT typed YES GOTO FINISH :YES ECHO You have typed YES! :FINISH ECHO ON
Generate this as a batch file with a suitable name (TEST.BAT for example)
Try running the Batch file by typing TEST, then afterwards try running it by typing TEST YES. Try making the YES lowercase. What happens?
The FOR…DO command
This is used to repeat a series of DOS commands using parameters specified within the program.
The parameters are specified like this :
FOR %%a IN (PARAM1 PARAM2 PARAM3) DO etc…..
%%a becomes PARAM1 on the first pass, PARAM2 on the second pass, PARAM3 on the third pass. After the DO comes any valid DOS command. For example, to delete all files with the names TEMP.*, NEW.* and NEXT.*…
FOR &&a IN (TEMP NEW NEXT) DO DEL %%a.*
Instead of %%a you can also use %%b, %%c etc.
Computer ArchitectureUsing batch files for screen control
It is easily possible to control the colour of your screen and/or the position of the courser using batch files providing the ANSI.SYS screen driver is installed.
If you are using your machine at home you need the like:
DEVICE=C:\DOS\ANSI.SYS or DEVICE =C:\WINDOWS\COMMAND\ANSI.SYS in your CONFIG.SYS file
ANSI escape sequences
All ANSI escape sequences commence like this :
ESCape key, the [
This cannot be done directly from the DOS prompt since ESCape simply clears whatever you have just typed.
What you have to do is use the ECHO command to write the ESCape sequence to the screen from within a batch file
If you are using EDIT to generate the batch file, you need to hold the CTRL key down and press P, then ESCape. What appears on the screen when you do this is !.
Here are the screen codes for the different colours, etc.
0 Default (white text, black background) 1 Bright text 5 Flashing text 6 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
The escape sequences need to be sent to the screen from a batch file using the ECHO command. Here are some examples:
In every case the is produces by holding CTRL and typing P, and then hitting the ESCape key. (in EDIT)
The following batch file is called SCREEN.BAT. enter and run It.
@Echo off REM Screen control batchfile which sets up colours REM Two parameters supplied REM First one is text colour REM Second one is background colour REM deal with no parameters situation using flashing message in bold IF "%1" == " " GOTO MESS1 REM else set text to desired colour, etc Echo [%1m REM Deal with no second parameter IF "%2" == " " GOTO FINISH REM Else set Background colour Echo [%2m GOTO FINISH REM Write Flashing bright yellow error message :MESS1 Echo [5;33;1m CLS Echo NO PARAMETERS SUPPLIED! REM Then reset to normal text and usual prompt Echo [0m GOTO FINISH :FINISH
You run the file by typing SCREEN followed by up tow numbers taken from the list above which set the text and background colours to what ever you want.
SCREEN 31 43 - Gives you red text on a yellow background.
Try various numbers, and also see that happened if you forget to provide any, by just typing SCREEN.
Try out these!
Write a batch file called NAME.BAT that writes your name in flashing bright red on the screen.
Moving the cursor and re-assigning keysESCape sequences can also be used to control the screen position of the cursor and change what the keys do.
Cursor position commands end with a A, B, C, D, or H and key assignments ones with a p.
Controlling Cursor Position
Remembering that the is obtained by pressing CTRL + P and then ESCape, here are some examples:
'A' moves the cursor up a specified number of lines:
This moves the cursor up 5 lines and then prints 'marvin' on the screen
'B' moves the cursor down a specified number of lines:
This moves the cursor down 7 lines and prints 'italic' in the screen
'C' moves the cursor right a specified number of columns
'D' moves the cursor left a specified number of spaces
'H' moves the cursor to a specified row and column position:
This moves the cursor to row 13, column 40 and prints 'xrmx' on the screen
Reassigning KeyYou can reassign keys like this:
When you run this and press 'a' you will get 'b'.
The following batch program called ATOB.BAT does both upper and lower case As:
@ECHO OFF REM Make the 'A' key in to a 'B' key ECHO ["A";"B"p ECHO ["a";"b"p
N.B. Make sure you make a BTOA.BAT file as well other wise you will need to reboot your computer to get back A
Programming the function KeysIn the same way that you can change what the normal keys do, you can also re-assign any of the other keys providing you know what their codes are.
The Functions keys F1 to F12
These are so-called soft keys and are often programmed to provide special functions.
These keys have special 'extended' codes both when used alone and when combined with SHIFT, CTRL or ALT. Here is the list:
How to Program a function key
You simply use the usual ESCape sequence. For example, suppose you want to press F10 instead of typing DIR/p:
The 13 at the end is an ENTER. If you don't put this in, you will need to press ENTER after F10.
Here is a batch program that reassigns two Function keys and gives you a menu:
@ECHO OFF REM Reassign F1 to list current directory REM Reassign F10 to give DOS version ECHO [0;59;"DIR/w/p";13p ECHO [0;68;"VER";13p REM Now Put a menu on the Screen in Bright Yellow CLS ECHO [1;33m ECHO [12;20HF1 List Current directory ECHO [14;20HF10 Print DOS Version Number REM Reset display to normal ECHO [0m
Type this file in (menu1.bat) and run it. Press F1. This will give a directory listing. Press F10. The DOS Version will be displayed.