The magazine of the Melbourne PC User Group

Is it Lunchtime Yet?
Tom Coleman

All too often when talking to users about computing I hear them say "I don't really know much about DOS" or "Batch files are a bit of a mystery to me" or "I wouldn't know how to start programming in Basic".

This is too much of a temptation for me. I am sorry but I just cant resist writing an article about combining DOS and Batch files and Basic.

I read in a recent magazine that someone wanted to know how to write a batch file that knew when it was morning or afternoon so that it could say "Good Morning" or "Good Afternoon" as appropriate.

DOS is not very good at date maths and can't do any time math so it was all a bit of a puzzle.

Of course the Basic language can manage a fair bit of both so it seems appropriate that a bit of quick and dirty Basic programming might do the trick.

I am going to set this out in GWBASIC because everyone gets it with DOS. For those of you who don't know Basic from a bulls foot, hang in there and all will be revealed. Take my word for it.

Basic files usually have a .BAS extension. Providing the file has got a .BAS extension you can execute the file by calling up GWBASIC.EXE and adding the filename as the first parameter thus:- 

GWBASIC FILENAME 
(you dont have to include the extension, providing that it is .BAS)

This will get the program running and it will keep going until it runs out of enthusiasm, unless it is written so that it loops around for ever.

When a Basic program stops running it returns to the Basic environment not DOS. To get out of Basic and back to DOS you must enter the command

SYSTEM 

This terminates the Basic session and returns you to DOS.

However I don't have to wait until the program completely ends. I can put SYSTEM in as the last line in the program so that it would return to DOS as it terminates.

How are you new users going. No sweat so far? Stick with it. I am on your side.

If I wrote a batch file with the line

GWBASIC FILENAME

in it, once the Basic session ended, DOS would take over again at the next line in the batch file.

So all I need to do is write a little Basic program that does something I cant do in DOS and call it up at at some appropriate point in a batch file, let it do its thing and carry on once control returns to the batch file.

So endeth the first lesson. Now if you are with me so far, just tighten your seat belts. It gets a bit more bumpy from here on but the scenery is breathtaking.

If I create a text file called GREETING.TXT or any other name that takes my fancy, I can print out its contents with the DOS command TYPE, thus:-

TYPE GREETING.TXT

This will display the contents of GREETING.TXT For the purpose of this exercise it will have the lines,
Good Morning      (or, as we shall see, Good Afternoon) 
The time now is 14:2 3:44     (or some such) 
Have a nice day

Theres nothing like a cheerful polite text file.

Having got this far I now need a means of writing this GREETING.TXT and then displaying it.

As I hinted earlier we are going to get Basic to write GREETING.TXT and we will use TYPE to display it. Just the job for a batch file.

This one is so short that we can get away with using COPY CON which can otherwise be a little unforgiving at times.

The COPY command comes with COMMAND.COM and gets loaded when you turn the computer on so it is always there. It is what is called an Internal Command. So is the TYPE command mentioned earlier.

CON is a Reserved Word which means it is a special word that DOS reserves for its own use. In other words you cant use it in file names or on the DOS command line.

The CON is the Console. Its the way DOS refers to the monitor and keyboard. So COPY CON is an instruction to DOS to copy something from the Console. The only thing that DOS knows how to COPY from the CON is a file, which must be given a name as part of the command.

I am going to call the file AMPM.BAT. Not very imaginative but nice and short. It is a Batch File so it must have a .BAT extension.

This is how the command looks.

COPY CON AMPM.BAT

Don't be alarmed when you press ENTER on the above line. Your prompt is supposed to disappear.

DOS is now waiting for you to enter the lines that will go into AMPM.BAT.

GWBASIC TIMEDAY 
TYPE GREETING.TXT

Press ENTER at the end of each line.

Then press F6 and press ENTER again.

You can hold down the Ctrl key and press Z instead of pressing F6. Either way it will put a ^Z on the screen. In either case you must then press ENTER which will return you to your DOS prompt.

If you examine your directory (enter DIR) you will find that you have created a file called AMPM.BAT.

The file contains two DOS commands. The first runs the file TIMEDAY.BAS in Basic and the other displays the contents of the file GREETING.TXT

The last thing to do is to write the file TIMEDAY.BAS.

Here it is in all its gory. This is what you must copy if you want to use it. I will explain it line by line shortly. 

10 IF VAL(TIME$) THEN TEXT$(1)= "Good Morning" ELSE TEXT$(1)= "Good Afternoon" 
20 TEXT$(2) = "The time now is" 
30 TEXT$(3) = TIME$ 
40 TEXT$(4) = "Have a nice day" 
50 OPEN "O", 1, "Greeting.TXT" 
60 FOR Z= 1 TO 4 
70 WRITE #1, TEXT$(Z) 
80 NEXT Z 
90 CLOSE 1 
100 SYSTEM

You could create it using COPY CON but it would be very frustrating unless you are an absolutely error-free typist.

