Solved

Can I read data from a text file using vbscript in a html file?

Posted on 2008-09-30
7
973 Views
Last Modified: 2012-05-05
I have done a lot of windows apps programs and vbscript files, but not web apps, hence my confusion.  Can I read a text file using the filesystemobject in a vbscript in an html file?  I just need to read some data from the text file.  I don't want to use a database and a table if I can read the data in a text file.  

the html file runs fine on my pc but I get the error when I move the html to the server and try to run it in a browser on my pc.

Here is my html
<HTML>
<HEAD>
<TITLE>Test</TITLE>
</HEAD>
<BODY>
Start <br>

<SCRIPT LANGUAGE="VBSCRIPT" runat="server">
on error resume next
dim fso
set fso = CreateObject("Scripting.FileSystemObject")      '''error occurs here
msgbox "fso error - " & err.description
</script>

End <br>
</BODY>
</HTML>
-------------------
the msbox displays "fso error - Activex component can't create object".

I can move the script to a.vbs file and run in on the server without the error.  It can create the filesystemobject in a vbs script file on the server,  but the script in the html file gets an error trying to create the filesystemobject on the server.

This seems like a simple thing to do but I'm just getting started with web apps and not sure what works or doesn't.  Can I do what I want???

Thanks





0
Comment
Question by:utahalee
  • 4
  • 3
7 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 22612142
Try changing this line:
set fso = CreateObject("Scripting.FileSystemObject")

to this
Set fso = Server.CreateObject("Scripting.FileSystemObject")

Regards,

Rob.
0
 

Author Comment

by:utahalee
ID: 22613954
Thanks for the answering the question.
I now get an "object required" error instead of the "Activex component can't create object" error.
Any other suggestions?  



0
 
LVL 65

Expert Comment

by:RobSampson
ID: 22619325
Which line gives you that error? There is no other line in the above code that uses that fso object.

You might have something like:

<SCRIPT LANGUAGE="VBSCRIPT" runat="server">
on error resume next
dim fso
set fso = CreateObject("Scripting.FileSystemObject")
Set objFile = fso.CreateTextFile("NewFile.txt", True)
objFile.Write "Something"
objFile.Close
</script>


note that because you have used
Set FSO = CreateObject("Scripting.FileSystemObject")

you must call the methods of the filesystemobject using the FSO object name, such as
fso.CreateTextFile

If you were to try
fso2.CreateTextFile

you'd get "Object Required"

Regards,

Rob.
0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 

Author Comment

by:utahalee
ID: 22624809
I changed my line to Set fso = Server.CreateObject("Scripting.FileSystemObject") as you suggested.

I get the "Object Required"  error on that line now instead of the Activex component can't create object that I got before I added "Server." to the createobject.

I can't use error the FileSystemObject methods becasue of the error I get when I try to create the FSO object.

The script works fine outside of the html.  Should I be able to use the FileSystemObject in vbscript in an html file to check if a  text file exists on the server?  If it exists I would use the filesystemobject to read the  textfile and format in a table on the web page.  I haven't done much with scripts in web pages so I hope this can work.

Thanks for all your help so far.

0
 
LVL 65

Accepted Solution

by:
RobSampson earned 100 total points
ID: 22628655
Hmmm, yes that should work:
http://www.eggheadcafe.com/articles/20030627b.asp

I'm really not sure.  You do need to be running the page on a Windows server though, but I'm not sure if other operating systems support it....I think you can get VBScript for Unix, but I can't remember.

Regards,

Rob.
0
 

Author Closing Comment

by:utahalee
ID: 31501546
Thanks for your help and verifying that what I want to do should work.  I am running on a windows server.  Further investigation of the error message is leading me down the path that the problem is with the server and something needs to be changed or fixed on it.  Thanks.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 22639269
No problem. Thanks for the grade.

Perhaps you need to look into re-installing or repairing the VBScript engine...

Regards,

Rob.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Making a simple AJAX shopping cart Couple years ago I made my first shopping cart, I used iframe and JavaScript, it was very good at that time, there were no sessions or AJAX, I used cookies on clients machine. Today we have more advanced techno…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn how to count occurrences of each item in an array.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question