The magazine of the Melbourne PC User Group

Some Useful BAT Samples
Stephen Davey

Some sample Batch files, fully explained, that will help you get the most from your computer. (This article is the third of a series written mainly for beginners and an understanding of the commands detailed in the previous articles will greatly help in the understanding of this one. See PC Update, November 1988, Beginning with BAT and PATH; and Jan-Feb 1989 Another innings with BAT.)

Having found, from my own (sometimes slow) learning experience, that seeing simple useful examples of programming or of .BAT files is the best way to come to grips with the generally simple but sometimes bewildering principles - here are a few examples with detailed explanations of how they work.

Note: Each batch file is listed with a number (in brackets) in front of each line. This number is only to identify quickly the particular line for the purposes of discussion in this article. These numbers must not be included in any files you might create.

Easy Directory Changing

Those of you with a hard disk system with several directories will appreciate just how tiresome it can be to type in repeatedly a complex directory name each time you want to change to that directory.

This batch file, once set up, will allow you to select quickly the directory you require with four or five key strokes no matter how long the directory name is.

Let us imagine that your hard disk has the following directories -

C:\ 
C:\DOS 
C:\WORDPRO 
C:\WORDPRO\DOCUMENT 
C:\WORDPRO\DOCUMENT\LETTER 
C:\GRAPHICS 
C:\GRAPHICS\PICS
C:\WORKAREA 
C:\WORKAREA\AREA2

Many of your systems will, of course, have many more, but for the time being this will illustrate the technique.