If you have GWBASIC.EXE in the PATH or in the current directory the following will be much easier. Enter

GWBASIC

Your DOS prompt will disappear and you will go into Basic.

For now stay well away from your function keys and copy the above lines of code.

Be sure that you copy each line EXACTLY and that you press ENTER on every line. If you move off with the arrow keys then go back and press ENTER before you finish.

The Ins and Del keys work fine as does the backspace.

If the screen is getting cluttered with typos and error messages you can press Fl and then ENTER Alternatively you can just type in LIST and press enter. Either way you get a listing of what you have done so far and you can edit the new listing.

Once you are satisfied that it is all OK you can press F4. on a blank line. This will cause the text SAVE" to be displayed.

Enter the file name TIMEDAY. That's enough. Make sure that there are no leading spaces. The first character (the T) goes next to the "

Press ENTER and the lines you have written will be saved to the file TIMEDAY.BAS. You can SAVE as often as you like but each save overwrites the last save if you use the same file name. If you save using different file names you will end up with lots of redundant copies.

If you are just starling out you should have the files GREETING.TXT, TIMEDAY.BAS, AMPM.BAT and GWBASIC.EXE all in the same subdirectory. If you are more DOS literate then you can suit yourself.

Now when you type in AMPM it should all come true.

OK. Smoko. Undo your seat belts. Get out and stretch your legs. Well done. Hooray for us. The bus will be leaving in 45 minutes. The toilets are around the back. Somebody put the billy on.

Those stalwart souls who actually typed in the ten lines of Basic listed above have probably worked out what they do without my having to explain them.

For those of you who would like a bit of an explanation, Here we go.

Basic will store numbers in variables, which is a fancy way of saying a combination of letters such as AAA or XYZ or FLEA or NOSE. A single letter will do. A or X or g. It is not case sensitive.

So A=24 or LOTS=1000000000 or BBB=9

It will store words or characters in what are called string variables which looks like any other variable except that it ends with a $. Like A$ or X$ or AAA$ or TEXT$ or FLEA$.

I think I said in an earlier article that BIN$ might hold "Rubbish" or FLEA$ might hold "ITCH" or YES$ might hold "NO" if you have a warped sense of humour.

In this case I made TEXT$ store a number of different strings of characters by numbering it. It's called giving it a dimension.

Lines 20, 30, and 40 don't need much explaining. Maybe line 30 is worth commenting on. TIME$ is a permanent string that always holds the time. Finding the value of TIME$ is to find the current time.

Line 10 needs a bit of translating. IF VAL(TIME$) is pretty obvious. If the value of TIME$ is less than 12 is what it means.

You see the VAL only reads the numbers in a string until it strikes a non number. The colon (:) separator in the time (eg 15:12:33) causes the VAL to only read the hour part of the time.

The rest of the line just about speaks for itself. So that the first line is saying. If the time is less than 12 then TEXT'$(1) contains the string "Good Morning" otherwise it contains the string "Good Afternoon".

Line 50 OPENS file number 1 as "Greeting.TXT' for "O"utput from the computer. That's how Basic can write to the file. Because the instruction was not to "A"ppend the data the whole file is re-written from scratch each time.

Line 60 is the top of a loop. The first time this line is used Z is equal to 1. The next time it is 2 and so on to the limit which is 4 in this case.

Line 70 writes TEXT(Z) to the open file number 1. First time through it writes TEXT(1), which was set to "Good Morning" or "Good Afternoon" in line 10.

Second time through Z will be equal to 2 so TEXT(2) gets written which is 'The time now is" which was set by line 20.

Line 80 sends the program back to line 60 to have another go and increase Z by 1.

So the whole loop executed 4 times. Each time one of the lines of text gets written to the file.

Once the loop runs out of things to do line 90 closes the file and line 100 gets out of Basic and back to DOS as was mentioned earlier.

In short, Line 10 tests to see if the time is less than 12 o'clock and sets the first line to Good Morning or Good Afternoon.

Other lines store the text and the time.

All of the text is written to the file Greeting.txt by the lines 60 - 90.

Line 100 returns to DOS.

As the whole thing was started from the batchfile AMPM.BAT control is returned at the line that instructs DOS to print the file GREETING.TXT, which has just been written by TIMEDAYBAS.

Once you get to thinking about it there are a number of non DOS things that you can do. Simple little things that DOS is not very good at.

Ifs enough to make you want to know a bit more about Basic and Batch files.

You could vary line 10 to test for Christmas day and print "Merry Christmas", or your family birthdays or access a file full of horoscopes or even something useful.

Think about it. But not for too long.

Thinking too much about computing only confuses you. Computing is a practical subject where you learn hands-on practical skills.

Thinking promotes knowledge. Doing generates experience.

Reprinted from the October 1991 issue of PC Update, the magazine of Melbourne PC User Group, Australia

[About Melbourne PC User Group]