HTA TutorialHTA Tutorial #10

So, now we've created a file, time now to read it back again. The program looks like this.

<HTML>
<BODY>
<HEAD>
<title>Save</title>
<HTA:APPLICATION>

<Script Language=VBS>

sub subLoad()   
dim fs
dim a

Const ForReading = 1
  
	Set fs = CreateObject("Scripting.FileSystemObject")
	Set a = fs.OpenTextFile("hello.txt", ForReading)
	msgbox(a.ReadLine)
	a.Close
end sub
</SCRIPT>
</HEAD>
<body scroll="no">
<INPUT TYPE="Submit" ID="btnQuit" VALUE="Quit App" onclick=self.close()>
<INPUT TYPE="Submit" ID="Read" VALUE="Read" onclick=subLoad()>
</BODY>
</HTML>

This is very similar to the previous tutorial and differs only in subroutine and it's name. The code breaks down as follows:

<HTML>
<BODY>
<HEAD>
<title>Save</title>
<HTA:APPLICATION>

<Script Language=VBS>

This is all the same as before.

sub subLoad()

A new name for a new routine that could be integrated into a much larger program that could include the routine in the previous tutorial.

dim fs
dim a

Some variables are created to hold all the object references. These are local to this subroutine and cannot be accessed by other subroutines or th main program.

Const ForReading = 1

This differs slightly from the previous one and it means that instead of using a number, a more meaningful variable name can be used.

Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.OpenTextFile("hello.txt", ForReading)

Same as before except that we are reading from the file.

msgbox(a.ReadLine)

For the sake of simplicity, I am merely displaying the contents of the file using a msgbox which will only read the first line of the file irrespective of how many lines there are in the file.

a.Close
end sub
</SCRIPT>
</HEAD>
<body scroll="no">
<INPUT TYPE="Submit" ID="btnQuit" VALUE="Quit App" onclick=self.close()>
<INPUT TYPE="Submit" ID="Read" VALUE="Read" onclick=subLoad()>

Everything is the same as before except that the button is called Read and the routine being called is subLoad. The quit button still works as before.

</BODY>
</HTML>

Now it might be useful to be able to test whether a file exists or not. Here is the code.

<HTML>
<BODY>
<HEAD>
<title>Exist</title>
<HTA:APPLICATION>

<Script Language=VBS>

sub subReportFileStatus()
Dim fs, msg

  Set fs = CreateObject("Scripting.FileSystemObject")
  If (fs.FileExists("hello.txt")) Then
    msg = "file" & " exists."
  Else
    msg = "file" & " doesn't exist."
  End If
  msgbox(msg)
End sub

</SCRIPT>
</HEAD>
<body scroll="no">
<INPUT TYPE="Submit" ID="btnQuit" VALUE="Quit App" onclick=self.close()>
<INPUT TYPE="Submit" ID="btnExist" VALUE="Exist?" onclick=subReportFileStatus()>
</BODY>
</HTML>

Now hopefully, if you've been following the whole tutorial, you'll have a good idea of what is going on here. The file object has another routine we can call that will test whether a file exists or not (see how useful objects can be) and it is simply a matter of deciding which message to display depending on the results of the IF statement.

I'll leave you with a little project to try. Could you combine the files from this tutorial with the previous one to create a program that will only create a file if it doesn't exist and only read from it if it exists. You'll have three buttons : Quit, Read and Create with the relevent routines in the code. Next time, I'll show you what it should look like and we'll try to append to a file instead of overwriting it. Have fun. Remember, send me an email if you get stuck.

Back

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