HTML Applications TutorialHTML Applications Tutorial #6

So who needs more boring loops. What you will want to do however is read and write to files. These will be stored on the local drive NOT on the Internet and can therefore be regarded as safe (or at least as safe as any other file created by an application). So as usual, the program looks like this and must be cut and pasted into your template file (see previous tutorials if you've lost it).

dim a
dim b
dim fs
dim filename
filename="hello.txt"

Set fs = CreateObject("Scripting.FileSystemObject")
Set b = fs.CreateTextFile(Filename, True)
b.Close

msgbox "Created"

When run, this will create a file called hello.txt in the same directory as the HTA which contains nothing at all. Here's how it works. First create a few temporary variables that will hold references to objects.

dim a
dim b
dim fs

Now we need a variable to hold a filename.

dim Filename
Filename="hello.txt"

First, we create an object that knows how to deal with the file system. Don't worry too much about objects. You've doubtless come across the word before and been completely put off by the kind of language used to describe them. Forget all that. An object contains programs called methods that can do useful things for you and makes getting to the methods easier. That's it in a nutshell (the more knowledgeable amongst you will be tutting by now - I've simplified okay?).

Set fs = CreateObject("Scripting.FileSystemObject")

Now we have a reference to the filesystem object, one of the methods contained within it is to create a file. The next line will create the file with the filename we've used above.

Set b = fs.CreateTextFile(Filename, True)

And then we close the file because we don't intend to write anything to it. Yet.

b.Close

Nice message to the user just to confirm that something happened.

msgbox "Created"

The file having been created can now be written to which is what the next program does. Start a new template file and cut and paste this into it.

Const ForAppending = 8

A const (short for constant) is a variable that cannot be changed whilst the program is running. It holds a number that tells the file system object that we want to add to the existiing file. We setup some variables as before.

dim a
dim fs
dim Filename
Filename="hello.txt"

As before, we need a reference to the file system object.

Set fs = CreateObject("Scripting.FileSystemObject")

And now, we use the open text file method to open a file to be added to rather than overwritten.

Set a = fs.OpenTextFile(Filename, ForAppending)

And now we want to write something to it. The writeline method adds a carriage return and linefeed to the end. When this file is opened in Notepad then it will display each character on two seperate lines.

a.WriteLine("a")
a.WriteLine("b")

Close the file because we've finished. Failure to close a file may result in the file remaining empty or becoming corrupted.

a.close
msgbox "Written"

Running this application more than once will merely append the same letters to the file making it larger and larger. Running the creation program will create a new version that will be empty again. So now to read from the file you've created. Some variables and a reference to the filesystem as before.

dim fs
dim a
dim retstring

Set fs = CreateObject("Scripting.FileSystemObject")

The file is opened.

Set a = fs.OpenTextFile("hello.txt")

Now comes a loop. The program will keep going through the file until it reaches the end displaying each line in a message box as it goes. The file is then closed as usual.

Do While a.AtEndOfStream <> True
retstring = a.ReadLine
msgbox retstring
Loop
a.Close

This time, provided the file has been created then this will read in the first line then the second then repeat until the end of the file has been reached. No attempt is made to store the characters as this involves the use of arrays which of course will be the subject of a future tutorial. In the meantime, try changing the programs to create different files and try complete words and sentences to see how this might be useful in creating a simple database program. If you're really impatitient then try the sample program which you can navigate to from the main Weblication page. Have fun.

Back

Written by Nick Cheesman. Last updated: 01/04/2004
Please eMail me at:
nickjc@nickjc.co.uk if you need help