Now create a batch file, called for example `CHD.BAT', that has the following lines -

(1) ECHO OFF 
(2) CLS 
(3) IF NOT /%1/==//
(4) ECHO ********************************
(5) Hard Disk Recovery
(6) ECHO 1 -- Root Directory C:\
(7) ECHO 2 -- C\DOS
(8) ECHO 3 -- C\WORDPRO
(9) ECHO 4 -- C\WORDPRO\DOCUMENT
(10) ECHO 5 -- C\WORDPRO\DOCUMENT\LETTER
(11) ECHO 6 -- C\GRAPHICS
(12) ECHO 7 -- C\GRPHICS\PICS
(13) ECHO 8 -- C\WORKAREA
(14) ECHO 9 -- C\WORKAREA\AREA2
(15) ECHO 
(16) ECHO *******************************
(17) ECHO Type CHD then the required number
(18) GOTO END
(19) :1
(20) CD\
(21) goto direct 
(22) :2 
(23) cd\DOS 
(24) goto direct 
(25) :3 
(26) cd\WORDPRO 
(27) goto direct 
(28) :4 
(29) cd\WORDPRO\DOCUMENT 
(30) goto direct 
(31) :5
(32) cd\WORDPRO\DOCUMENT\LETTER 
(33) goto direct 
(34) :6 
(35) CD\GRAPHICS 
(36) goto direct 
(37) :7 
(38) CD\GRAPHICS\PICS 
(39) goto direct 
(40) :8
(41) CD\WORKAREA goto direct :9 CD\WORKAREA\AREA2 goto direct :direct DIR/W :END
(42) goto direct 
(43) :9 
(44) CD\WORKAREA\AREA2 
(45) goto direct 
(46) :direct 
(47) DIR/W 
(48) :END

At the DOS prompt we now type CHD (the name we called the batch file), press enter, and a summary of our disk directories is shown with a number beside each. To change to a particular directory, all we have to do is type CHD again followed by a space and the number of the required directory. The batch file will then change to that directory.

If, however, we can remember the number designated to the directory we can type CHD and the number the first time, and the batch file will change directly to the directory without first displaying the screen summary of all the directories.

Here's how it works

Line 1 & 2 - Turns the ECHO off, so that only the important lines are shown on the screen. CLS clears the screen ready for the display.

Line 3 - Checks to see if any variable (ie a number in this case) was typed after the CHD. If there was, then the batch file will proceed directly to the corresponding label. For example if you typed CHD 4, then the file would jump to the label :4 on line 28. If nothing was typed after the CHD then the file will proceed to line 4.

Line 4 to 16 - Sets up a screen display with a summary of your directories with a number (or if you prefer a letter) beside each. The order in which you set these out is entirely up to you and you may prefer to use a more fancy text file to set up the screen display. In this case you would substitute the following single line in place of lines 4-16

TYPE DISPLAY.TXT

(DISPLAY.TXT being the name of the file you created.) With both these methods of creating a screen display, take care to ensure that the display is no bigger than your screen height or you will end up having the top lines scroll off the screen in order to get the bottom lines on.

Line 17 - Prints the reminder message on the screen. This line could be included in the text file described above.

Line 18 - Now takes us to the end and back to the DOS prompt, leaving the display on the screen, allowing us to either enter any DOS command, or select a directory number and type, for example, CHD 6. In this instance the CHD file would discover that the 6 had been typed after the CHD (Line 3) and direct the file to the label :6 on line 34. (1) (2) (3) (9) (5) Line 19 - is the label corresponding to a particular directory. (6 ) In this case 1 for the root directory C:\. We could have just as easily chosen the letter C and made line 19 - (9 )

Line 19, 20 & 21 - For each directory we include in our summary, we now need these three lines.

Line 20 - is the DOS change directory command that actually changes the directory to the one required.

Line 21 - now, having changed the directory, directs us to the label DIRECT on line 46.

Lines 22, 23, 24 through to 43, 44, 45 - are groups of three lines for each directory and you can have as many as you like. My CHD file has 37 directories so I have 37 groups of 3 lines like this!

In each case the first line is the label corresponding to the directory on the screen display.

The second line is the DOS change directory command for that directory and the third line directs the file to the DIRECT label near the end of the file.

Line 46 - is the label that we have directed all the other lines to once the directory has been changed. We could have of course, picked any name for this label.

Line 47 - displays a DOS directory of all the files on the directory you have just chosen. The /W after the DIR command just means that the directory of files will be displayed across the screen rather than down. You could use DIR /P if you prefer. This line could be omitted, but I find it most useful to automatically see a listing of all the files on the directory I have just changed to.

Line 48 - ends the file and returns you to the DOS prompt.

Time Well Spent

Although it may take you a little time to set up this file it will save you a considerable amount of time in the long run if you are constantly changing directories.

Cleaning Out the Rubbish!!

Often after a period of time various files accumulate on our hard disk that are no longer required. Backup files (.BAK) in particular, while often a "lifesaver", can become a pest.

This little batch file I've called KILL.BAT simply displays all the *.BAK files in your directory, then gives you the opportunity to delete them all.

(1) ECHO OFF 
(2) CLS
(3) DIR *.BAK /W 
(4) ECHO ******************************
(5) ECHO Press ^C if you do NOT want to 
(6) ECHO delete ALL these files 
(7) ECHO ******************************
(8) PAUSE 
(9) DEL *.BAK


Line 1 - turns the echo off so that only the required lines are "echoed" on the screen.

Line 2 - clears the screen

Line 3 - displays a directory of all the files that have the extension .BAK. The /W makes the directory spread across rather than down the screen.

Line 4, 5, 6 & 7 - displays the following on the screen under the directory -

**********************************
Press ^C if you do NOT want to 
delete ALL these files
**********************************

Line 8 - Halts the batch file and displays the screen message "Strike any key when ready" giving you the opportunity to either press any key - to continue and delete all *.BAK files - or - press ^C (control C) which will halt the batch file and return you to DOS without finishing the last command in the file on line 9.

Line 9 - If ^C is NOT pressed then this line is reached and all the *.BAK files will be deleted.

To use this file effectively simply change to the required directory using the previous CHD.BAT file then run KILL.BAT to display then delete any *.BAK files.

Reprinted from the April 1989 issue of PC Update, the magazine of Melbourne PC User Group, Australia

[About Melbourne PC User Group]